Skip to content

Instantly share code, notes, and snippets.

@SeanDS
Created January 10, 2021 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanDS/3b0e5eefa837f5ff0a136b71f9a54d26 to your computer and use it in GitHub Desktop.
Save SeanDS/3b0e5eefa837f5ff0a136b71f9a54d26 to your computer and use it in GitHub Desktop.
Download tagged photos from Facebook
"""Facebook tagged photos download script.
Requires a Facebook API token - see the following blog post for more
information: https://attackllama.com/2021/01/downloading-tagged-photos-from-facebook/
Sean Leavey
https://attackllama.com/
https://github.com/SeanDS/
"""
import os
import pickle
import datetime
import time
import requests
SLEEP = 3600 // 200 # One hour / max requests per hour.
ACCESS_TOKEN = "replace-with-long-API-token-string"
OUTPUT_DIR = "/tmp"
PICKLE_FILE = "/tmp/photos.pickle"
def fetch():
if os.path.isfile(PICKLE_FILE):
print("Using existing photo list")
with open(PICKLE_FILE, "rb") as fobj:
data = pickle.load(fobj)
else:
print("Dumping photo list")
url = f"https://graph.facebook.com/me/photos"
result = requests.get(
url, params={"limit": 10000, "access_token": ACCESS_TOKEN}
)
result.raise_for_status()
data = result.json()["data"]
with open(PICKLE_FILE, "wb") as fobj:
pickle.dump(data, fobj)
ndata = len(data)
for i, photo in enumerate(data, start=1):
print(f"{i}/{ndata}")
imagedate = datetime.datetime.strptime(
photo["created_time"], "%Y-%m-%dT%H:%M:%S%z"
)
imagepath = f"{imagedate.strftime('%Y%m%d%H%M%S')}-{photo['id']}.jpg"
filename = f"{OUTPUT_DIR}/{imagepath}"
if os.path.isfile(filename):
print(f"Skipping already downloaded file {filename}")
continue
image_url = f"https://graph.facebook.com/v9.0/{photo['id']}"
image_result = requests.get(
image_url,
params={"fields": ["images"], "access_token": ACCESS_TOKEN}
)
image_result.raise_for_status()
images = image_result.json()["images"]
os.system(f"wget -O '{filename}' '{images[0]['source']}'")
print(f"Sleeping for {SLEEP} s")
time.sleep(SLEEP)
if __name__ == "__main__":
fetch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment