Files
scripts/movie_list/index.py

106 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python
from imdb import IMDb
import os, re, threading, time, sys, subprocess
from time import gmtime, strftime
path = os.path.dirname(sys.argv[0])
ia = IMDb()
movie_list = []
status_list = []
threads = []
query = []
html = """<html>
<head>
<title>My Movie List</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="../jquery-3.1.0.min.js"></script>
<script src="jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('#sortable').DataTable({
"pageLength": -1,
"bPaginate": false
});
});
</script>
</head>
<body>
<header>
<div class="scroll-indicator" />
</header>
<base target="_parent" />
<table id="sortable" class="sortable">
<thead>
<tr><th> Index </th><th> Title </th><th> Year </th><th> IMDb Rating </th><th> Genre </th><th> Status </th></tr>
</thead>
<tbody>"""
# Open the movie list & split the columns
file_ = open(path + '/movie_list', 'r')
tmp_names = tmp_status = file_.read()
file_.close()
tmp_names = re.sub(r'\).*', ')', tmp_names)
tmp_status = re.sub(r'\]|.*\[', '', tmp_status)
movie_list = tmp_names.splitlines()
status_list = tmp_status.splitlines()
# Scan IMDb for a given movie and append it to the html
# This collects rating, genres, official name and a hyperlink
def worker(arg, index):
save_stdout = sys.stdout
sys.stdout = open('trash', 'w')
query = ia.search_movie(arg)
sys.stdout = save_stdout
movie = query[0]
ia.update(movie)
movie['ID'] = query[0].movieID
if 'genres' not in movie.keys():
movie['genres'] = 'N/A'
if 'rating' not in movie.keys():
movie['rating'] = 'N/A'
global html
html = (html +
'\n\t<tr><td>' + str(index+1) + '</td><td><p hidden>' + movie['title'] + '</p>' +
'<a href="http://www.imdb.com/title/tt' + movie['ID'] + '" target="_blank">' +
str(movie['title']) +
'</a></td><td>' +
str(movie['year']) +
'</td><td align="center">' +
str(movie['rating']) +
'</td><td>' +
re.sub(r'\[|\]|\'', '', str(movie['genres'])) +
'</td><td align="center">' +
str(status_list[index]) +
'</td></tr>')
# Progress bar. Enough said
import progressbar
bar = progressbar.ProgressBar(max_value=len(movie_list))
for idx, val in enumerate(movie_list):
t = threading.Thread(target=worker, args=(val, idx,))
threads.append(t)
t.start()
bar.update(idx+1)
time.sleep(0.2)
if len(threads)%16 == 0:
time.sleep(6)
for x in threads:
x.join()
# Just a fancy scrollbar for the html
scroll = """
<script type="text/javascript" src="scroll-indicator.js"></script>
"""
html += ('\n\t</tbody>\n</table>\n' +
'\nGenerated on: ' + strftime('%Y-%m-%d %H:%M:%S', gmtime()) +
' by ' + sys.argv[0] + scroll + '</body>\n</html>')
file_ = open(path + '/index.html', 'wb')
file_.write(html.encode('utf8'))
file_.close()