Created
March 24, 2016 14:37
-
-
Save oyakodon/dd1690ae710e74552834 to your computer and use it in GitHub Desktop.
ヤマト運輸の荷物を追跡して、得た情報をツイートする。 / Python, Tweepy
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
#! /usr/bin/env python2.7 | |
#coding: utf-8 | |
import json | |
import requests | |
import tweepy # pip install tweepy | |
KEY = "" | |
SECRET = "" | |
ATOKEN = "" | |
ASECRET = "" | |
auth = tweepy.OAuthHandler(KEY, SECRET) | |
auth.set_access_token(ATOKEN, ASECRET) | |
api = tweepy.API(auth) | |
def GET_status(number): | |
#参考:http://qiita.com/OvKNyRgir3BuEJj/items/195c002663b3bfcccacb | |
URL_JSON = 'http://nanoappli.com/tracking/api/%d.json' % number | |
r = requests.get(URL_JSON) | |
j = r.json() | |
itemType = j['itemType'] | |
stat = j['statusList'][-1] # 最新の配送状況を取得 | |
return [itemType, stat] | |
if __name__ == '__main__': | |
path = "{Path to JSON}" | |
f = open(path, 'r') # JSONファイルの読み込み | |
jsonData = json.load(f) # これで辞書型になるらしい | |
for slip in jsonData: # 伝票の数だけループ | |
i = jsonData[slip]['slipNo'] | |
itemType, stat = GET_status(i) # 商品名と配達状況をいただく | |
status = stat['status'] # 最新の配送状況 | |
date = stat['date'] # 日付 | |
time = stat['time'] # 時間 | |
placeName = stat['placeName'] # 場所(担当店名) | |
if jsonData[slip]['status'] != status: | |
# 更新された | |
prev_stat = jsonData[slip]['status'] | |
jsonData[slip]['status'] = status | |
status = "[" + prev_stat + "] => [" + status + "]" | |
tweet = "@{YOUR SCREEN NAME HERE}: \n" | |
tweet += u"【宅配便追跡くん】\n" | |
tweet += u"例のブツ(%s)の配送情報が更新されました!\n" % itemType | |
tweet += u"[荷物状況] %s\n" % status | |
tweet += u"[担当店] %s\n" % placeName | |
tweet += u"[日付・時刻] %s %s" % (date, time) | |
api.update_status(status=tweet) | |
# JSONファイル書き込み | |
text = json.dumps(jsonData, sort_keys=True, ensure_ascii=False, indent=2) | |
f = open(path, 'w') | |
f.write(text.encode("utf-8")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment