Mercurial > lm
annotate lm @ 4:a8b144574340 default tip
README: typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jul 2010 21:29:12 +0800 |
parents | 6f4b5e24cf08 |
children |
rev | line source |
---|---|
0
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 lm: list movies (or list media) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2010 Jérôme Poisson (goffi@goffi.org) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import re |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import sys |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 import os,os.path |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import cPickle as pickle |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from difflib import SequenceMatcher |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from optparse import OptionParser #To be replace by argparse ASAP |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 import imdb |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 except: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 print "lm needs IMDbPY to work, please install it" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 sys.exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 NAME = 'lm (list movies)' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 VERSION = '0.1' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 ABOUT = NAME+" v"+VERSION+""" (c) Jérôme Poisson (aka Goffi) 2010 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 --- |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 """+NAME+""" Copyright (C) 2010 Jérôme Poisson (aka Goffi) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 This program comes with ABSOLUTELY NO WARRANTY; |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 This is free software, and you are welcome to redistribute it |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 under certain conditions. |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 --- |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 This software is a command line tool for listing movies using IMDb metadata |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 Get the latest version at http://www.goffi.org |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 """ |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 movie_ext = [u'.divx', u'.mov', u'.avi', u'.ogv', u'.rmvb', u'.mkv', u'.mpg', u'.wmv', u'.mp4'] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 forbidden_words = ['divx','dvdrip','xvid'] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 i = imdb.IMDb() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 COLOR_RED = "\033[00;31m" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 COLOR_GREEN = "\033[01;32m" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 COLOR_BLUE = "\033[01;34m" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 COLOR_END = '\033[0m' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 class FilterParsingError(Exception): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 pass |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 class ListMedia(): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 def __init__(self): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 self.load_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 def load_cache(self): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 """try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 with open(os.path.expanduser('~/.lm_imdb_cache'),'r') as f: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self.found_cache = pickle.load(f) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 except: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self.found_cache = {}""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 with open(os.path.expanduser('~/.lm_movies_cache'),'r') as f: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.movies = pickle.load(f) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 except: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.movies = {} |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 #self.movies = {} |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 def save_cache(self): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 #with open(os.path.expanduser('~/.lm_imdb_cache'),'w') as f: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 # pickle.dump(self.found_cache,f) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 with open(os.path.expanduser('~/.lm_movies_cache'),'w') as f: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 pickle.dump(self.movies,f) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 def __set_id(self, imdb_id, filename): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 """Set an individual id""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 found = i.get_movie(self.options.set_id) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 except imdb.IMDbError: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 print "connexion error" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 sys.exit(3) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 if not found: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 print "No movie found with the id [%s]" % imdb_id |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 sys.exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 if not self.movies.has_key(filename): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 self.movies[filename] = {'guessed_title':found.get('title') or filename} |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 self.__fill_metadata(self.movies[filename], found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 self.movies[filename]['unsure'] = False |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 self.save_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 def parse_arguments(self): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 _usage=""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 %prog [options] [FILE1 FILE2 ...] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 %prog --set-id [IMDb id] MOVIE_FILE |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 %prog --help for options list |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 """ |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 parser = OptionParser(usage=_usage,version=ABOUT) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 parser.add_option('-a','--alphabetical', action="store_true", default=False, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 help="sort by alphabetical order of title instead of rating") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 parser.add_option('-r','--reverse', action="store_true", default=False, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 help="show media in reverse order") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 parser.add_option('-f','--filter', action="store", type="string", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 help="filter (cf README)") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 parser.add_option('--set-id', action="store", type="string", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 help="manually give id to a movie (only one movie must be given in argument)") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 parser.add_option('-l','--long', action="store_true", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 help="Show long information on movie") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 parser.add_option('-L','--very-long', action="store_true", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 help="Show full information on movie") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 parser.add_option('-o','--outline', action="store_true", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 help="Show plot outline") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 parser.add_option('--confirm', action="store_true", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 help="Confirm unsure movies") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 parser.add_option('-s', '--show', action="store_true", |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 help="Show IMDb webpage of each movie in default navigator (DON'T USE IF YOU'RE LISTING A LOT OF FILES)") |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 (self.options, args) = parser.parse_args() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 if self.options.set_id: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 if len(args) != 1: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 print "Only one movie can be given with set-id option" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 sys.exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 self.__set_id(self.options.set_id,args[0]) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 sys.exit(0) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 if self.options.show: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 import webbrowser |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 global webbrowser |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 if not args: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 if self.options.confirm: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 print "You have to explicitly give movies when using --confirm" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 args=['.'] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 return args |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 def post_traitement(self,basenames): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 """Must be called at the end""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 if self.options.confirm: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 for movie in basenames: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 if self.movies.has_key(movie): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 self.movies[movie]['unsure'] = False |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 self.save_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 def get_files(self,args): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 """Return files from args, files contained for directories""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 result = [] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 for arg in args: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 if not arg: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 continue #we don't want empty arg |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 real_path = os.path.expanduser(arg).decode('utf-8') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 if os.path.isdir(real_path): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 base_path = arg+'/' if arg[-1]!='/' else arg |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 if base_path == "./": |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 base_path = '' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 result.extend([base_path+basename for basename in os.listdir(real_path)]) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 elif os.path.isfile(real_path): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 result.append(arg.decode('utf-8')) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 return result |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 def guess_titles(self,movie_files): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 """Try to guess title from movie filename, and fill movies 'guessed_title' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 @param movie_files: filenames to parse""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 #some useful regex |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 title_reg = re.compile('^[^[(]+') #we take everything before information in bracket or square bracket, as these info are usually not part of the title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 before_year_reg = re.compile(r'(.*[^0-9])[0-9]{4}[^0-9].*') #the year is most of time placed between the title and other information, we are intersted by what is before |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 upper_reg = re.compile(r'(^.+?)[A-Z]{2}.*') #in some case, we have the title with lowercases, and other info (e.g. language) fully uppercase, this regex test this |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 #We now try to clean the filename, to guess the real title from it, which we will need for our imdb request |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 for filename in movie_files: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 if self.movies.has_key(filename) and self.movies[filename].has_key('guessed_title'): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 #if movie already in cache, we pass it |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 continue |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 file_tuple = os.path.splitext(filename) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 tmp_title = (re.findall(title_reg,file_tuple[0]) or [file_tuple[0]])[0].replace('.',' ').replace('_',' ') #first regex & '.' and '_' replaced by space |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 tmp_title = re.sub(before_year_reg, r'\1', tmp_title) or tmp_title #2nd regex |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 title = re.sub(upper_reg,r'\1', tmp_title) or tmp_title #3rd regex |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 if len(title) < 3: #In some cases, the previous regex give a wrong title, we try to detect this by cancelling too short title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 title = tmp_title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 for forbidden in forbidden_words: #we now remove words which can stay in the title and are propably not part of it |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 if forbidden in title.lower(): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 idx = title.lower().find(forbidden) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 title = title[:idx] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 #finished, we must have a title more sexy |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 self.movies[filename] = {'guessed_title':title} |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 def __print_not_found(self, not_found): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 if not_found: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 print "Movies not found:" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 for filename in not_found: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 print filename |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 print "---\n\n" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 def post_check(self, movie, current, found): |
2 | 211 """Check after filling, that the found movie has a title close to the filename |
0
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 @param movie: filename of the movie |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 @param current: dict of metadata for current movie, 'unsure' value will be filled by this method |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 @param found: found imdb.Movie.Movie""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 _filename = movie.lower().replace('.',' ').replace('_',' ') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 _title = current['title'].lower().replace('!','').replace('?','') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 ratio = SequenceMatcher(None,_title,_filename[:len(_title)]).ratio() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 if ratio < 0.5: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 #Bad ratio, we do additionnal checks |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 #print "\nbad ratio(%f): %s ==> %s" % (ratio, movie, current['title']) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 _best_title = _title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 if _title in _filename: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 #We can found the title in the filename, sounds good |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 #print "title found in filename, OK :)" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 current['unsure'] = False |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 _biggest_ratio = ratio |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 #We now check with other titles found in IMDB ('akas' key) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 for other_title in [title.split(' - ')[0].replace('"','') for title in (found.get('akas') or [])] or '': |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 current_ratio = SequenceMatcher(None,other_title,_filename[:len(other_title)]).ratio() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 if current_ratio > _biggest_ratio: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 _biggest_ratio = current_ratio |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 _best_title = other_title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 #print "biggest ratio ==> %f (for [%s]) -- %s" % (_biggest_ratio, _best_title, 'OK' if _biggest_ratio>0.5 else 'refused') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 current['unsure'] = False if _biggest_ratio >= 0.5 else True |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 current['unsure'] = False |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 if current['unsure']: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 current['best_matching'] = _best_title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 def __fill_metadata(self, current, found): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 """Fill metadata for one movie |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 @param current: metadata dict to fill |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 @param found: found imdb.Movie.Movie""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 current['id'] = found.movieID |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 current['title'] = found.get('title') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 current['canonical_title'] = found.get('smart canonical title') or title |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 current['rating'] = found.get('rating') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 current['year'] = found.get('year') |
1
df1b98df8be9
storing empty list for genre instead of None when there is now genre
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
256 current['genre'] = found.get('genre') or [] |
0
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 current['director'] = [director.get('name') for director in (found.get('director') or [])] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 current['short_summary'] = found.get('plot outline') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 current['summary'] = (found.get('plot') or [''])[0] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 current['cast'] = [actor.get('name') for actor in (found.get('cast') or [])] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 return current |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 def get_metadata(self, files): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 """Get metadata for files not already in cache |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 @param files: list of filename (just basename, with extension)""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 _movie_files = filter(lambda file:os.path.splitext(file)[1].lower() in movie_ext,files) #We only wants movies |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 self.guess_titles(_movie_files) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 _movies_to_get = filter(lambda movie:not self.movies[movie].has_key('title'), self.movies) #We want to parse movies not already parsed |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 if not _movies_to_get: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 return |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 idx = 1 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 total = len(_movies_to_get) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 not_found = [] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 last_len = 0 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 for movie in _movies_to_get: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 out_str = u"Getting metadata: [%(index)i/%(nb_movies)i] %(guessed_title)s\r" % {'index':idx,'nb_movies':total,'filename':movie,'guessed_title':self.movies[movie]['guessed_title']} |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 if len(out_str) < last_len: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 sys.stdout.write(' '*last_len+'\r') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 last_len = len(out_str) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 sys.stdout.write(out_str.encode('utf-8')) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 sys.stdout.flush() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 idx+=1 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 results = i.search_movie(self.movies[movie]['guessed_title']) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 #results = [self.found_cache[movie]] if self.found_cache.has_key(movie) else [] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 if not results: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 not_found.append(movie) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 self.movies[movie]['title'] = None |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 found = results[0] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 i.update(found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 #self.found_cache[movie] = found |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 current = self.__fill_metadata(self.movies[movie], found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 self.post_check(movie, current, found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 except imdb.IMDbError, e: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 print "Connexion error, current movie: [%s]" % movie |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 self.__print_not_found(not_found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 print "\n\n---\n\n" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 print e |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 self.save_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 sys.exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 if idx%15==0: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 #We save work in case of problem |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 self.save_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 self.save_cache() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 print "\n" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 self.__print_not_found(not_found) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 def user_filter(self, file_tuple): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 """Filter movies according to user given arguments""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 filt = self.options.filter |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 pos = 0 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 try: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 while filt: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 if filt[0]!='@': |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 raise FilterParsingError |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 end = filt.find(':') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 if end == -1: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 raise FilterParsingError |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 filter_type = filt[1:end] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 if not filter_type in ['genre','director','actor']: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 raise FilterParsingError |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 if filter_type=="actor": |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 filter_type='cast' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 filt = filt[end+1:] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 end = filt.find('@') |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 if end == -1: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 end = None |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 keys = set([key.lower() for key in filt[:end].split(',')]) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 filt = filt[end:] if end else '' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 file_tuple = filter(lambda m:set([key.lower() for key in self.movies[m[1]][filter_type]]).intersection(keys),file_tuple) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 except FilterParsingError,IndexError: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 print "Invalid filter ! Please read README for syntax" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 exit(2) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 return file_tuple |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 def pretty_print(self, file_tuple): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 """Print movie with metadata and colors according to arguments""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 filename,basename = file_tuple |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 current = self.movies[basename] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 values_dict = {'b':COLOR_BLUE, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 'e':COLOR_END, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 'header':COLOR_RED + '/!\\ ' + COLOR_END if current['unsure'] else '', |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 'title':COLOR_GREEN + current['title'] + COLOR_END, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 'rating':unicode(current['rating']), |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 'year':current['year'], |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 'genre':"%s" % ', '.join(current['genre']), |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 'filename':filename, |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 'director':', '.join(current['director']) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 } |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 if self.options.very_long: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 out_str = u"%(header)s%(title)s (%(b)srating%(e)s: %(rating)s)\n%(b)syear%(e)s: %(year)s %(b)sgenre%(e)s: %(genre)s\n%(b)sfile%(e)s: %(filename)s\n%(b)sdirector%(e)s: %(director)s\n" % values_dict |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 cast_header = COLOR_BLUE+u"cast"+COLOR_END+": " |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 len_cast_header = len(cast_header) - len(COLOR_BLUE) - len(COLOR_END) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 out_str+=cast_header |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 first = True |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 for actor in current['cast']: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 if first: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 first = False |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 out_str += actor+'\n' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 out_str+=len_cast_header*u' '+actor+'\n' |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 out_str += "\n" + COLOR_BLUE + "summary"+COLOR_END+": %s\n---\n" % current['summary'] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 elif self.options.long: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 out_str = u"%(header)s%(title)s (%(year)s,%(rating)s) [%(b)s%(genre)s%(e)s] from %(director)s: %(filename)s\n" % values_dict |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 out_str = u"%(header)s%(title)s (%(filename)s)\n" % values_dict |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 sys.stdout.write(out_str.encode('utf-8')) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 if self.options.outline and current['short_summary']: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 sys.stdout.write(unicode("\t"+current['short_summary']+'\n').encode('utf-8')) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 def show_list(self, files): |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 """Show the list of files, using metadata according to arguments""" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 files_tuple = [(path, os.path.basename(path)) for path in files] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 _movie_files = filter(lambda file:os.path.splitext(file[1])[1].lower() in movie_ext,files_tuple) #We only wants movies |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
387 _movie_files = filter(lambda m: self.movies[m[1]]['title'] != None, _movie_files) #We want only parsed movies |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
389 if self.options.filter: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 _movie_files = self.user_filter(_movie_files) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 if not _movie_files: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 print "No movie found" |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 exit(1) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 if self.options.alphabetical: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 _key=lambda m: self.movies[m[1]]['canonical_title'].lower() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 else: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 _key=lambda m: self.movies[m[1]]['rating'] |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 _movie_files.sort(key=_key,reverse=self.options.reverse) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 for _file_tuple in _movie_files: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 self.pretty_print(_file_tuple) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 if self.options.show: |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 webbrowser.open_new_tab(imdb.imdbURL_movie_main % self.movies[_file_tuple[1]]['id']) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 LM = ListMedia() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 args = LM.parse_arguments() |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 files = LM.get_files(args) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 basenames = set(map(os.path.basename,files)) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 LM.get_metadata(basenames) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 LM.show_list(files) |
c6e7b3287d65
Initial commit: first public realease (0.1)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
414 LM.post_traitement(basenames) |