Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Last active October 27, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pirafrank/d5ec45fecdd7b3a124a79110f5e893d4 to your computer and use it in GitHub Desktop.
Save pirafrank/d5ec45fecdd7b3a124a79110f5e893d4 to your computer and use it in GitHub Desktop.
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). Save this file in the 'This Phone' path in Pythonista, then download this shortcut: https://www.icloud.com/shortcuts/d45759eb469b445b943049bf8488e995
#!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)
@pirafrank
Copy link
Author

You can see it in action in this video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment