Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload my directory full of instagram photos to my website, using granary to look up the metadata
#!/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