歩いたら休め

なんでこんな模様をしているのですか?

【Python】最近Twitterを見る暇がないので、Twitterのリストから最新ニュースのurlを簡単に取ってくるライブラリ(らしきもの)を作った

就職して1年も経つと多少は忙しくなってしまいます。つまり、しっかりと意識して自分自身の勉強や趣味の時間を確保する必要があります。

そのため、Twitterについても今までのようにだらだらとタイムラインを眺めるような使い方はできなくなり、 Twitter業務の効率化が求められていました。

そこで、

  1. Twitterのアクセストークンを設定し、
  2. Twitterのリストを指定する

だけでいい感じにそのリスト内でつぶやかれているニュース(URLとタイトル等の情報)を取ってくるライブラリを作りました。

あとはこの結果を多少整形(favorite_countsの最大値でソートとか、タイトルの全角半角を統一するとか、余計なURLを排除するとか)して、Slackなりメールなりで通知すれば、自分だけのニュースツールが完成です。

github.com

こんな感じで使います。

from twlist_to_urllist import *

# 自分のアカウントのtwitterアクセストークンを設定
your_twitter_api_config = {
    'token': 'your access token',
    'token_secret': 'your access token secret',
    'consumer_key': 'your consumer key',
    'consumer_secret': 'your consumer secret'
    }
client = urlReporter(**your_twitter_api_config)

# 欲しいリスト(ここでは'palafo/breakingnews')を指定
url_dicts = client.request_urls(
    owner_screen_name='palafo',
    slug='breakingnews',
    count=10,
    target_time=1459639872 # UNIXTIMEがこれ以前のツイートを除去する
    )

for x in url_dicts:
    print(x['url'], x['title'], x['favorite_counts'])

こんな結果が返ってきます。

Syria: ISIS dumped bodies in mass grave in Palmyra - CNN.com http://edition.cnn.com/2016/04/02/middleeast/syria-war-palmyra/index.html [66]
The Chinese bodies still being found in South Korea - BBC News http://www.bbc.co.uk/news/magazine-35943604?ocid=socialflow_twitter#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa [12]
Man held on $2M bail for Little Village slaying in 2014 - Chicago Tribune http://www.chicagotribune.com/news/local/breaking/ct-man-held-on-2m-bail-for-little-village-slaying-in-2014-20160402-story.html [0]
Der Spiegel: Germany Spying on Israeli Prime Minister's Office - Israel News - Haaretz  http://www.haaretz.com/israel-news/.premium-1.712287 [0]
Electoral Map Is a Reality Check to Donald Trump’s Bid - The New York Times http://www.nytimes.com/2016/04/03/us/politics/donald-trump-general-election.html?smid=tw-nytimes&smtyp=cur&_r=0 [0]
Government calls on councils to buy British steel in bid to save industry http://www.telegraph.co.uk/news/2016/04/02/government-calls-on-councils-to-buy-british-steel-in-bid-to-save/?utm_source=dlvr.it&utm_medium=twitter [0]
Travel booking scams see holidaymakers conned out of £12m http://www.telegraph.co.uk/news/2016/04/02/travel-booking-scams-see-holidaymakers-conned-out-of-12m/?utm_source=dlvr.it&utm_medium=twitter [0]
In a devastated Turkish town, teenagers dream of joining the Kurdish guerrillas
| World news | The Guardian http://www.theguardian.com/world/2016/apr/03/diyarbakir-kurdish-teenagers-dream-of-becoming-guerrillas [0]
 Transcript: Donald Trump interview with Bob Woodward and Robert Costa - The Washington Post https://www.washingtonpost.com/news/post-politics/wp/2016/04/02/transcript-donald-trump-interview-with-bob-woodward-and-robert-costa/ [13]
 Turkey rejects Amnesty claims over 'forcibly returning' Syrians https://www.yahoo.com/news/turkey-rejects-amnesty-claims-over-forcibly-returning-syrians-224617423.html [4]

以下の辞書が入ったジェネレーターが返ってきます。

{
    'url': ページのURL(短縮URLは展開済),
    'title': ページのタイトル,
    'body': ページのhtmlをBeautifulSoupにかけたオブジェクト,
    'favorite_counts': お気に入り数のリスト,
    'retweet_counts': リツイート数のリスト,
    'tweet_info': ツイートの情報のリスト
}

favorite_countsなどを操作することによって、「リツイート数の平均値が閾値以下のものは排除する」みたいな操作も簡単にできると思います。

また、内部で各URLにリクエストを送っているので、あまりに対象のURLが多い場合には、requests.getの前にsleepするような処理を挟むべきかもしれません。

もしかすると、文字コード周りでバグが残っているかもしれません。ちょっと使ってみてアレした場合はアレします。

PythonによるWebスクレイピング』がけっこう参考になりました。

PythonによるWebスクレイピング

PythonによるWebスクレイピング