Skip to content
Newer
Older
100644 170 lines (126 sloc) 4.55 KB
1e18452 @theatrus Lots of license additions
authored Jul 14, 2008
1 #!/usr/bin/env python
2 # Copyright (c) 2008 Yann Ramin
3 # This file is part of quickmovie.
4 #
5 # quickmovie is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # quickmovie is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with quickmovie. If not, see <http://www.gnu.org/licenses/>.
17
eb6c06e @theatrus quickmovie round 1
authored May 31, 2008
18 import sys
19 sys.path.append('.')
20
21 import os
22
23 import cherrypy
24
25 from quickmovie.model import meta
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
26 from quickmovie.model import Movie, Plot
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
27 import quickmovie.model
28
29 import sqlalchemy as sa
30
31 import quickmovie.controller.main
32 import imdb
33 import re
34
35 import urllib2
36
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 14, 2008
37 def fetch_cover(inurl, outfile):
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 15, 2008
38 print " Trying to fetch",inurl
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
39 filein = urllib2.urlopen(inurl)
40 fileout = open(outfile, 'w')
41 while True:
42 try:
43 bytes = filein.read(1024)
44 fileout.write(bytes)
45 except IOError, (errno, strerror):
46 print "IOERROR!"
47
48 if bytes == "":
49 break
50 filein.close()
51 fileout.close()
52 return outfile
53
54
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
55
56 def store_plot(rawplot, movie):
61fb350 @theatrus Small change in how plots are fetched
authored Aug 22, 2009
57
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
58 plot = u"".join(rawplot)
59 rplot = re.search('(.*)::(.*)', plot)
61fb350 @theatrus Small change in how plots are fetched
authored Aug 23, 2009
60 plot = rplot.group(1)
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
61
62 found = False
63
64 for existing_plot in movie.plots:
65 if plot == existing_plot.plot:
66 found = True
67
68 if found == False:
69 print " Fetched a plot"
70 po = Plot()
71
72 po.plot = plot
73 po.pick = False
74 movie.plots.append(po)
b40fdc1 @theatrus Genre sorting by Michael Fuller <mike@fuller.id.au>
authored Oct 19, 2010
75 meta.Session.add(po)
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
76
77
78
79 def store_plots(iamovie, movie):
80 if iamovie.has_key('plot'):
81 for rawplot in iamovie['plot']:
82 store_plot(rawplot, movie)
83 else:
84 store_plot(u"No plots found.", movie)
85
86
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
87 def main():
88 current_dir = os.path.dirname(os.path.abspath(__file__))
89
90
91 engine = sa.create_engine('sqlite:///quickmovie.sqlite')
92 quickmovie.model.init(engine)
a719c97 @theatrus Update plot fetching
authored Aug 15, 2008
93 print "Running quickmovie scanner"
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
94
95 for dirpath, dirnames, filenames in os.walk(sys.argv[1]):
a719c97 @theatrus Update plot fetching
authored Aug 16, 2008
96
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
97 for file in filenames:
98 file = file.lower()
a719c97 @theatrus Update plot fetching
authored Aug 16, 2008
99 if file[-3:] != 'iso' and file[-3:] != 'avi':
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
100 continue
101
102 rawfile = unicode(file)
a719c97 @theatrus Update plot fetching
authored Aug 16, 2008
103 print rawfile
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
104 m = None
105
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
106 r = meta.Session.query(Movie).filter_by(filename = rawfile).all()
107 if len(r) > 0:
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
108 print "Already in DB, going to update",rawfile
109 m = r[0]
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
110
111 print rawfile
112
1ac1768 @theatrus Fix indent issue
authored Oct 19, 2010
113 file = file[:-4] # Crude extension strip
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
114 file = file.replace("_", " ")
115
116 ia = imdb.IMDb()
117 iares = ia.search_movie(file)
1ac1768 @theatrus Fix indent issue
authored Oct 20, 2010
118 if len(iares) == 0:
119 continue
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
120 iamovie = iares[0]
121 ia.update(iamovie)
a719c97 @theatrus Update plot fetching
authored Aug 16, 2008
122 ia.update(iamovie, 'plot')
4a92935 @theatrus Make a link?
authored May 31, 2008
123 # Try to fix broken consoles without unicode - force to ascii
124
125 print file, u'->', iamovie['title'].encode('ascii', 'replace')
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
126
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
127 try:
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
128 if m == None:
129 m = Movie() # Make a new movie
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
130 m.name = iamovie['title']
131 m.filename = rawfile
132 m.imdb_id = unicode(iamovie.getID())
133 m.year = int(iamovie['year'])
134
135 # Fetch cover
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
136 cover_url = os.path.sep + 'covers' + os.path.sep + m.imdb_id + '.jpg'
137
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
138 if not iamovie.has_key('cover url'):
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
139 print " Couldn't fetch cover art"
c742072 @theatrus Make nocover a unicode string
authored Jul 14, 2008
140 m.imageurl = u'nocover.jpg'
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
141 else:
142 fetch_cover(iamovie['cover url'], 'htdocs' + os.path.sep + cover_url)
143 m.imageurl = cover_url
144
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
145 m.genres = iamovie['genres'][0]
146 m.length = iamovie['runtimes'][0]
147 if iamovie.has_key('taglines'):
148 ia.update(iamovie, 'taglines')
149 m.taglines = u"".join(iamovie['taglines'])
150 m.rating = float(iamovie['rating'])
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
151
152
153
51b14b9 @theatrus Multiple plot support
authored Aug 16, 2008
154 # Look for existing plots
155 store_plots(iamovie, m)
156
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
157 try:
b40fdc1 @theatrus Genre sorting by Michael Fuller <mike@fuller.id.au>
authored Oct 20, 2010
158 meta.Session.add(m)
0a35533 @theatrus Allow the fetcher to update movie entries
authored Aug 16, 2008
159 except:
160 pass
161 finally:
162 meta.Session.commit()
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
163
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
164 except Exception,e:
5c390aa @theatrus Slight refactoring in imdbfetch - planning to integrate into the web …
authored Jul 15, 2008
165 print "Exception while processing IMDB request",e
eb6c06e @theatrus quickmovie round 1
authored Jun 1, 2008
166
167
168 if __name__ == '__main__':
169 main()
Something went wrong with that request. Please try again.