Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
リスト内のURLを一気に短縮化するためのスクリプト
# coding:utf-8
import sys
import re
from urllib2 import urlopen as U, Request as R
from json import loads as J
API_KEY = "ここはGoogleのAPIを取得して使う"
API = "https://www.googleapis.com/urlshortener/v1/url?key=" + API_KEY
# ここに短縮化したいURLをリスト形式で置いておく
URLarray = ['http://google.com','http://yahoo.co.jp']
for URL in URLarray:
if re.match('http://goo\.gl/.+', URL):
print J(U(API + '?shortUrl=%s' % URL).read())['longUrl']
else:
print J(U(R(API, '{"longUrl":"%s"}' % URL, {'Content-Type': 'application/json'})).read())['id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.