-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScan.py
70 lines (63 loc) · 2.74 KB
/
Scan.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
import requests
from Plexensus_logic import *
import os
import numpy as np
import ast
def api_request(movie: str) -> dict:
"""Using RapidAPI sends a query string and returns the movies dict with relevant info"""
url = "https://movie-database-imdb-alternative.p.rapidapi.com/"
querystring = {"s":movie,"page":"1","r":"json"}
headers = headerrequest()
response = requests.request("GET", url, headers=headers, params=querystring)
return response.text
def insert_movie(addname: str, addyear: int, addposter: str, nomatchresult: int) -> None:
"""SQL inserts the movie into the DB"""
with UseDatabase(dbconfig) as cursor:
_SQL = """insert into moviedata
(name, year, poster, moviematch, nomatch, disliked)
values
(%s, %s, %s, %s, %s, %s)"""
cursor.execute(_SQL, (addname, addyear, addposter, 0, nomatchresult, 0))
def local_scan() -> dict:
"""Scans local movie files and removes junk. Returns the result from imdb"""
moviefile = os.listdir(moviespath)
onboarding_movies = set()
for moviename in moviefile:
#Trim 1080p/720p
if moviename.find('1080p'):
moviename = moviename.replace('1080p', '')
elif moviename.find('720p'):
moviename = moviename.replace('720p', '')
#Remove the year from the end
if '(' and ')' in moviename:
moviename = moviename.strip()
moviename = moviename[:-6]
moviename = moviename.strip()
onboarding_movies.add(moviename)
return onboarding_movies
def load_database() -> list:
"""Returns a list of every movie name in the database"""
with UseDatabase(dbconfig) as cursor:
_SQL = """select name from moviedata"""
cursor.execute(_SQL)
contents = [item[0] for item in cursor.fetchall()]
return contents
def perform_scan() -> None:
"""When activated will scan local movies, check the db for any missing. If missing, the api fills the db"""
local_movies = list(local_scan())
database_movies = load_database()
new_movies = np.setdiff1d(local_movies,database_movies) #Find local movies not appearing in the DB
for i in new_movies:
api_result = api_request(i)
converted_literal = ast.literal_eval(api_result)
state = converted_literal['Response']
insert_movie(i, 1999, "None", 1)
if state == 'True':
searched_movie = converted_literal['Search']
selected_movie = searched_movie[0]
get_title = selected_movie['Title']
get_year = selected_movie['Year']
get_poster = selected_movie['Poster']
insert_movie(get_title, get_year, get_poster, 0)
print('****Database Scan Complete!****')
perform_scan()