Last active
November 15, 2019 05:37
-
-
Save jinglescode/4399678315112db75dee37b30e035d63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import time | |
| import re | |
| import os | |
| def download_poster(downloaded_image_dir, title, label, poster_path): | |
| if not os.path.exists(downloaded_image_dir): | |
| os.makedirs(downloaded_image_dir) | |
| if not os.path.exists(downloaded_image_dir+'/'+label): | |
| os.makedirs(downloaded_image_dir+'/'+label) | |
| imgUrl = 'http://image.tmdb.org/t/p/w185/' + poster_path | |
| local_filename = re.sub(r'\W+', ' ', title).lower().strip().replace(" ", "-") + '.jpg' | |
| try: | |
| session = requests.Session() | |
| r = session.get(imgUrl, stream=True, verify=False) | |
| with open(downloaded_image_dir+'/'+label+'/'+local_filename, 'wb') as f: | |
| for chunk in r.iter_content(chunk_size=1024): | |
| f.write(chunk) | |
| except: | |
| print('PROBLEM downloading', title,label,poster_path,imgUrl) | |
| time.sleep(1) | |
| # download image by iterate pandas | |
| for index, row in df_movies.iterrows(): | |
| download_poster( | |
| 'images_movies_genre', | |
| str(row['title']), | |
| str(row['genre']), | |
| row['poster_path'] | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment