~ubuntu-branches/ubuntu/karmic/imdbpy/karmic

« back to all changes in this revision

Viewing changes to bin/get_first_character.py

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2007-11-18 15:18:06 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071118151806-s6s42pw750q3g59y
Tags: 3.3-1
* New upstream version.
* Move Homepage into control field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
get_first_character.py
 
4
 
 
5
Usage: get_first_character "character name"
 
6
 
 
7
Search for the given name and print the best matching result.
 
8
"""
 
9
# Parameters to initialize the IMDb class.
 
10
IMDB_PARAMS = {
 
11
    # The used access system. 'web' means that you're retrieving data
 
12
    # from the IMDb web server.
 
13
    'accessSystem': 'web'
 
14
    #'accessSystem': 'mobile'
 
15
    # XXX: if you've a local installation of the IMDb database,
 
16
    # comment the above line and uncomment the following two.
 
17
    #'accessSystem': 'local',
 
18
    #'dbDirectory':  '/usr/local/imdb' # or, in a Windows environment:
 
19
    #'dbDirectory':  'D:/imdb-20060107'
 
20
 
 
21
    # XXX: parameters for a SQL installation.
 
22
    #'accessSystem': 'sql',
 
23
    #'uri': 'mysql://userName:yourPassword@localhost/dbName'
 
24
}
 
25
 
 
26
import sys
 
27
 
 
28
# Import the IMDbPY package.
 
29
try:
 
30
    import imdb
 
31
except ImportError:
 
32
    print 'You bad boy!  You need to install the IMDbPY package!'
 
33
    sys.exit(1)
 
34
 
 
35
 
 
36
if len(sys.argv) != 2:
 
37
    print 'Only one argument is required:'
 
38
    print '  %s "character name"' % sys.argv[0]
 
39
    sys.exit(2)
 
40
 
 
41
name = sys.argv[1]
 
42
 
 
43
 
 
44
i = imdb.IMDb(**IMDB_PARAMS)
 
45
 
 
46
in_encoding = sys.stdin.encoding or sys.getdefaultencoding()
 
47
out_encoding = sys.stdout.encoding or sys.getdefaultencoding()
 
48
 
 
49
name = unicode(name, in_encoding, 'replace')
 
50
try:
 
51
    # Do the search, and get the results (a list of character objects).
 
52
    results = i.search_character(name)
 
53
except imdb.IMDbError, e:
 
54
    print "Probably you're not connected to Internet.  Complete error report:"
 
55
    print e
 
56
    sys.exit(3)
 
57
 
 
58
if not results:
 
59
    print 'No matches for "%s", sorry.' % name.encode(out_encoding, 'replace')
 
60
    sys.exit(0)
 
61
 
 
62
# Print only the first result.
 
63
print '    Best match for "%s"' % name.encode(out_encoding, 'replace')
 
64
 
 
65
# This is a character instance.
 
66
character = results[0]
 
67
 
 
68
# So far the character object only contains basic information like the
 
69
# name; retrieve main information:
 
70
i.update(character)
 
71
 
 
72
print character.summary().encode(out_encoding, 'replace')
 
73
 
 
74
 
 
75