Make the script more robust, pylint fixes

Signed-off-by: Bogomil Vasilev <smirky@smirky.net>
This commit is contained in:
2022-04-29 09:37:15 +03:00
parent 481fa5886a
commit 4bfc45c100
2 changed files with 63 additions and 38 deletions

View File

@@ -8,12 +8,11 @@
""" """
import os import os
import re
import time
import sys import sys
from http.client import IncompleteRead import time
import threading import threading
from pathlib import Path from pathlib import Path
from http.client import IncompleteRead
import progressbar import progressbar
from imdb import IMDb from imdb import IMDb
@@ -52,7 +51,6 @@ class MovieList:
self.src = src self.src = src
self.dst = Path(dst) if dst else Path(os.path.dirname(sys.argv[0])) / 'index.html' self.dst = Path(dst) if dst else Path(os.path.dirname(sys.argv[0])) / 'index.html'
self.movie_list = [] self.movie_list = []
self.status_list = []
self.threads = [] self.threads = []
self.read_prev_output() self.read_prev_output()
self.html_table = None self.html_table = None
@@ -65,32 +63,48 @@ class MovieList:
with open(os.devnull, 'wb') as sys.stdout: with open(os.devnull, 'wb') as sys.stdout:
while True: while True:
try: try:
query = imdb.search_movie(arg) query = imdb.search_movie(f'{arg["title"]} {arg["year"]}')
break break
except IncompleteRead: except IncompleteRead:
pass pass
sys.stdout = save_stdout sys.stdout = save_stdout
movie = query[0] movie = None
imdb.update(movie) for entry in query:
movie['ID'] = query[0].movieID has_minimum_keys = True
for key in ['kind', 'year', 'title']:
if key not in entry.keys():
has_minimum_keys = False
if not has_minimum_keys:
continue
# Try to eliminate episode results
if [i for i in entry.keys() if 'episode' in i.lower()] or \
'episode' in entry['title'].lower():
continue
if entry['kind'].lower() == arg['kind'].lower():
movie = entry
break
if not movie:
movie = {
'title': arg['title'],
'kind': arg['kind'],
'year': arg['year'],
'dummy': None
}
if 'genres' not in movie.keys(): if 'genres' not in movie.keys():
movie['genres'] = ['N/A'] movie['genres'] = ['N/A']
if 'rating' not in movie.keys(): if 'rating' not in movie.keys():
movie['rating'] = 'N/A' movie['rating'] = 'N/A'
url = f'http://www.imdb.com/title/tt{movie["ID"]}' html_title_td = movie['title'] if 'dummy' in movie.keys() else \
title = movie['title'] f'<a href="https://www.imdb.com/title/tt{movie.movieID}" target="_blank">{movie["title"]}</a>'
year = movie['year']
rating = movie['rating']
genres = ', '.join(movie['genres'])
status = self.status_list[index]
self.html_table[index] = ( self.html_table[index] = (
f'\n{" "*8}<tr><td>{index + 1}</td>' f'\n{" "*8}<tr><td>{index + 1}</td>'
f'<td><p hidden>{title}</p><a href="{url}" target="_blank">{title}</a></td>' f'<td><p hidden>{movie["title"]}</p>{html_title_td}</td>'
f'<td>{year}</td><td align="center">{rating}</td>' f'<td>{movie["year"]}</td><td align="center">{movie["rating"]}</td>'
f'<td>{genres}</td>' f'<td>{", ".join(movie["genres"])}</td>'
f'<td align="center">{status}</td></tr>' f'<td align="center">{arg["status"]}</td></tr>'
) )
def gen(self): def gen(self):
@@ -103,28 +117,34 @@ class MovieList:
sys.stderr.write(f'error: input does not exist - {self.src}\n') sys.stderr.write(f'error: input does not exist - {self.src}\n')
return False return False
self.movie_list = {}
# Open the movie list & split the columns # Open the movie list & split the columns
with open(self.src, 'r') as fp_handle: with open(self.src, 'r', encoding='utf-8') as fp_handle:
tmp_names = tmp_status = fp_handle.read() mlist_raw = fp_handle.read()
tmp_names = re.sub(r'\).*', ')', tmp_names) for raw_line in mlist_raw.splitlines():
tmp_status = re.sub(r'\]|.*\[', '', tmp_status) self.movie_list.update({
self.movie_list = tmp_names.splitlines() len(self.movie_list): {
self.status_list = tmp_status.splitlines() 'title': raw_line.split('(', 1)[0].strip(),
'kind': raw_line[raw_line.find('<')+1:raw_line.rfind('>')+1].strip('<>') or 'movie',
'year': raw_line[raw_line.find('(')+1:raw_line.find(')')],
'status': raw_line[raw_line.find('[')+1:raw_line.find(']')],
}
})
self.html_table = [None] * len(self.movie_list) self.html_table = [None] * len(self.movie_list)
# Progress bar. Enough said # Progress bar. Enough said
pbar = progressbar.ProgressBar(max_value=len(self.movie_list)) pbar = progressbar.ProgressBar(max_value=len(self.movie_list))
for idx, movie in self.movie_list.items():
for idx, val in enumerate(self.movie_list): match = [html_row for html_row in self.prev_html if movie['title'] in html_row]
match = [line for line in self.prev_html if re.sub(r' \(.*\)', '', val) in line]
if match: if match:
# Update movies as DONE in case of change # Update movies as DONE in case of change
match = match[0].replace('*', self.status_list[idx]) match = match[0].replace('*', movie['status'])
# Directly insert the current HTML line from the older output # Directly insert the current HTML line from the older output
self.html_table[idx] = f'\n{" "*8}<tr><td>{idx + 1}</td>{match[match.find("</td>") + 5:]}' self.html_table[idx] = \
f'\n{" "*8}<tr><td>{idx + 1}</td>{match[match.find("</td>") + 5:]}'
pbar.update(idx + 1) pbar.update(idx + 1)
else: else:
thread = threading.Thread(target=self._worker, args=(val, idx)) thread = threading.Thread(target=self._worker, args=(movie, idx))
self.threads.append(thread) self.threads.append(thread)
thread.start() thread.start()
pbar.update(idx+1) pbar.update(idx+1)
@@ -149,6 +169,8 @@ class MovieList:
fp_handle.write(self.html.encode('utf8')) fp_handle.write(self.html.encode('utf8'))
def read_prev_output(self): def read_prev_output(self):
""" Import a previous HTML table """
if self.dst.exists():
with open(self.dst, 'rb') as fp_handle: with open(self.dst, 'rb') as fp_handle:
self.prev_html = fp_handle.read().decode('utf8').split('\n') self.prev_html = fp_handle.read().decode('utf8').split('\n')
@@ -158,7 +180,7 @@ def main():
src = dst = None src = dst = None
if len(sys.argv) > 3: if len(sys.argv) > 3:
sys.stderr.write(f'error: max 2 variables, {len(sys.argv)-1} given!\n') sys.stderr.write(f'error: max 2 variables, {len(sys.argv)-1} given!\n')
exit(1) sys.exit(1)
if len(sys.argv) > 1: if len(sys.argv) > 1:
src = sys.argv[1] src = sys.argv[1]

View File

@@ -290,7 +290,7 @@ Spider-Man: Homecoming (2017) [DONE]
Rememory (2017) [DONE] Rememory (2017) [DONE]
Wristcutters: A Love Story (2006) [DONE] Wristcutters: A Love Story (2006) [DONE]
Bohemian Rhapsody (2018) [DONE] Bohemian Rhapsody (2018) [DONE]
Killer's Bodyguard (2017) [DONE] The Hitmans's Bodyguard (2017) [DONE]
Danny Collins (2015) [DONE] Danny Collins (2015) [DONE]
Annabelle: Creation (2017) [DONE] Annabelle: Creation (2017) [DONE]
Secrets of State (2008) [*] Secrets of State (2008) [*]
@@ -322,7 +322,7 @@ Full Metal Jacket (1987) [*]
Woman at War (2018) [*] Woman at War (2018) [*]
Roman J. Israel, Esq. (2017) [DONE] Roman J. Israel, Esq. (2017) [DONE]
Venom (2018) [DONE] Venom (2018) [DONE]
Moby Dick (1998) [DONE] Moby Dick (1998) <TV Mini Series> [DONE]
The Help (2011) [DONE] The Help (2011) [DONE]
Hidden Figures (2016) [DONE] Hidden Figures (2016) [DONE]
The Guilty (2018) [DONE] The Guilty (2018) [DONE]
@@ -357,10 +357,10 @@ The Collini Case (2019) [DONE]
The Traitor (2019) [DONE] The Traitor (2019) [DONE]
Motherless Brooklyn (2019) [DONE] Motherless Brooklyn (2019) [DONE]
The Banker (2020) [DONE] The Banker (2020) [DONE]
The Gentlemen (2020) [DONE] The Gentlemen (2019) [DONE]
The Platform (2019) [DONE] The Platform (2019) [DONE]
Radioactive (2019) [DONE] Radioactive (2019) [DONE]
Radium Girls (2020) [DONE] Radium Girls (2018) [DONE]
The Stanford Prison Experiment (2015) [DONE] The Stanford Prison Experiment (2015) [DONE]
Red Dragon (2002) [DONE] Red Dragon (2002) [DONE]
The Silence of the Lambs (1991) [DONE] The Silence of the Lambs (1991) [DONE]
@@ -384,7 +384,7 @@ Super Lopez (2018) [DONE]
Midnight Special (2016) [DONE] Midnight Special (2016) [DONE]
The Father (2020) [DONE] The Father (2020) [DONE]
Another Round (2020) [DONE] Another Round (2020) [DONE]
Promising Young Woman (2021) [DONE] Promising Young Woman (2020) [DONE]
La Daronne (2020) [DONE] La Daronne (2020) [DONE]
Tangerines (2013) [DONE] Tangerines (2013) [DONE]
Bridge of Spies (2015) [DONE] Bridge of Spies (2015) [DONE]
@@ -408,3 +408,6 @@ Finch (2021) [DONE]
Last Night in Soho (2021) [DONE] Last Night in Soho (2021) [DONE]
Deepwater Horizon (2016) [DONE] Deepwater Horizon (2016) [DONE]
Boss Level (2020) [DONE] Boss Level (2020) [DONE]
Boite Noire (2021) [DONE]
Fresh (2022) [DONE]
Death on the Nile (2022) [DONE]