Advertisement
Guest User

imdb

a guest
Mar 12th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import requests
  2. import math
  3. import os
  4. from BeautifulSoup import BeautifulStoneSoup
  5. from PIL import Image, ImageDraw, ImageFont
  6.  
  7. database=[]
  8. text=[]
  9.  
  10. def make_database():
  11.     global database
  12.     file_of_films=open('films.txt','r')
  13.     keys=['film','title','id','xml','poster','$']
  14.     x=0
  15.     for line in file_of_films:
  16.         if line[0]=='$': None
  17.         else:
  18.             database.append(dict((tag, None ) for tag in keys))
  19.             name=line.strip('\n').split(' ')
  20.             if len(name)==2:
  21.                 database[x]['film']=name[0]
  22.                 database[x]['id']=name[1]
  23.             else:
  24.                 database[x]['film']=name[0]
  25.             x+=1
  26. def carriage_return_in_image():
  27.     x=0
  28.     for line in open('films.txt','r'):
  29.         if line[0]=='$':
  30.             database[x-1]['$']='$'
  31.         else: x+=1                        #dont try understand this
  32.  
  33. def search_movie_in_database():
  34.     for film in database:
  35.         list_of_params={'title':film['film'],'id':film['id'],'type':'xml'}
  36.         urlreq = requests.get("http://imdbapi.org/", params=list_of_params)
  37.         film['xml']=urlreq.content
  38.  
  39. def parse_results():
  40.     for film in database:
  41.         soup = BeautifulStoneSoup(''.join(film['xml']))
  42.         if soup.poster==None: None
  43.         else:
  44.             film['poster']=str(soup.poster).strip('</poster>')
  45.         film['title']=str(soup.title).strip('</title>')
  46.  
  47. def downloads():
  48.     for film in database:
  49.         if film['poster']!=None:
  50.             url=requests.get(film['poster'])
  51.         if film['poster']!=None:
  52.             with open(film['title']+'.jpg', "wb") as code:
  53.                 code.write(url.content)
  54.  
  55. def make_genres():
  56.     global text
  57.     for line in open('films.txt','r'):
  58.         if line[0]=='$':
  59.             text.append(line)
  60.            
  61.            
  62. def params_of_image():
  63.    
  64.     num=len(text)-1
  65.    
  66.     i,z=0,0
  67.     for film in database:
  68.         i+=1
  69.         if film['$']=='$':
  70.             if z<i: z=i
  71.             i=0
  72.            
  73.     xx=num*214+(num-1)*40+100
  74.     yy=z*317+z*(430-317)+300
  75.     return num,z,xx,yy
  76.            
  77. def make_image():
  78.     num,z,xx,yy=params_of_image()
  79.     x,y=0,0
  80.    
  81.  
  82.     image=Image.new('RGB',(xx,yy),'black')
  83.  
  84.  
  85.            
  86.     for film in database:
  87.        
  88.         if film['poster']!=None:
  89.             poster=Image.open(film['title']+'.jpg')
  90.         else: poster=Image.new('RGB',(214,317),'white')
  91.        
  92.         size = poster.size
  93.         if poster.size!=(214,317):
  94.             poster=poster.resize((214,317))
  95.         image.paste(poster,(xx-50-(num-1)*40-num*214+x, yy-z*317-(z-1)*(430-317)-100+y))
  96.         y+=430
  97.         if film['$']=='$':
  98.             x+=254
  99.             y=0
  100.  
  101.     image.save('your_advise.png','PNG')
  102.  
  103.  
  104. def draw_text2():
  105.    
  106.     num,z=params_of_image()
  107.     x,y=0,0
  108.    
  109.     file=open('films.txt','r')
  110.     title_of_image=file.readline().strip('$').strip('\n')
  111.     image=Image.open('your_advise.png')
  112.     font=font = ImageFont.truetype("font.ttf", 200)
  113.     draw = ImageDraw.Draw(image)
  114.    
  115.     size_of_title=font.getsize(title_of_image)
  116.     x_pos_title=round((xx-size_of_title[0])/2)
  117.     draw.text((x_pos_title,10), str(title_of_image),fill='white',font=font)
  118.        
  119.    
  120.     image.save('your_advise.png')
  121.    
  122. def delute_trash():
  123.     for film in database:
  124.         if film['poster']!=None:
  125.             try:
  126.                 files=film['title']+'.jpg'
  127.                 os.remove(files)
  128.             except: None
  129.  
  130. def main():
  131.     make_database()
  132.     carriage_return_in_image()
  133.     make_genres()
  134.     search_movie_in_database()
  135.     parse_results()
  136.     downloads()
  137.     make_image()
  138.     draw_text2()
  139.     delute_trash()
  140.    
  141. if __name__ == '__main__':
  142.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement