Last active
December 18, 2015 22:19
-
-
Save benad/5853797 to your computer and use it in GitHub Desktop.
Small script that scapes into HTML your bookmarks on bitly.com
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
#!/opt/local/bin/python2.7 | |
# Small script that scapes into HTML your bookmarks on bitly.com | |
# Licensed under DWTFYWWI: https://github.com/avar/DWTFYWWI/blob/master/DWTFYWWI | |
from __future__ import print_function | |
import bitly_api | |
import time | |
import sys | |
import httplib | |
import base64 | |
import string | |
# curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token" | |
if len(sys.argv) != 3: | |
print("Usage: " + sys.argv[0] + " <username> <password>") | |
print("Fetches all saved links in a user's bitly account as HTML to standard output") | |
sys.exit(1) | |
username = sys.argv[1] | |
password = sys.argv[2] | |
host = 'api-ssl.bitly.com' | |
# From http://mozgovipc.blogspot.ca/2012/06/python-http-basic-authentication-with.html | |
conn = httplib.HTTPSConnection(host) | |
auth = auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') | |
conn.putrequest("POST", "/oauth/access_token") | |
conn.putheader("Host", host) | |
conn.putheader("User-Agent", "Python") | |
conn.putheader("Authorization", "Basic %s" % auth) | |
conn.endheaders() | |
conn.send("") | |
resp = conn.getresponse() | |
token = resp.read() | |
bitly = bitly_api.Connection(access_token=token) | |
print('<html><head><meta charset="utf-8"><title>Links</title></head><body>') | |
offset = 0 | |
window = 100 | |
res = [1] | |
while len(res) > 0: | |
print('count: ' + str(offset), file=sys.stderr) | |
time.sleep(0.5) # Be nice to bitly's servers | |
res = bitly.user_link_history(limit=window, offset=offset) | |
for i in res: | |
url = i[u'long_url'].encode('utf8') | |
if i.has_key(u'title') and i[u'title'] != None: | |
title = i[u'title'].encode('utf8') | |
if i.has_key(u'title') and i[u'title'] != None: | |
title = i[u'title'].encode('utf8') | |
else: | |
title = url | |
print('<a href="' + url + '">' + title + '</a><br/>') | |
offset += len(res) | |
print('</body></html>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment