Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Iwara to python scraper #2137

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions scrapers/Iwara.yml

This file was deleted.

90 changes: 90 additions & 0 deletions scrapers/Iwara/Iwara.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import re
import json
import requests
import sys
from datetime import datetime
import py_common.log as log
from py_common.cache import cache_to_disk

def fail(message: str):
print(f"Error: {message}", file=sys.stderr)
sys.exit(1)

@cache_to_disk(key="iwara_auth_token", ttl=86400)
def login(force=False):
"""Logs in to get an auth token"""
if force:
return relogin()

username = "YOUR_USERNAME"
password = "YOUR_PASSWORD"
# if no credentials defined, return "undefined to bypass login"
if (username == "YOUR_USERNAME"):
log.info("Iwara login not specified")
return "undefined"
login_url = 'https://api.iwara.tv/user/login'
payload = {'email': username, 'password': password}
response = requests.post(login_url, json=payload)
if response.status_code != 200:
log.error("Iwara login failed")
sys.exit(1)
return response.json().get('token')

def relogin():
"""Forces a new login"""
return login(force=True)

def get_video_details(video_id, token):
try:
headers = {'Authorization': f'Bearer {token}'}
response = requests.get(f'https://api.iwara.tv/video/{video_id}', headers=headers)
response.raise_for_status()
if response.status_code == 401:
token = relogin()
return get_video_details(video_id, token)
elif response.status_code == 404:
fail("404 - Video might be behind login wall")

video_data = response.json()
except requests.RequestException as e:
fail(f"Failed to fetch video data: {e}")
except json.JSONDecodeError:
fail("Failed to decode JSON from response")

return {
"title": video_data.get('title'),
"url": f"https://www.iwara.tv/videos/{video_id}",
"image": f"https://files.iwara.tv/image/thumbnail/{video_data.get('file', {}).get('id')}/thumbnail-00.jpg",
"date": datetime.strptime(video_data.get('createdAt'), "%Y-%m-%dT%H:%M:%S.%fZ").date().isoformat(),
"details": video_data.get('body'),
"studio": {
"Name": video_data.get('user', {}).get('name'),
"URL": f"https://www.iwara.tv/profile/{video_data.get('user', {}).get('username')}"
},
"tags": [{"name": tag.get('id')} for tag in video_data.get('tags', [])]
}

def sceneByURL(params):
token = login()
video_url = params['url']
match = re.search(r'/video/([^/]+)/', video_url)
if not match:
fail("Invalid video URL")
video_id = match.group(1)
return get_video_details(video_id, token)

def sceneByFragment(params):
token = login()
video_id = params['video_id']
return get_video_details(video_id, token)

if __name__ == "__main__":
calledFunction = sys.argv[1]
params = json.loads(sys.stdin.read())

if calledFunction == "sceneByURL":
print(json.dumps(sceneByURL(params)))
elif calledFunction == "sceneByFragment":
print(json.dumps(sceneByFragment(params)))
else:
fail("This scrape method has not been implemented!")
20 changes: 20 additions & 0 deletions scrapers/Iwara/Iwara.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: iwara
# requires: py_common, requests, re, json, sys, datetime # specify additional Python packages if necessary

sceneByURL:
- url:
- iwara.tv/video/
action: script
script:
- python
- Iwara.py # Replace with your actual Python scraper file name
- sceneByURL

sceneByFragment:
action: script
script:
- python
- Iwara.py # Adjust the file name as necessary
- sceneByFragment # Stash or the user will supply the video ID extracted from a filename or other source

# Last Updated December 15, 2024