Created
March 5, 2018 01:38
-
-
Save cleverdevil/5bb767fd152de9b4c246d01086e91399 to your computer and use it in GitHub Desktop.
Upload my directory full of instagram photos to my website, using granary to look up the metadata
This file contains 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 python | |
import os | |
import sys | |
import requests | |
base_url = 'https://granary.io/instagram/cleverdevil/@self/@app/%s' | |
query_args = { | |
'format': 'mf2-json', | |
'cookie': 'INSERT_YOUR_INSTA_COOKIE_HERE' | |
} | |
# configuration | |
token = os.environ.get('INDIEAUTH_TOKEN') | |
endpoint = os.environ.get('MICROPUB_ENDPOINT') | |
h = {'Authorization': 'Bearer ' + token} | |
if __name__ == '__main__': | |
all_files = sorted(os.listdir('.')) | |
filenames = ( | |
f for f in all_files if f.endswith('.jpg') | |
) | |
for filename in filenames: | |
code = filename.split('.')[-2] | |
scraped = requests.get( | |
base_url % code, | |
params=query_args | |
).json()['items'][0]['properties'] | |
published = scraped['published'][0] | |
caption = scraped.get('content', [{}])[0].get('value', 'Published Photo') | |
permalink = scraped['url'][0] | |
params = { | |
'published': published, | |
'name': caption, | |
'syndication': permalink | |
} | |
files = { | |
'photo': (filename, open(filename, 'rb'), 'image/jpeg') | |
} | |
response = requests.post( | |
endpoint, | |
files=files, | |
data=params, | |
headers=h | |
) | |
if response.status_code in (200, 201): | |
print(response.headers['Location']) | |
os.system('mv "%s" published/' % filename) | |
else: | |
print(response.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment