* さくらインターネットでも使えるようになった,半日分ぐらいの時間を掛けて作成した,ツイートの全文を取得するpython3のスクリプト

:CATEGORIES: パソコン,python,TwitterAPI

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json, config
from requests_oauthlib import OAuth1Session
import re
import sys
from datetime import datetime
from datetime import timezone  

args = sys.argv
if len(sys.argv) == 1:
    user = 'kk_hirono'
else:
    user = args[1]

if not len(sys.argv) == 2:
    offset = 1
else:
    offset = args[2]

#def convert_to_datetime(datetime_str):
  #tweet_datetime = datetime.strptime(datetime_str,'%a %b %d %H:%M:%S %z %Y')
  #return(tweet_datetime)

CK="Consumer Key"
CS="Consumer Secret"
AT="Access Token"
ATS="Access Token Secret"
twitter = OAuth1Session(CK, CS, AT, ATS)
url = "https://api.twitter.com/1.1/statuses/user_timeline.json" #タイムライン取得エンドポイント

params ={
  'count' : 200, # 取得数
  'page' : {offset},
 # 'trim_user' : True, # ユーザー情報を除く
  #'exclude_replies' : True, # リプライを除く
  'tweet_mode' : 'extended', # 拡張モード
  'screen_name' : {user}
}
res = twitter.get(url, params = params)

if res.status_code == 200:
    timelines = json.loads(res.text)
    #timelines = timelines.reverse()
    for line in reversed(timelines): #JSONの並び替え(逆順)
        if  line['retweeted'] or re.compile("^RT @").search(line['full_text']):
            user = line['user']['screen_name']
            name = line['user']['name']
            #org_text = re.sub('\A', '> ', line['full_text'])
            org_text = re.sub("^", '> ',  line['full_text'], flags=re.MULTILINE) #すべての行頭を置換
            #org_text = line['full_text'].replace("^", "> ")
            rt_user = line['retweeted_status']['user']['screen_name']
            rt_name = line['retweeted_status']['user']['name']
            tw_date = datetime.strptime(line['created_at'], '%a %b %d %H:%M:%S %z %Y').replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%Y-%m-%d %H:%M:%S')
            rt_date = datetime.strptime(line['retweeted_status']['created_at'], '%a %b %d %H:%M:%S %z %Y').replace(tzinfo=timezone.utc).astimezone(tz=None).strftime('%Y-%m-%d %H:%M:%S')
            tw_url =  "https://twitter.com/%s/status/#%s" % (user, line['id_str'])
            rt_url =  "https://twitter.com/%s/status/#%s" % (rt_user, line['retweeted_status']['id_str'])
            
            print("RT %s(%s)|%s(%s) 日時:%s/%s URL: %s %s\n%s\n" % (user, name, rt_user, rt_name,tw_date, rt_date, tw_url, rt_url, org_text))
            #print("RT {user}({name})|{rt_user}({rt_name}) 日時:{tw_date}/{rt_date} URL: {tw_url} {rt_url}\n> {org_tweet}\n\n")
            #print(line)
        else:    
            if not re.compile("^( - |- |> |[source:]|RT |TW )").search(line['full_text']):
                print(" ", end="")
            print(re.sub('>', '>', line['full_text'])) # ツイートの最初の行を見出しにして、 Markdown で良い感じにマークアップ
            print("")
            if 'media' in line['entities'].keys():
                for media in line['entities']['media']:
                    print("![image]("+media['media_url_https']+')' + "\n") # 画像URLをMarkdownの埋め込み形式に
                #print("\n")
else:
    print("Failed: %d" % res.status_code)

 次が参考にしたページですが,手間取って時間も掛かりました。

▶▶▶ kk_hironoのリツイート ▶▶▶

> Python入門|re.sub を使用して正規表現で「行頭」「行末」を置換する|dot blog https://t.co/0pjrNHMDLy text_new = re.sub('^', '「', text, flags=re.MULTILINE)

▶▶▶ kk_hironoのリツイート ▶▶▶

> 文字列操作の比較表: Ruby, Python, JavaScript, Perl, C++ - bkブログ https://t.co/5ob5vHj65w

▶▶▶ kk_hironoのリツイート ▶▶▶

> pythonTwitterの日付からPython datetime date https://t.co/HQBSadBoaz from datetime import timezone datetime.strptime(mydata["created_at"], '%a %b %d %H:%M:%S %z %Y').replace( tzinfo=timezone.utc).astimezone(tz=None).strftime('%Y-%m-%d %H:%M:%S'))

▶▶▶ kk_hironoのリツイート ▶▶▶

> #Twitter Timeline #API で得られる created_at の時刻を元に unixtimestamp 基準で絞り込む #python スクリプトの例 - Qiita https://t.co/qsFR7O0C21

▶▶▶ kk_hironoのリツイート ▶▶▶

> Twitterでいいね(かRT)した画像をひたすら保存する - Qiita https://t.co/4JuPf3bqQd

▶▶▶ kk_hironoのリツイート ▶▶▶

> 文字列中の変数展開(およびヒア・ドキュメント) - Qiita https://t.co/Buaxc4smv5

▶▶▶ kk_hironoのリツイート ▶▶▶

> Pythonの文字列における変数展開を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン https://t.co/1rbndFF0yE

▶▶▶ kk_hironoのリツイート ▶▶▶

> 正規表現でif文を書くとき - Qiita https://t.co/OdaFvnJf8L

▶▶▶ kk_hironoのリツイート ▶▶▶

> Python3のprintで改行しない方法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン https://t.co/2nF7c0dLeN

▶▶▶ kk_hironoのリツイート ▶▶▶

> Python リストの逆順 - Qiita https://t.co/p7xspAvoxV reversedメソッドの使用

▶▶▶ kk_hironoのリツイート ▶▶▶

> 【Twitter API】タイムライン取得に使えるパラメータ集 | 無能投資家の苦悩 https://t.co/ZOQfwL6Pxd

▶▶▶ kk_hironoのリツイート ▶▶▶

> Pythonで文字列を置換(replace, translate, re.sub, re.subn) | https://t.co/y8h83ipByj https://t.co/Bf9Z2bOJHO

▶▶▶ kk_hironoのリツイート ▶▶▶

> twitter APIで遊んでみる #2(ユーザータイムラインの取得) - Qiita https://t.co/BqgzL6OF00