Last active
October 27, 2020 08:28
-
-
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
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
#!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 see it in action in this video.