Skip to content

Instantly share code, notes, and snippets.

@qunwang6
Last active December 12, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qunwang6/33a82375463fa4eb72e8c025f0dbccbf to your computer and use it in GitHub Desktop.
Save qunwang6/33a82375463fa4eb72e8c025f0dbccbf to your computer and use it in GitHub Desktop.
import requests
import clipboard
import console
'''
Get Raw Text From URL
A wrench script to copy the text of a url to the clipboard.
I did this so I can get the text of a gist quickly.
To use, when you are viewing the gist in a web browser, click the
raw button on the page.
Then use the share btn on the page to copy the url.
Then run this scipt, the script does a very weak test to see if it should
fill in the input_alert for you. otherwise paste or type it in.
Then requests attempts to get the url. If the status_code == 200,
means we got something. A hud is displayed saying how many bytes where copied
to the clipboard.
Look, this is just a quick and dirty tool i did for my own use. I know
there are many ways to achieve the same result, using eg. the share sheet
Maybe there is an easy way to get the text of the gist without going to
the raw page. I am not sure.
Could have also given an option to put the text into a file and open it
in the editor. But I wanted to keep it simple for myself. I already have a
wrench item to quickly create a new file in a particular dir.
@Phuket2 Pythonistia Forum
'''
if __name__ == '__main__':
clip_txt = clipboard.get()
# do a simple lame test to see if we think we have a url of interest
# on the clipboard. Just saves having to do a paste
if not clip_txt.startswith('https://gist'):
clip_txt = ""
url = console.input_alert('Enter URL of raw text', "", clip_txt)
resp = requests.get(url, timeout=5)
if resp.status_code != 200:
# I think this is the correct way to handle a requests error.
requests.raise_for_status()
clipboard.set(resp.text)
console.hud_alert('{} bytes copied'.format(len(resp.text)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment