Skip to content

Instantly share code, notes, and snippets.

@takuya-andou
Created August 28, 2017 06:11
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 takuya-andou/1f41829de43cbe6c98d0d8ba5dec4eca to your computer and use it in GitHub Desktop.
Save takuya-andou/1f41829de43cbe6c98d0d8ba5dec4eca to your computer and use it in GitHub Desktop.
リスト内の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