40
|
1 import urllib.request
|
|
2 import json
|
|
3 import sys
|
30
|
4
|
32
|
5 # Initialize variables
|
40
|
6 # https://github.com/xbmc/metadata.themoviedb.org.python/blob/master/python/lib/tmdbscraper/tmdbapi.py#L36
|
|
7 key = "f090bb54758cabf231fb605d3e3e0468"
|
32
|
8
|
|
9 # Ask for source
|
40
|
10 if not 2 >= len(sys.argv):
|
|
11 source = sys.argv[1]
|
|
12 if 'source' not in locals() or 'source' not in globals():
|
|
13 source = input(
|
|
14 "Which website would you like to pull titles from? [tmdb, mal]: ")
|
32
|
15 while source not in ["tmdb", "mal"]:
|
|
16 print("Not a valid source! Exiting.")
|
|
17 sys.exit()
|
|
18
|
|
19 # Ask for ID
|
40
|
20 if not 3 >= len(sys.argv):
|
|
21 source = sys.argv[2]
|
|
22 if 'id' not in locals() or 'id' not in globals():
|
32
|
23 id = input("What is the ID for your show?: ")
|
30
|
24 try:
|
|
25 temp = int(id)
|
40
|
26 except Exception:
|
32
|
27 print("Not a valid ID! Exiting.")
|
30
|
28 sys.exit()
|
|
29
|
32
|
30 # Scrapers
|
|
31 if source == 'tmdb':
|
40
|
32 # required because api is... odd
|
|
33 season = input("Which season do you want?: ")
|
32
|
34 try:
|
|
35 temp = int(season)
|
40
|
36 except Exception:
|
32
|
37 print("Not a valid season! Exiting.")
|
|
38 sys.exit()
|
|
39 data = json.loads(urllib.request.urlopen(f'https://api.themoviedb.org/3/tv/{str(id)}?api_key={key}').read().decode())
|
|
40 amount = data["number_of_episodes"]
|
31
|
41 f = open("list.txt", "w", encoding="utf-8")
|
|
42 f.write("")
|
|
43 f.close()
|
32
|
44 count = 1
|
40
|
45 for i in range(amount): # this may count as spamming the api but i don't care lol
|
|
46 with urllib.request.urlopen(
|
|
47 f'https://api.themoviedb.org/3/tv/{str(id)}/season/{season}/episode/{count}?api_key={key}') as url:
|
32
|
48 data = json.loads(url.read().decode())
|
|
49 f = open("list.txt", "a", encoding="utf-8")
|
40
|
50 f.write(data["name"].replace("?", "?").replace(
|
|
51 ":", "꞉").replace('"', "“").split("/")[0].rstrip() + "\n")
|
32
|
52 count += 1
|
|
53 f.close()
|
|
54 if source == 'mal':
|
|
55 with urllib.request.urlopen(f"https://api.jikan.moe/v3/anime/{str(id)}/episodes") as url:
|
|
56 data = json.loads(url.read().decode())
|
|
57 count = 0
|
|
58 f = open("list.txt", "w", encoding="utf-8")
|
|
59 f.write("")
|
30
|
60 f.close()
|
32
|
61 for i in range(len(data["episodes"])):
|
|
62 f = open("list.txt", "a", encoding="utf-8")
|
40
|
63 f.write(data["episodes"][count]["title"].replace("?", "?").replace(
|
|
64 ":", "꞉").replace('"', "“").split("/")[0].rstrip() + "\n")
|
32
|
65 count += 1
|
|
66 f.close()
|