#python で #Twitter #API を叩き、日本時間で日付を指定して、タイムラインから時間順に #Markdown を作る
python で #Twitter #API を叩き、日本時間で日付を指定して、タイムラインから時間順に #Markdown を作る
# https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html import json, config, os, re from requests_oauthlib import OAuth1Session import pytz import time import datetime CK = config.CONSUMER_KEY CS = config.CONSUMER_SECRET AT = config.ACCESS_TOKEN ATS = config.ACCESS_TOKEN_SECRET twitter = OAuth1Session(CK, CS, AT, ATS) url = "https://api.twitter.com/1.1/statuses/user_timeline.json" params ={ 'count' : os.environ.get('COUNT') or 1000, 'trim_user' : True, 'exclude_replies' : True, 'tweet_mode' : 'extended' } res = twitter.get(url, params = params) if res.status_code != 200: print("Failed: %d" % res.status_code) exit() def jst_ymd(at): t = time.strptime(at,'%a %b %d %H:%M:%S +0000 %Y') utc = pytz.timezone('UTC') d = datetime.datetime(*t[:6], tzinfo=utc) tm = d.astimezone(pytz.timezone('Asia/Tokyo')) zn = tm.strftime('%Y-%m-%d %H:%M:%S %Z%z') ymd = tm.strftime('%Y-%m-%d') return(ymd) timelines = json.loads(res.text) filter_date = os.environ['DATE'] results = [] for line in timelines: jst_date = jst_ymd(line['created_at']) if jst_date != filter_date: continue text = re.sub(r'https://t\.co/\w+', '' ,line['full_text']) # リンク先URL的なのを削除 text = re.sub(r'#', '' , text) # ハッシュタグを削除 text = '# ' + text text += '<a href="https://twitter.com/YumaInaura/status/' + str(line['id']) + '">Tweet</a>' if 'media' in line['entities'].keys(): for media in line['entities']['media']: text += "\n" text += "![image]("+media['media_url_https']+')' text += "\n" results.append(text) results.reverse() for result in results: print(result)
Example
$ COUNT=5 DATE=2019-02-11 python timeline.py
# プログラミング言語 nomikai 飲み会に行くべきか、行かざるべきか。 自分で決める。以上。 コマンド? 飲み会は道具なので、使えるスキルがあるなら行けば良い。 無邪気に楽しめるなら、迷う余地なし。 社内評価を上げる、そんな下心だけで、成果もないならやめてしまえ。 エンジニア <a href="https://twitter.com/YumaInaura/status/1094855919001718785">Tweet</a> # 天空のプログラマ。 プログラミングに必要なのは頭の良さじゃない。体力。エネルギーだ。 頭の良いタービンがいくら回ろうと、ガス欠では、車は進まない。 僕らは生身の物理サーバなので、クラウドの上まで飛べない。 自動車に学び、エネルギー管理の天才になろう。 地上で。 エンジニア<a href="https://twitter.com/YumaInaura/status/1094858090841657344">Tweet</a> # プログラミングとマインドフルネス。 マインドフルネス の技術で呼吸を整える。 心理療法も。セルフコンパッション。自分に信じられないほど優しくする。 同時にキーボードを打つ。まだ練習中。 プログラミングは莫大なエネルギーが必要。ストレスも高い。 だから命を保つ技術。 エンジニア<a href="https://twitter.com/YumaInaura/status/1094860778476232706">Tweet</a>
# Ref