-
Notifications
You must be signed in to change notification settings - Fork 9
/
get_tweet
executable file
·125 lines (116 loc) · 4.12 KB
/
get_tweet
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
"""Helper program for dumping details of real tweets
Use this to generate test cases from the actual representation of tweets from the twitter API"""
import argparse
import pprint
from textwrap import dedent
from urllib.parse import urlparse
from pathlib import Path
from Externals import set_arguments, setup_network
def print_tweet_details(tw, targetfile):
quoted_status_id = None
ext = ''
if 'quoted_status_id' in tw.__dict__:
quoted_status_id = tw.quoted_status_id
if 'extended_entities' in tw.__dict__:
ext = 'extended_entities=\n{},'.format(pp.pformat(tw.extended_entities))
print(dedent('''\
list_of_tweets.append(TweepyMock(
full_text={},
id={},
display_text_range={},
in_reply_to_user_id={},
in_reply_to_status_id={},
in_reply_to_screen_name={},
quoted_status_id={},
entities=
{},
{}
user=User(
screen_name={},
name={},
id={},
follows={}
)))
''').format(
repr(tw.full_text),
repr(tw.id),
pp.pformat(tw.display_text_range),
repr(tw.in_reply_to_user_id),
repr(tw.in_reply_to_status_id),
repr(tw.in_reply_to_screen_name),
repr(quoted_status_id),
pp.pformat(tw.entities),
ext,
repr(tw.user.screen_name),
repr(tw.user.name),
repr(tw.user.id),
repr(twapi.is_followed(tw.user))
), file=targetfile)
parser = argparse.ArgumentParser(description=__doc__)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--id',
dest='id',
help='ID of the tweet that will be downloaded',
type=int,
required=False,
action='store'
)
group.add_argument('--url',
dest='url',
help='URL of the tweet that will be downloaded',
type=str,
required=False,
action='store'
)
parser.add_argument('--mode',
dest='mode',
choices=['dump', 'mock', 'both'],
help=dedent('''\
dump: Just dump the tweet and be done with it.
mock: Prepare TweepyMock objects from this tweet and
referenced tweets that can be used in test cases.
'''),
required=True,
default='mock',
action='store'
)
set_arguments(parser)
args = parser.parse_args()
do_mock = args.mode == 'mock'
do_dump = args.mode == 'dump'
if args.mode == 'both':
do_mock = True
do_dump = True
twapi = setup_network('twitter', args, {})
tid = args.id
if tid is None:
try:
tid = int(Path(urlparse(args.url).path).name)
except ValueError:
parser.error("Cannot extract tweet id from URL {}".format(args.url))
tweepy_tweet = twapi.get_status(tid)
pp = pprint.PrettyPrinter(indent=2, width=80)
if do_mock:
with open('tweet_details.py', 'w', encoding='utf-8') as target:
print(dedent('''\
import datetime
from Mock import TweepyMock, User
list_of_tweets = []
'''), file=target)
print_tweet_details(tweepy_tweet, target)
if tweepy_tweet.in_reply_to_status_id is not None:
replied_to_tweet = twapi.get_status(tweepy_tweet.in_reply_to_status_id)
print_tweet_details(replied_to_tweet, target)
if 'quoted_status_id' in tweepy_tweet.__dict__:
if tweepy_tweet.quoted_status_id is not None:
quoted_tweet = twapi.get_status(tweepy_tweet.quoted_status_id)
print_tweet_details(quoted_tweet, target)
if do_dump:
# promote inner objects so that pretty print also prints them
for key in ['user', 'author']:
tweepy_tweet.__dict__[key] = tweepy_tweet.__dict__[key].__dict__['_json']
del tweepy_tweet.__dict__['_api']
del tweepy_tweet.__dict__['_json']
print("tweetdict = ", end='')
pp.pprint(tweepy_tweet.__dict__)