add elad files

This commit is contained in:
2023-07-15 18:11:30 +03:00
parent 1c1a003647
commit 77c7956e55
9 changed files with 334 additions and 0 deletions

3
constants.py Normal file
View File

@@ -0,0 +1,3 @@
FAILED_LOGIN_MESSAGE = 'שם המשתמש ו/או הסיסמה שהזנת שגויים'
SERIES_NAME_YEAR_PATTERN = r'^(.*)\s(\d{4})$'

1
cookie.json Normal file
View File

@@ -0,0 +1 @@
{}

2
credentials.yaml Normal file
View File

@@ -0,0 +1,2 @@
username: elad2911@gmail.com
password: Elad29112000

178
main.py Normal file
View File

@@ -0,0 +1,178 @@
import json
import re
from bs4 import BeautifulSoup
import utils
from sdarot_api import SdarotApi
import requests, time
SDAROT_DOMAIN = "sdarot.buzz"
def get_sdarot_cookies():
return {"Sdarot": requests.get(f"https://{SDAROT_DOMAIN}/index").cookies["Sdarot"]}
def login_to_sdarot_tv(username, password, cookies):
data = {
'username': username,
'password': password,
'submit_login': ''
}
response = requests.post(f'https://{SDAROT_DOMAIN}/login', data=data, cookies=cookies)
if 'שם המשתמש ו/או הסיסמה שהזנת שגויים' in response.text:
return False
return True
def get_series_dictionary(cookies):
data = "srl=1"
series_json = requests.get(f"https://www.{SDAROT_DOMAIN}/ajax/index?srl=1", cookies=cookies, data=data).json()
with open('series.json', 'w') as file:
json.dump(series_json, file)
return series_json
def get_serie_seasons_list(sid, cookies):
with open('series_metadata.json') as file:
series_metadata = json.load(file)
if str(sid) in series_metadata:
print(sid)
return series_metadata[str(sid)]
response = requests.get(f'https://{SDAROT_DOMAIN}/watch/{sid}', cookies=cookies)
serie_html = BeautifulSoup(response.text, 'html.parser')
seasons_ul = serie_html.find('ul', attrs={'id': 'season'})
seasons_li = seasons_ul.select('li[data-season]')
series_metadata[sid] = {}
for season_li in seasons_li:
season_number = season_li.get('data-season')
season_episodes = get_serie_season_episodes(sid, season_number, cookies)
time.sleep(2)
series_metadata[sid][season_number] = season_episodes
with open('series_metadata.json', 'w') as file:
json.dump(series_metadata, file)
return series_metadata[sid]
def get_serie_season_episodes(sid, season, cookies):
response = requests.get(f'https://{SDAROT_DOMAIN}/ajax/watch?episodeList={sid}&season={season}',cookies=cookies)
print(response.status_code)
series_html = BeautifulSoup(response.text, 'html.parser')
print(series_html)
episodes_li = series_html.select('li[data-episode]')
return [episode_li.get('data-episode') for episode_li in episodes_li]
def download_serie_seasons(cookies, serie_name, sid, season, season_dictionary):
if season:
for episode in season_dictionary[season]:
token = create_new_token(cookies, sid, season, episode)
time.sleep(32)
video_url = get_video_url(cookies, token, sid, season, episode)
download_video(cookies, video_url, f"{serie_name} S{season}E{episode}.mp4")
else:
for season in season_dictionary.keys:
for episode in season_dictionary[season]:
token = create_new_token(cookies, sid, season, episode)
time.sleep(32)
video_url = get_video_url(cookies, token, sid, season, episode)
download_video(cookies, video_url, f"{serie_name} S{season}E{episode}.mp4")
def create_new_token(cookies, sid, season, ep):
form_data = {
'preWatch': 'true',
'SID': sid,
'season': season,
'ep': ep
}
headers = {
'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'
}
res = requests.post(f"https://www.{SDAROT_DOMAIN}/ajax/watch", data=form_data, cookies=cookies, headers=headers)
return res.content.decode()
def get_video_url(cookies, token, sid, season, episode):
form_data = {
'watch': 'true',
'token': token,
'serie': sid,
'season': season,
'episode': episode,
'type': 'episode'
}
headers = {
'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'
}
res = requests.post(f"https://{SDAROT_DOMAIN}/ajax/watch", data=form_data, cookies=cookies, headers=headers)
return "https:" + res.json()["watch"]["480"]
def download_video(cookies, video_url, file_path):
response = requests.get(video_url, stream=True, cookies=cookies, timeout=5)
with open(file_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
def check_for_cookies():
with open('cookie.json') as cookie_file:
cookies_json = json.load(cookie_file)
return cookies_json
def check_for_series_dictionary():
with open('series.json') as file:
series = json.load(file)
return series
def main():
sdarot_api = SdarotApi()
sdarot_cookies = check_for_cookies()
if not sdarot_cookies:
username, password = utils.get_credentials()
sdarot_cookies = sdarot_api.get_cookies()
utils.save_cookies(sdarot_cookies)
has_logged_in = sdarot_api.login(username, password)
if not has_logged_in:
print('Failed to log in')
return
series = check_for_series_dictionary()
if not series:
series = sdarot_api.get_series_dictionary()
utils.save_series_dictionary(series)
serie_name = input('Enter a serie you want to download\n')
sid = utils.get_matched_series_ids(series, serie_name)[0]["id"]
seasons_dictionary = get_serie_seasons_list(sid, sdarot_cookies) # stopped after this
season = input('Enter the season you wanna download or press enter for all seasons\n')
download_serie_seasons(sdarot_cookies, serie_name, sid, season, seasons_dictionary)
# token = create_new_token(sdarot_cookies, sid, 1, 1)
# print(token)
# print("Start 30 seconds...")
# time.sleep(32)
# video_url = get_video_url(sdarot_cookies, token, sid, 1, 1)
# download_video(sdarot_cookies, video_url, "example.mp4")
if __name__ == '__main__':
main()

110
sdarot_api.py Normal file
View File

@@ -0,0 +1,110 @@
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)

7
serie.py Normal file
View File

@@ -0,0 +1,7 @@
class Serie:
def __init__(self, sid, name, seasons):
self.sid = sid
self.name = name
self.seasons = seasons
def get_serie_seasons(self):

1
series.json Normal file

File diff suppressed because one or more lines are too long

1
series_metadata.json Normal file
View File

@@ -0,0 +1 @@
{"383": {"1": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"], "2": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55"], "3": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51"], "4": ["1", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47"]}, "695": {"1": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"], "2": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"], "3": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"], "4": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"], "5": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]}}

31
utils.py Normal file
View File

@@ -0,0 +1,31 @@
import json
import yaml
import re
from constants import SERIES_NAME_YEAR_PATTERN
def get_credentials():
with open('credentials.yaml') as file:
sdarot_credentials = yaml.full_load(file)
return sdarot_credentials.get('username'), sdarot_credentials.get('password')
def save_cookies(cookies):
with open('cookie.json') as cookie_file:
json.dump(cookies, cookie_file)
def save_series_dictionary(series_json):
with open('series.json', 'w') as file:
json.dump(series_json, file)
def get_matched_series_ids(series_dictionary, series_name):
year = ''
match = re.match(SERIES_NAME_YEAR_PATTERN, series_name)
if match:
series_name = match.group(1)
year = match.group(2)
return list(filter(lambda obj: (series_name in obj['eng'].lower() or series_name in obj['heb']) and year in obj['year'], series_dictionary))