Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Pythonista to download an image from Instagram. Usage via the share extension or run it straight from your home screen (in this case it will get the url from the iOS clipboard).
#!python2
# this script is for Pythonista 2 and 3
# author: Francesco Pira (fpira.com)
import sys
import requests
from bs4 import BeautifulSoup
import re
import clipboard
import Image
import photos
from urllib import urlopen
from io import BytesIO
import console
# if no arg is passed (aka you're not running it from Share Extension,
# get the url from iOS clipboard)
source_url = sys.argv[1] if len(sys.argv) > 1 else clipboard.get()
if not source_url:
console.alert('Sorry, no URL given!','','OK',hide_cancel_button=True)
exit()
try:
r = requests.get(source_url)
html_doc=''
for line in r:
html_doc = html_doc + line
soup = BeautifulSoup(html_doc, 'html.parser')
for meta in soup.find_all(re.compile("^meta")):
tags = meta.attrs
if 'property' in tags:
if tags['property'] == 'og:image':
img_url = tags['content']
img = Image.open(BytesIO(urlopen(img_url).read()))
photos.save_image(img)
console.alert('Image saved in Photos','','OK',hide_cancel_button=True)
except:
console.alert('Oops! Something went wrong... Please check your clipboard!','','OK',hide_cancel_button=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.