python 使用Python和Bitly API将来自Yourls的缩短链接迁移到bit.ly

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python和Bitly API将来自Yourls的缩短链接迁移到bit.ly相关的知识,希望对你有一定的参考价值。

#! /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 = 'dest-domain.com'
bitly_token = 'TOKEN FROM BITLY'
yourls_signature = 'YOURLS SIGNATURE'

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)

以上是关于python 使用Python和Bitly API将来自Yourls的缩短链接迁移到bit.ly的主要内容,如果未能解决你的问题,请参考以下文章

如何使用httpclient c#调用bitly v4 api来缩短url?

PHP PHP Media API类(YouTube,Flickr,Vimeo,Bitly)

ActionScript 3 使用Bitly缩短URL

《利用python进行数据分析》学习笔记

golang使用Nsq

python系统学习:第五周之数据分析1