import requests, os from constants import * SDAROT_DOMAIN = "sdarot.buzz" DOWNLOAD_DIRECTORY_PATH = 'downloads' class SdarotApi: def __init__(self): self.session = requests.session() self.session.headers.update({ 'Referer': f'https://www.{SDAROT_DOMAIN}/', 'accepts': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' }) def get_cookies(self): sdarot_cookies = self.session.get(f"https://{SDAROT_DOMAIN}/index").cookies['Sdarot'] self.session.cookies.update({"Sdarot": sdarot_cookies}) return {"Sdarot": sdarot_cookies} def login(self, username, password): form_data = { 'username': username, 'password': password, 'submit_login': '' } try: response = self.session.post(f'https://{SDAROT_DOMAIN}/login', data=form_data) if FAILED_LOGIN_MESSAGE in response.text: return False return True except Exception as ex: print('Error in login', ex) raise ex def get_series_dictionary(self): try: series_json = self.session.get(f"https://www.{SDAROT_DOMAIN}/ajax/index?srl=1").json() return series_json except Exception as ex: print('Failed to get series dictionary', ex) raise ex def get_series_seasons_list(self, sid): try: response = self.session.get(f'https://{SDAROT_DOMAIN}/watch/{sid}') return response except Exception as ex: print('Failed to get serie seasons', ex) def get_series_season_episodes(self, sid, season): try: response = self.session.get(f'https://{SDAROT_DOMAIN}/ajax/watch?episodeList={sid}&season={season}') return response except Exception as ex: print('Failed to get season episodes', ex) def get_episode_token(self, sid, season, episode): form_data = { 'preWatch': 'true', 'SID': sid, 'season': season, 'ep': episode } try: res = self.session.post(f"https://www.{SDAROT_DOMAIN}/ajax/watch", data=form_data) return res.content.decode() except Exception as ex: print('Failed to get episode token', ex) def get_episode_url(self, token, sid, season, episode): form_data = { 'watch': 'true', 'token': token, 'serie': sid, 'season': season, 'episode': episode, 'type': 'episode' } try: res = self.session.post(f"https://{SDAROT_DOMAIN}/ajax/watch", data=form_data) watch_json_response = res.json()["watch"] qualities = [int(quality) for quality in watch_json_response.keys()] max_quality = max(qualities) return "https:" + watch_json_response[str(max_quality)] except Exception as ex: print('Failed to get episode url', ex) def download_episode(self, episode_url, file_path): full_download_path = os.path.join(DOWNLOAD_DIRECTORY_PATH, file_path) try: response = self.session.get(episode_url, stream=True, timeout=5) with open(full_download_path, "wb") as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk) except Exception as ex: print('Failed to download episode', ex)