-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetflix_items.py
130 lines (106 loc) · 4.78 KB
/
netflix_items.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os, logging, csv, json, sys
from pathlib import Path
from movie import Movie
from show import Show
try:
from dotenv import load_dotenv
import requests
except ModuleNotFoundError:
sys.exit("please run: pip install -r requirements.txt")
basepath = Path()
basedir = str(basepath.cwd())
envars = basepath.cwd() / "SECRETS.env"
load_dotenv(envars)
class NetflixItems:
_baseurl = "https://api.trakt.tv"
_final_request = {"movies": [], "episodes": []}
_duplicates = {"movies": {}, "episodes": {}}
_headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": "Trakt importer",
"Connection": "Keep-Alive",
"trakt-api-version": "2",
"trakt-api-key": os.getenv("TRAKT_API_KEY"),
"Authorization": "Bearer " + os.getenv("TOKEN"),
}
def __init__(self):
self.duplicates = {"movies": {}, "episodes": {}}
self.items = {}
def load_csv(self, fileName):
with open(fileName) as f:
reader = csv.reader(f, delimiter=",")
next(reader) # ignore header
logging.debug(f"loaded csv file {fileName}")
self.items = dict(reader)
def isSeries(self, text) -> bool:
return text.find(":") > 0
def search_items_create_request(self):
"""
Search movies or tv shows and find it's trakt id
"""
global _final_request
m = {}
for item_to_search in self.items:
if self.isSeries(item_to_search):
pos1 = item_to_search.find(":")
pos2 = item_to_search[item_to_search.find(":") + 2 :].find(":")
show_title = item_to_search[:pos1]
title = item_to_search[pos1 + pos2 + 2 + 2 :]
item_watched = Show(show_title, title, self.items[item_to_search])
else:
title = item_to_search
item_watched = Movie(item_to_search, self.items[item_to_search])
try:
response = requests.get(
self._baseurl + "/search/" + item_watched.type + "?query=" + item_watched.title.replace(" ", "%20"),
headers=self._headers,
)
except Exception as e:
logging.error("Error in get", e)
if response:
json_data = json.loads(response.text)
i = 0
item_found_prev = {item_watched.type: {}}
for item_found in json_data:
if item_found[item_watched.type]["title"].lower() == title.lower() and (
item_watched.type == "movie" or item_found["show"]["title"] == show_title
):
m = {
"watched_at": item_watched.watched_at.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
"title": item_found[item_watched.type]["title"],
"ids": {
"trakt": item_found[item_watched.type]["ids"]["trakt"],
"imdb": item_found[item_watched.type]["ids"]["imdb"],
},
}
if (
i != 0
and item_found[item_watched.type]["title"].lower()
== item_found_prev[item_watched.type]["title"].lower()
):
self.addDuplicate(item_found, title, item_watched)
item_found_prev = item_found
i += 1
else:
if m != {} and i == 1:
self._final_request[item_watched.type2].append(m)
def addDuplicate(self, item_found, title, item_watched):
global _duplicates
item = {
"title": item_found[item_watched.type]["title"],
"year": str(item_found[item_watched.type]["year"]),
"Imdb_id": (item_found[item_watched.type]["ids"]["imdb"] if item_found[item_watched.type]["ids"]["imdb"] else "null"),
"trakt": item_found[item_watched.type]["ids"]["trakt"],
"URL": " https://trakt.tv/movies/" + str(item_found[item_watched.type]["ids"]["trakt"]),
"watched_at": item_watched.watched_at.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
"validated": "False",
}
if title in self._duplicates[item_watched.type2]:
self._duplicates[item_watched.type2][title].append(item)
else:
self._duplicates[item_watched.type2][title] = []
self._duplicates[item_watched.type2][title].append(item)
def appendValidDuplicates(self, validatedDuplicates):
for duplicate in validatedDuplicates:
self._final_request["movies"].append(duplicate)