Wuhu! Mein erstes Python Skript.

Python ist einfach toll. :-)

 1 #------------------------------------------------------------------------------
 2 # @FILENAME:      ./imdbCovers.py
 3 # @AUTHOR:        Happy
 4 # @DESCRIPTION:   Using imdbapi.com to get the URI of a movie poster 
 5 #                 by providing the title and and year.
 6 #                 Example: http://www.imdbapi.com/?t=True%20Grit&y=1969
 7 # @LICENSE:       WTFPL - http://sam.zoy.org/wtfpl/
 8 #------------------------------------------------------------------------------
 9 #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
10 #                    Version 2, December 2004
11 #
12 # Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
13 #
14 # Everyone is permitted to copy and distribute verbatim or modified
15 # copies of this license document, and changing it is allowed as long
16 # as the name is changed.
17 #
18 #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
19 #   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
20 #
21 #  0. You just DO WHAT THE FUCK YOU WANT TO. 
22 #------------------------------------------------------------------------------
23 #/* This program is free software. It comes without any warranty, to
24 # * the extent permitted by applicable law. You can redistribute it
25 # * and/or modify it under the terms of the Do What The Fuck You Want
26 # * To Public License, Version 2, as published by Sam Hocevar. See
27 # * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
28 #------------------------------------------------------------------------------
29 
30 import urllib, urllib2
31 import json
32 
33 
34 # Function to purge the unclean! Or the JSON stuff. We just want the 'Poster'.
35 def purgeJsonCrap(jsonCrap):
36    lol = json.load(jsonCrap)
37    return lol['Poster']
38 
39 # Get the Cover URI
40 def gimmeCover(jahr, titel):
41    # URI to the API Server.
42    baseurl = "http://www.imdbapi.com/"
43    # Creating the dictionary for 'urlencode()'.
44    titeldic = dict()
45    titeldic = {'t' : titel}
46    titel = urllib.urlencode(titeldic)
47    # Concatenating the parts.
48    imguri = "?" + titel
49    imguri += "&amp;" + "?y=" + str(jahr)
50    uri = baseurl
51    uri += imguri
52    # Getting the JSON data.
53    opener = urllib2.urlopen(uri)
54    # Cleaning and returning.
55    return purgeJsonCrap(opener)
56 
57 
58 # Examples :-)
59 # print "\n" + gimmeCover(1995, 'Hackers')
60 # print gimmeCover(1995, 'True Grit')
61 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.