Skip to content

Instantly share code, notes, and snippets.

@PiotrKrzyzek
Created June 3, 2016 23:37
Show Gist options
  • Save PiotrKrzyzek/9d26c24c1d015c7c64e3ca79b3cb3ea1 to your computer and use it in GitHub Desktop.
Save PiotrKrzyzek/9d26c24c1d015c7c64e3ca79b3cb3ea1 to your computer and use it in GitHub Desktop.
Migrate Links From Bit.LY To Your YOURLS Install
#! /usr/bin/python
#Simple script for importing data from bitly to yourls using bitly and yourls API.
import urllib2, json, urllib
#Add the following data
yourls_host = 'your-domain-where-yours-is-installed.com'
bitly_token = 'your-bitly-token'
yourls_signature = 'your-yourls-signature-key'
def pushto_yourls_api(data):
url_dest = 'http://'+yourls_host+'/yourls-api.php'
for x in data['data']['link_history']:
link = x['link'].split('/')[3]
long_url = x['long_url']
values = dict(action='shorturl', url = long_url, keyword = link , signature=yourls_signature)
req_data = urllib.urlencode(values)
req = urllib2.Request(url_dest, req_data)
rsp = urllib2.urlopen(req)
content = rsp.read()
print content
origen_url = 'https://api-ssl.bitly.com/v3/user/link_history?format=json&access_token='+bitly_token+'&limit=50'
bitly_result = True
offset = 0
while bitly_result:
url = origen_url + '&offset=' + str(offset)
print url
response = urllib.urlopen(url)
data = json.loads(response.read())
if len(data['data']['link_history']) < 50:
bitly_result = False
offset +=50
pushto_yourls_api(data)
@GadgetComa
Copy link

I think I've managed to fix the UnicodeEncodeError. I don't know python, but from other programming experience and poking around a bit, I changed line 16 to:

values = dict(action='shorturl', url = unicode(long_url).encode('utf-8'), keyword = link , signature=yourls_signature)

The script was failing for me whenever there were accented characters. This change fixed that. It may not cover every scenario or be the most elegant, but it works.

Thanks for the script. It helped me migrate nearly 20+ links.

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