From 4bfc45c100a4bcb7246243eff597211713681694 Mon Sep 17 00:00:00 2001 From: Bogomil Vasilev Date: Fri, 29 Apr 2022 09:37:15 +0300 Subject: [PATCH] Make the script more robust, pylint fixes Signed-off-by: Bogomil Vasilev --- movie_list/index.py | 88 +++++++++++++++++++++++++++---------------- movie_list/movie_list | 13 ++++--- 2 files changed, 63 insertions(+), 38 deletions(-) diff --git a/movie_list/index.py b/movie_list/index.py index c3d14f1..002991d 100755 --- a/movie_list/index.py +++ b/movie_list/index.py @@ -8,12 +8,11 @@ """ import os -import re -import time import sys -from http.client import IncompleteRead +import time import threading from pathlib import Path +from http.client import IncompleteRead import progressbar from imdb import IMDb @@ -52,7 +51,6 @@ class MovieList: self.src = src self.dst = Path(dst) if dst else Path(os.path.dirname(sys.argv[0])) / 'index.html' self.movie_list = [] - self.status_list = [] self.threads = [] self.read_prev_output() self.html_table = None @@ -65,32 +63,48 @@ class MovieList: with open(os.devnull, 'wb') as sys.stdout: while True: try: - query = imdb.search_movie(arg) + query = imdb.search_movie(f'{arg["title"]} {arg["year"]}') break except IncompleteRead: pass sys.stdout = save_stdout - movie = query[0] - imdb.update(movie) - movie['ID'] = query[0].movieID + movie = None + for entry in query: + 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(): movie['genres'] = ['N/A'] if 'rating' not in movie.keys(): movie['rating'] = 'N/A' - url = f'http://www.imdb.com/title/tt{movie["ID"]}' - title = movie['title'] - year = movie['year'] - rating = movie['rating'] - genres = ', '.join(movie['genres']) - status = self.status_list[index] + html_title_td = movie['title'] if 'dummy' in movie.keys() else \ + f'{movie["title"]}' self.html_table[index] = ( f'\n{" "*8}{index + 1}' - f'{title}' - f'{year}{rating}' - f'{genres}' - f'{status}' + f'{html_title_td}' + f'{movie["year"]}{movie["rating"]}' + f'{", ".join(movie["genres"])}' + f'{arg["status"]}' ) def gen(self): @@ -103,28 +117,34 @@ class MovieList: sys.stderr.write(f'error: input does not exist - {self.src}\n') return False + self.movie_list = {} # Open the movie list & split the columns - with open(self.src, 'r') as fp_handle: - tmp_names = tmp_status = fp_handle.read() - tmp_names = re.sub(r'\).*', ')', tmp_names) - tmp_status = re.sub(r'\]|.*\[', '', tmp_status) - self.movie_list = tmp_names.splitlines() - self.status_list = tmp_status.splitlines() + with open(self.src, 'r', encoding='utf-8') as fp_handle: + mlist_raw = fp_handle.read() + for raw_line in mlist_raw.splitlines(): + self.movie_list.update({ + len(self.movie_list): { + '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) # Progress bar. Enough said pbar = progressbar.ProgressBar(max_value=len(self.movie_list)) - - for idx, val in enumerate(self.movie_list): - match = [line for line in self.prev_html if re.sub(r' \(.*\)', '', val) in line] + for idx, movie in self.movie_list.items(): + match = [html_row for html_row in self.prev_html if movie['title'] in html_row] if match: # 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 - self.html_table[idx] = f'\n{" "*8}{idx + 1}{match[match.find("") + 5:]}' + self.html_table[idx] = \ + f'\n{" "*8}{idx + 1}{match[match.find("") + 5:]}' pbar.update(idx + 1) else: - thread = threading.Thread(target=self._worker, args=(val, idx)) + thread = threading.Thread(target=self._worker, args=(movie, idx)) self.threads.append(thread) thread.start() pbar.update(idx+1) @@ -149,8 +169,10 @@ class MovieList: fp_handle.write(self.html.encode('utf8')) def read_prev_output(self): - with open(self.dst, 'rb') as fp_handle: - self.prev_html = fp_handle.read().decode('utf8').split('\n') + """ Import a previous HTML table """ + if self.dst.exists(): + with open(self.dst, 'rb') as fp_handle: + self.prev_html = fp_handle.read().decode('utf8').split('\n') def main(): @@ -158,7 +180,7 @@ def main(): src = dst = None if len(sys.argv) > 3: sys.stderr.write(f'error: max 2 variables, {len(sys.argv)-1} given!\n') - exit(1) + sys.exit(1) if len(sys.argv) > 1: src = sys.argv[1] diff --git a/movie_list/movie_list b/movie_list/movie_list index 4c47c7b..158324d 100644 --- a/movie_list/movie_list +++ b/movie_list/movie_list @@ -290,7 +290,7 @@ Spider-Man: Homecoming (2017) [DONE] Rememory (2017) [DONE] Wristcutters: A Love Story (2006) [DONE] Bohemian Rhapsody (2018) [DONE] -Killer's Bodyguard (2017) [DONE] +The Hitmans's Bodyguard (2017) [DONE] Danny Collins (2015) [DONE] Annabelle: Creation (2017) [DONE] Secrets of State (2008) [*] @@ -322,7 +322,7 @@ Full Metal Jacket (1987) [*] Woman at War (2018) [*] Roman J. Israel, Esq. (2017) [DONE] Venom (2018) [DONE] -Moby Dick (1998) [DONE] +Moby Dick (1998) [DONE] The Help (2011) [DONE] Hidden Figures (2016) [DONE] The Guilty (2018) [DONE] @@ -357,10 +357,10 @@ The Collini Case (2019) [DONE] The Traitor (2019) [DONE] Motherless Brooklyn (2019) [DONE] The Banker (2020) [DONE] -The Gentlemen (2020) [DONE] +The Gentlemen (2019) [DONE] The Platform (2019) [DONE] Radioactive (2019) [DONE] -Radium Girls (2020) [DONE] +Radium Girls (2018) [DONE] The Stanford Prison Experiment (2015) [DONE] Red Dragon (2002) [DONE] The Silence of the Lambs (1991) [DONE] @@ -384,7 +384,7 @@ Super Lopez (2018) [DONE] Midnight Special (2016) [DONE] The Father (2020) [DONE] Another Round (2020) [DONE] -Promising Young Woman (2021) [DONE] +Promising Young Woman (2020) [DONE] La Daronne (2020) [DONE] Tangerines (2013) [DONE] Bridge of Spies (2015) [DONE] @@ -408,3 +408,6 @@ Finch (2021) [DONE] Last Night in Soho (2021) [DONE] Deepwater Horizon (2016) [DONE] Boss Level (2020) [DONE] +Boite Noire (2021) [DONE] +Fresh (2022) [DONE] +Death on the Nile (2022) [DONE]