altnight (owner)

Revisions

gist: 2659391 Download_button fork
public
Description:
Twitterでアレな発言してる人の過去ログ3200件取得して表示するだけのスクリプト
Public Clone URL: git://gist.github.com/2659391.git
Embed All Files: show embed
arena_tl.py #
embed raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#-*- coding:utf-8 -*-
"""
TLを3200件なり指定しただけとってくる簡単なスクリプト

argv[1] type
1. tweet with OAuth
2. tweet with not OAuth
3. list
argv[2] page
argv[3] user
argv[4] list
"""
import sys
import re
import datetime
import codecs

import tweepy

CONSUMER_KEY = "your key"
CONSUMER_SECRET = "your key"
ACCESS_TOKEN = "your key"
ACCESS_TOKEN_SECRET = "your key"

def help():
    print __doc__
    sys.exit()

def expandTCO(arg):
    """t.coの展開"""
    text = arg.text
    for url in arg.entities["urls"]:
        if not url:
            continue
        else:
            for i in arg.entities["urls"]:
                if re.search(i["url"], text):
                    text = re.sub(i["url"], i["expanded_url"], text)
    return text

def set_type(_type, page):
    """tweetを取ってくる種類の設定"""
    if _type == '1': # tweet with OAuth
        api = set_OAuth()
        return api.user_timeline(sys.argv[3], include_entities=True, include_rts=True, count=200, page=page)
    elif _type == '2': # tweet with not OAuth
        return tweepy.api.user_timeline(sys.argv[3], include_entities=True, include_rts=True, count=200, page=page)
    elif _type == '3': # list
        api = set_OAuth()
        return api.list_timeline(sys.argv[3], sys.argv[4], include_entities=True, include_rts=True, count=20, page=page)
    else:
        raise Exception

def set_OAuth():
    """OAuthをとりつける"""
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    return api

def main():
    #help
    if sys.argv[1] == '-h':
        help()
    #main
    result = ''
    for i, page in enumerate(range(1, int(sys.argv[2])+1)):
        for ii, arg in enumerate(set_type(sys.argv[1], page)):
            #公式RTならargを入れ替える
            if hasattr(arg, "retweeted_status"):
                arg = arg.retweeted_status
            text = expandTCO(arg)
            result += u"page%d,%d %s:%s:%s\n" % (i+1 ,ii+1,
                                                (datetime.timedelta(hours=9) + arg.created_at).strftime("%Y/%m/%d %H:%m:%S"),
                                                arg.user.screen_name,
                                                text,
                                                )
    with codecs.open(sys.argv[3] + '_tweet.txt', 'w', 'utf-8') as f:
        f.write(result)

if __name__ == "__main__":
    main()
Please sign in to comment.