Advertisement
Guest User

advice.py

a guest
Mar 14th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from cStringIO import StringIO
  5. from urllib import urlopen
  6.  
  7. from PIL import Image, ImageDraw, ImageFont
  8. from requests import get
  9.  
  10. # Poster width and hight
  11. WIDTH = 214
  12. HIGHT = 317
  13.  
  14.  
  15. def get_poster_url(imdb_id):
  16.     """Return poster URL if exist or False otherwise"""
  17.    
  18.     response = get('http://imdbapi.org/', params={'ids': imdb_id})
  19.     json = response.json()
  20.  
  21.     if json[0].__contains__('poster'):
  22.         return json[0]['poster']
  23.     else:
  24.         return False
  25.  
  26.  
  27. def main():
  28.  
  29.     # Getting list of unique genres from input file
  30.     genres = [line.split(',')[2] for line in open('films.txt', 'r')]
  31.     genres = [item.strip() for item in genres]
  32.     unique_genres = [item for item in set(genres)]
  33.  
  34.     # Getting maximum value of posters in column (genre)
  35.     counts = [''.join(genres).count(item) for item in unique_genres]
  36.     max_posters = max(counts)
  37.  
  38.     # Creating canvas
  39.     canv_width = 20 + (20 + WIDTH) * len(unique_genres)
  40.     canv_hight = 100 + HIGHT * max_posters + 20
  41.     canvas = Image.new('RGB', (canv_width, canv_hight), 'black')
  42.     draw = ImageDraw.Draw(canvas)
  43.    
  44.     # Adding genres
  45.     font = ImageFont.truetype(
  46.             "/usr/share/fonts/ubuntu-font-family/Ubuntu-C.ttf",
  47.             42)
  48.     shift = 0
  49.  
  50.     for genre in unique_genres:
  51.         draw.text((20 + shift, 20), genre, fill='white', font=font)
  52.         shift += (20 + WIDTH)
  53.  
  54.     # Adding posters
  55.     y = [100] * len(unique_genres)  # List of y values for each column (genre)
  56.     count = [0] * len(unique_genres)  # Number of posters in each column
  57.     xx = 0
  58.     yy = 0
  59.     with open('films.txt', 'r') as f:
  60.         for line in f:
  61.             imdb_id = line.split(',')[0]
  62.             title = line.split(',')[1].strip()
  63.             genre = line.split(',')[2].strip()
  64.  
  65.             poster_url = get_poster_url(imdb_id)
  66.            
  67.             if poster_url:
  68.                 # Open image(poster) from URL
  69.                 fs = StringIO(urlopen(poster_url).read())
  70.                 poster = Image.open(fs)
  71.                
  72.                 if poster.size != (WIDTH, HIGHT):
  73.                     poster = poster.resize (WIDTH, HIGHT)
  74.  
  75.                 # Sorting by genre
  76.                 for col in range(0, len(unique_genres)):
  77.                     if genre == unique_genres[col]:
  78.                         xx = 20 + (20 + WIDTH)*col
  79.                         y[col] = y[col] + HIGHT * count[col]
  80.                         yy = y[col]
  81.                         count[col] = count[col] + 1
  82.  
  83.                 canvas.paste(poster, (xx, yy))
  84.  
  85.             else:
  86.                 # Drawing caption if no poster available
  87.                 #draw.text((xx, yy), title, fill='white')
  88.                 pass
  89.  
  90.     canvas.save('advice.png')
  91.  
  92.    
  93. if __name__ == '__main__':
  94.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement