add elad files
This commit is contained in:
178
main.py
Normal file
178
main.py
Normal 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()
|
||||
Reference in New Issue
Block a user