Created
October 28, 2016 11:21
-
-
Save derhofbauer/fd78f014579f3c81bc7d3b8d9be3353b to your computer and use it in GitHub Desktop.
Python3 Facebook Album Downloader: Album ID and Graph API Access token are required
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Get an access token here: | |
# https://developers.facebook.com/tools/explorer/ | |
# | |
# Get the album id from the url when watching the album | |
# in your browser. | |
import sys | |
import os.path | |
import getopt | |
import requests | |
ACCES_TOKEN = "" | |
HELPSTRING = "downloader.py -a <album_id> -t <access_token>" | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv, "ha:t:", ["album=", "token="]) | |
except getopt.GetoptError: | |
print(HELPSTRING) | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print(HELPSTRING) | |
sys.exit() | |
elif opt in ("-a", "--album"): | |
album_id = arg | |
elif opt in ("-t", "--token"): | |
ACCES_TOKEN = arg | |
else: | |
print(HELPSTRING) | |
sys.exit(2) | |
if album_id.isdigit(): | |
created_time, name, album_id = get_album_meta(album_id) | |
q = input("Do you want to download '"+name+"' [Y/n] ") | |
if q in ("Y", "y", "yes", "Yes"): | |
download_album(album_id) | |
def get_album_meta(album_id): | |
r = requests.get("https://graph.facebook.com/v2.8/" + album_id + | |
"?access_token=" + ACCES_TOKEN) | |
if r.status_code == 200 and r.json()['name']: | |
return r.json()["created_time"], r.json()["name"], r.json()["id"] | |
else: | |
print("Error: request went wrong or you have no permission to access " | |
"this album") | |
sys.exit(1) | |
def get_album_photos(album_id): | |
r = requests.get("https://graph.facebook.com/v2.8/" + album_id + | |
"/photos?access_token=" + ACCES_TOKEN) | |
return r.json()["data"] | |
def download_album(album_id): | |
photos = get_album_photos(album_id) | |
download_urls = [] | |
for photo in photos: | |
uid = photo['id'] | |
r = requests.get("https://graph.facebook.com/v2.8/" + uid + | |
"?fields=images&access_token="+ACCES_TOKEN) | |
print("get download url for picture '"+photo["id"]+"' ...") | |
download_urls.append(r.json()["images"][0]['source']) | |
# print(r.json()["images"][0]['source']) | |
print("There are "+str(len(download_urls))+" pictures in this album") | |
if not os.path.exists("./photos"): | |
os.makedirs("./photos") | |
for url in download_urls: | |
filename = url.split('/')[-1] | |
path = os.path.abspath("./photos/"+filename) | |
print("downlading picture '"+filename+"' ...") | |
r = requests.get(url, stream=True) | |
with open(path, 'wb') as file: | |
total_length = int(r.headers.get('content-length')) | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
file.write(chunk) | |
file.flush() | |
if os.path.getsize("./photos/"+filename) == total_length: | |
pass | |
else: | |
print("Error downloading "+filename) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment