load-balancer / api.py
ChandimaPrabath's picture
0.0.2.3 V Beta. added download episode.
1fa8166
import requests
import logging
class InstancesAPI:
def __init__(self, instances):
self.instances = instances
def fetch_reports(self):
reports = {}
for instance_url in self.instances:
try:
response = requests.get(f"{instance_url}/api/get/report")
response.raise_for_status()
reports[instance_url] = response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Error contacting instance {instance_url}: {e}")
return reports
def download_film(self, instance_url, title):
"""
Download a film to an instance.
If the download started, it returns a JSON like this:
example:
{"film_id": "my_spy_2020",
"status": "Download started"}
If the film has already been downloaded, it will return the video file.
"""
data = {}
try:
response = requests.get(f"{instance_url}/api/film/{title}")
response.raise_for_status()
data = response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Error contacting instance {instance_url}: {e}")
data = {"error": str(e)}
return data
def download_episode(self, instance_url, title, season, episode):
"""
Download a film to an instance.
If the download started, it returns a JSON like this:
example:
{"film_id": "my_spy_2020",
"status": "Download started"}
If the film has already been downloaded, it will return the video file.
"""
data = {}
try:
response = requests.get(f"{instance_url}/api/tv/{title}/{season}/{episode}")
response.raise_for_status()
data = response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Error contacting instance {instance_url}: {e}")
data = {"error": str(e)}
return data