Created
August 28, 2017 06:11
-
-
Save takuya-andou/1f41829de43cbe6c98d0d8ba5dec4eca to your computer and use it in GitHub Desktop.
リスト内のURLを一気に短縮化するためのスクリプト
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
# 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