-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymoviez.py
executable file
·106 lines (88 loc) · 3.76 KB
/
pymoviez.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
#!/usr/bin/env python
import os
# import urllib
import sys
import os.path
import logging
from helper import *
logging.getLogger(__name__)
def create_html(movies):
html_data = "<html><head><title>Movie List</title></head><body>\n"
html_data += "<table>\n" #<tr><th>Title</th><th>Medium</th></tr>
for movie in movies:
if movie['URL']:
title = "<a href='%s' target='_new'>%s</a>" % (movie['URL'], movie['Title'].encode('ascii', 'xmlcharrefreplace'))
else:
title = movie['Title'].encode('ascii', 'xmlcharrefreplace')
if movie['Cover']:
cover = "<img src='%s' alt='%s' style='max-height: 80px; max-width: 80px'/>" % (movie['Cover'], movie['Title'].encode('ascii', 'xmlcharrefreplace'))
else:
cover = ""
if movie['Year']:
if movie['Country']:
year = " (" + movie['Country'] + ", " + str(movie['Year']) + ")"
else:
year = " (" + str(movie['Year']) + ")"
else:
year = ""
if movie['Genre']:
genre = " | " + movie['Genre']
else:
genre = ""
if movie['Medium']:
medium = " | " + movie['Medium']
else:
medium = ""
html_data += "<tr><td>%s</td><td><b>%s%s</b><br />%s%s%s</td></tr>\n" % (cover,
title,
year,
movie['Length'],
genre,
medium)
html_data += "</table></body></html>\n"
logging.info("created html table with %s entries" % len(movies))
return html_data
def create_csv(movies):
csv_data = ""
for movie in movies:
csv_data += "%s;%s;%s;%s;%s;%s;%s;%s;%s\n" % (movie['Title'].encode("utf-8"),
movie['Year'],
movie['Country'],
movie['Length'],
movie['Genre'].encode("utf-8"),
movie['Medium'],
movie['Loaned'].encode("utf-8"),
movie['LoanDate'],
movie['Cover'])
logging.info("created csv data with %s entries" % len(movies))
return csv_data
# main
if __name__ == '__main__':
if len(sys.argv) > 1:
try:
output_dir = sys.argv[2]
except IndexError:
output_dir = "output/"
pass
# load data from zipfile
xml_file_path = process_zip(sys.argv[1], output_dir)
movies_dict = process_xml(xml_file_path)
# render outputs
if movies_dict:
csv_data = create_csv(movies_dict)
output = open(os.path.join(output_dir, "movies.csv"), 'wb')
output.write(csv_data)
output.close()
html_data = create_html(movies_dict)
output = open(os.path.join(output_dir, "index.html"), 'wb')
output.write(html_data)
output.close()
# saving hash
try:
file = open(get_histfile(), 'w')
file.write(hashfile(sys.argv[1]))
file.close()
except IOError:
logging.error("unable to save last hash to %s" % get_histfile())
else:
logging.error("please add a filepath/name to movies.zip export file")