Last active
January 4, 2020 15:23
-
-
Save Cyklan/b68b363b4ace4c9dcc74a3e87efd9e2a to your computer and use it in GitHub Desktop.
Twist.moe Download Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twistdl | |
pushbullet.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import json | |
from pathlib import Path | |
from twistdl import TwistDL | |
from pushbullet import Pushbullet | |
def parseCommand(): | |
if (len(sys.argv) <= 1): | |
print("No arguments found") | |
showHelp() | |
elif (sys.argv[1] == "help"): | |
showHelp() | |
elif (sys.argv[1] == "download" and len(sys.argv) == 3 and sys.argv[2] != ""): | |
downloadAnime(sys.argv[2]) | |
sys.exit() | |
elif (sys.argv[1] == "add" and len(sys.argv) == 3 and sys.argv[2] != ""): | |
add(sys.argv[2]) | |
sys.exit() | |
elif (sys.argv[1] == "delete" and len(sys.argv) == 3 and sys.argv[2] != ""): | |
delete(sys.argv[2]) | |
elif (sys.argv[1] == "update"): | |
try: | |
update(sys.argv[2]) | |
except: | |
update() | |
elif (sys.argv[1] == "show"): | |
showAll() | |
else: | |
showHelp() | |
def showHelp(): | |
print("Download some anime\n") | |
print("Usage: python main.py [download|add|delete|update] 'anime name'") | |
print("\tdownload: download all episodes of an anime") | |
print("\tadd: add to refresh list") | |
print("\tdelete: delete from refresh list") | |
print("\tupdate: download all not previously downloaded episodes of added anime") | |
sys.exit() | |
def downloadAnime(anime_title): | |
if not os.path.exists('./anime'): | |
os.mkdir("./anime") | |
anime_to_download = selectAnime(anime_title) | |
download(anime_to_download) | |
# expects anime object | |
def download(anime): | |
print(f"Downloading {anime.title}\n\n-----------------\n") | |
for episode in anime.episodes: | |
file = Path(f'./anime/{anime.slug_name}/{anime.title} - {episode.number}.mp4') | |
print(f'{anime.title} - {episode.number}') | |
file.parent.mkdir(exist_ok=True, parents=True) | |
episode.download(file) | |
def selectAnime(anime_title): | |
client = TwistDL() | |
animes = client.search_animes(title=anime_title) | |
if (len(animes) == 1): | |
return animes[0] | |
elif (len(animes) == 0): | |
print(f"No anime called {anime_title} found") | |
sys.exit() | |
else: | |
for i in range(len(animes)): | |
title = animes[i].title.encode('utf-8') | |
print(f"{i + 1}: {title}") | |
cont = True | |
anime_of_choice = -1 | |
while(cont): | |
try: | |
anime_of_choice = int(input("Select an anime: ")) | |
if (anime_of_choice < 1 or anime_of_choice - 1 >= len(animes)): | |
raise Exception() | |
cont = False | |
except KeyboardInterrupt: | |
sys.exit() | |
except: | |
print("Please pick available anime") | |
return animes[anime_of_choice - 1] | |
def selectAnimeUpdate(anime_title): | |
client = TwistDL() | |
anime = client.get_anime_by("slug_name", anime_title) | |
return anime | |
def add(anime_title): | |
createAnimeFile() | |
anime = selectAnime(anime_title) | |
data = [] | |
with open("./my.anime") as json_file: | |
data = json.load(json_file) | |
for a in data: | |
if (anime.title == a["name"]): | |
print(f"Anime {anime.title} already in list") | |
sys.exit() | |
data.append({"name": anime.title, "ep": anime.episodes[0].number - 1, "slug": anime.slug_name}) | |
with open("./my.anime", "w+") as output_file: | |
json.dump(data, output_file) | |
print(f"Added anime {anime.title} to watch list") | |
def delete(anime_title): | |
createAnimeFile() | |
data = [] | |
with open("./my.anime") as json_file: | |
data = json.load(json_file) | |
if (len(data) == 0): | |
print("No anime in list") | |
sys.exit() | |
anime = selectAnime(anime_title) | |
for i in range(len(data)): | |
if (data[i]["name"] == anime.title): | |
data.pop(i) | |
print(f"Anime {anime.title} removed from watch list") | |
with open("./my.anime", "w") as output_file: | |
json.dump(data, output_file) | |
print(f"Anime {anime.title} is not on watch list") | |
def createAnimeFile(): | |
if (not Path("./my.anime").is_file()): | |
f = open("./my.anime", "w") | |
f.write("[]") | |
f.close() | |
def update(api_token=None): | |
createAnimeFile() | |
if (api_token != None): | |
pb = Pushbullet(api_token) | |
data = [] | |
with open("./my.anime") as json_file: | |
data = json.load(json_file) | |
if (len(data) == 0): | |
print("No anime in list") | |
sys.exit() | |
if not os.path.exists('./anime'): | |
os.mkdir("./anime") | |
for i in range(len(data)): | |
anime = selectAnimeUpdate(data[i]["slug"]) | |
episodes = [] | |
for episode in anime.episodes: | |
if (episode.number > data[i]["ep"]): | |
episodes.append(episode) | |
for episode in episodes: | |
file = Path(f'anime/{anime.slug_name}/{anime.title} - {episode.number}.mp4') | |
print(f"Downloading {anime.title} - {episode.number}") | |
file.parent.mkdir(exist_ok=True, parents=True) | |
episode.download(file) | |
if (api_token != None): | |
pb.push_note("Anime Downloader", f"Downloaded {anime.title}, Episode {episode.number}") | |
if (len(episodes) != 0): | |
data[i]["ep"] = episodes[-1].number | |
with open("./my.anime", "w") as output_file: | |
json.dump(data, output_file) | |
sys.exit() | |
def showAll(): | |
createAnimeFile() | |
data = [] | |
with open("./my.anime") as json_file: | |
data = json.load(json_file) | |
if (len(data) == 0): | |
print("No anime in list") | |
sys.exit() | |
for i in range(len(data)): | |
print(f"{data[i]['name']}") | |
parseCommand() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment