Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import math
- import os
- from BeautifulSoup import BeautifulStoneSoup
- from PIL import Image, ImageDraw, ImageFont
- database=[]
- text=[]
- def make_database():
- global database
- file_of_films=open('films.txt','r')
- keys=['film','title','id','xml','poster','$']
- x=0
- for line in file_of_films:
- if line[0]=='$': None
- else:
- database.append(dict((tag, None ) for tag in keys))
- name=line.strip('\n').split(' ')
- if len(name)==2:
- database[x]['film']=name[0]
- database[x]['id']=name[1]
- else:
- database[x]['film']=name[0]
- x+=1
- def carriage_return_in_image():
- x=0
- for line in open('films.txt','r'):
- if line[0]=='$':
- database[x-1]['$']='$'
- else: x+=1 #dont try understand this
- def search_movie_in_database():
- for film in database:
- list_of_params={'title':film['film'],'id':film['id'],'type':'xml'}
- urlreq = requests.get("http://imdbapi.org/", params=list_of_params)
- film['xml']=urlreq.content
- def parse_results():
- for film in database:
- soup = BeautifulStoneSoup(''.join(film['xml']))
- if soup.poster==None: None
- else:
- film['poster']=str(soup.poster).strip('</poster>')
- film['title']=str(soup.title).strip('</title>')
- def downloads():
- for film in database:
- if film['poster']!=None:
- url=requests.get(film['poster'])
- if film['poster']!=None:
- with open(film['title']+'.jpg', "wb") as code:
- code.write(url.content)
- def make_genres():
- global text
- for line in open('films.txt','r'):
- if line[0]=='$':
- text.append(line)
- def params_of_image():
- num=len(text)-1
- i,z=0,0
- for film in database:
- i+=1
- if film['$']=='$':
- if z<i: z=i
- i=0
- xx=num*214+(num-1)*40+100
- yy=z*317+z*(430-317)+300
- return num,z,xx,yy
- def make_image():
- num,z,xx,yy=params_of_image()
- x,y=0,0
- image=Image.new('RGB',(xx,yy),'black')
- for film in database:
- if film['poster']!=None:
- poster=Image.open(film['title']+'.jpg')
- else: poster=Image.new('RGB',(214,317),'white')
- size = poster.size
- if poster.size!=(214,317):
- poster=poster.resize((214,317))
- image.paste(poster,(xx-50-(num-1)*40-num*214+x, yy-z*317-(z-1)*(430-317)-100+y))
- y+=430
- if film['$']=='$':
- x+=254
- y=0
- image.save('your_advise.png','PNG')
- def draw_text2():
- num,z=params_of_image()
- x,y=0,0
- file=open('films.txt','r')
- title_of_image=file.readline().strip('$').strip('\n')
- image=Image.open('your_advise.png')
- font=font = ImageFont.truetype("font.ttf", 200)
- draw = ImageDraw.Draw(image)
- size_of_title=font.getsize(title_of_image)
- x_pos_title=round((xx-size_of_title[0])/2)
- draw.text((x_pos_title,10), str(title_of_image),fill='white',font=font)
- image.save('your_advise.png')
- def delute_trash():
- for film in database:
- if film['poster']!=None:
- try:
- files=film['title']+'.jpg'
- os.remove(files)
- except: None
- def main():
- make_database()
- carriage_return_in_image()
- make_genres()
- search_movie_in_database()
- parse_results()
- downloads()
- make_image()
- draw_text2()
- delute_trash()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement