-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollect_info.py
154 lines (112 loc) · 4.59 KB
/
collect_info.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import json
import progressbar
import urllib.request
from imdbpie import Imdb
from guessit import guessit
from openpyxl import Workbook
from openpyxl.styles import Alignment
from openpyxl.worksheet.table import Table, TableStyleInfo
def get_movie_names(adir="."):
exten = ('.mp4', '.avi', '.mkv', '.mpg', '.mov', '.vob', '.3gp', '.m2ts',
'.3g2', '.flv', '.h264', '.mpeg', '.m4v', '.rm', '.wmv', '.swf')
# Yields valid movie names
for dirpath, dirnames, files in os.walk(adir):
for name in files:
if name.lower().endswith(exten) and not name.lower().startswith("sample"):
yield ((guessit(name)).get('title'), dirpath)
def fetch_movie_info(name):
# API key
apikey = "78d08b59"
try:
# Gets movie info
imdb = Imdb()
imdb_search = imdb.search_for_title(name)
movie_id = imdb_search[0]["imdb_id"]
url = "http://www.omdbapi.com/?i=" + movie_id + "&apikey=" + apikey
response = urllib.request.urlopen(url).read()
jsonvalues = json.loads(response)
if jsonvalues['Response'] == 'True':
title = jsonvalues["Title"]
rating = jsonvalues["imdbRating"]
genre = jsonvalues["Genre"]
year = jsonvalues["Year"]
actors = jsonvalues["Actors"]
director = jsonvalues["Director"]
runtime = jsonvalues["Runtime"]
# Gathers movie data in a list
info_list = [movie_id, title, float(rating), genre, int(year),
actors, director, runtime]
return (True, info_list)
else:
return (False, name)
except Exception:
return (False, name)
def create_collection():
# Deletes existed .xlsx file
if os.path.isfile("movie_info.xlsx"):
os.remove("movie_info.xlsx")
# Creates .xlsx file
wb = Workbook()
ws = wb.active
# Adds Column headings
ws.append(['Movie id', 'Movie name', 'Imdb rating', 'Genre',
'Year', 'Actors', 'Director', 'Running time'])
# Progressbar to see the progress
bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
print()
print("Reading your local directory for movies.\n")
print("Fetching movie info....... Please wait!\n")
end_row_no = 1
# Set to add unique values only to the movie_names collection
movie_names = set()
# writes data
for name, dirpath in get_movie_names():
if name not in movie_names:
abool, info_list = fetch_movie_info(name)
if abool is True:
movie_names.add(name)
length = len(movie_names)
bar.update(length)
end_row_no += 1
# Appends row
ws.append(info_list)
# Hyperlinks movie names
ws.cell(row=end_row_no, column=2).value = '=HYPERLINK("{}", "{}")'.format(
dirpath, name)
bar.finish()
print()
print(f"Found : {str(length)} movies in your collection.\n")
print("Done!!!\n")
# Excel Design
# Freezes the first row
ws.freeze_panes = 'A2'
# Adjusts column width based on its cell's content
# excluding column two (because of hyperlink formulas)
skip_col_no = 2
for column_cells in ws.columns:
if skip_col_no >= 0:
skip_col_no -= 1
if skip_col_no == 0:
continue
# Adjusts column width acc. value of max length (col2 excluded)
value_length = max(len(str(cell.value) or "") for cell in column_cells)
ws.column_dimensions[column_cells[0].column].width = value_length + 6
# Center-aligns contents of the cells (col2 excluded)
for cell in column_cells:
cell.alignment = Alignment(horizontal='center')
# Adjusts 2nd col
col2_val_length = max(len(nam) for nam in movie_names)
ws.column_dimensions['B'].width = col2_val_length
# Center-aligns col2
ws['B1'].alignment = Alignment(horizontal='center')
# Table
tab = Table(displayName="movie_info", ref="A1:H"+str(end_row_no))
# Adds a default style with striped rows and banded columns
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=True)
tab.tableStyleInfo = style
ws.add_table(tab)
wb.save("movie_info.xlsx")
if __name__ == "__main__":
create_collection()