Last active
December 22, 2016 05:34
-
-
Save andreiw/d445dcd6aae8218728aa13d5c3d23b77 to your computer and use it in GitHub Desktop.
Pythonista extension to import URL as Python script
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
import appex | |
import clipboard | |
import urllib3 | |
import os | |
def main(): | |
if not appex.is_running_extension(): | |
print('This script is intended to be run from the sharing extension.') | |
return | |
url = appex.get_url() | |
if not url: | |
print('No URL?') | |
return | |
fname = url.split('/')[-1] | |
fname, fext = os.path.splitext(fname) | |
if fext != ".py": | |
fext = ".py" | |
fname = fname + fext | |
fname = os.path.join(os.path.expanduser('~/Documents'), fname) | |
http = urllib3.PoolManager() | |
response = http.request('GET', url) | |
with open(fname, 'wb') as f: | |
f.write(response.data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment