X Tutup
Skip to content

Commit c1793e3

Browse files
committed
Merge branch 'GetStatuses' of https://github.com/BarraQDA/python-twitter into BarraQDA-GetStatuses
2 parents b692a9b + 826a618 commit c1793e3

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

testdata/get_statuses.1.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

testdata/get_statuses.2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"contributors": null, "truncated": false, "text": "just setting up my twttr", "is_quote_status": false, "in_reply_to_status_id": null, "id": 20, "favorite_count": 76282, "source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 103721, "id_str": "20", "favorited": false, "user": {"follow_request_sent": false, "has_extended_profile": true, "profile_use_background_image": true, "default_profile_image": false, "id": 12, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme7/bg.gif", "verified": true, "translator_type": "regular", "profile_text_color": "333333", "profile_image_url_https": "https://pbs.twimg.com/profile_images/839863609345794048/mkpdB9Tf_normal.jpg", "profile_sidebar_fill_color": "F3F3F3", "entities": {"description": {"urls": []}}, "followers_count": 4039047, "profile_sidebar_border_color": "DFDFDF", "id_str": "12", "profile_background_color": "EBEBEB", "listed_count": 27178, "is_translation_enabled": false, "utc_offset": -25200, "statuses_count": 21829, "description": "", "friends_count": 2696, "location": "California, USA", "profile_link_color": "990000", "profile_image_url": "http://pbs.twimg.com/profile_images/839863609345794048/mkpdB9Tf_normal.jpg", "following": false, "geo_enabled": true, "profile_banner_url": "https://pbs.twimg.com/profile_banners/12/1483046077", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme7/bg.gif", "screen_name": "jack", "lang": "en", "profile_background_tile": false, "favourites_count": 16961, "name": "jack", "notifications": false, "url": null, "created_at": "Tue Mar 21 20:50:14 +0000 2006", "contributors_enabled": false, "time_zone": "Pacific Time (US & Canada)", "protected": false, "default_profile": false, "is_translator": false}, "geo": null, "in_reply_to_user_id_str": null, "lang": "en", "created_at": "Tue Mar 21 20:50:14 +0000 2006", "in_reply_to_status_id_str": null, "place": null}]

testdata/get_statuses.ids.txt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
724
2+
701
3+
691
4+
690
5+
689
6+
688
7+
687
8+
686
9+
685
10+
674
11+
673
12+
668
13+
662
14+
660
15+
654
16+
651
17+
650
18+
643
19+
637
20+
636
21+
634
22+
623
23+
614
24+
596
25+
594
26+
593
27+
592
28+
581
29+
579
30+
567
31+
566
32+
564
33+
555
34+
544
35+
541
36+
540
37+
538
38+
536
39+
517
40+
507
41+
501
42+
495
43+
493
44+
487
45+
477
46+
475
47+
465
48+
463
49+
453
50+
447
51+
443
52+
438
53+
435
54+
432
55+
397
56+
387
57+
376
58+
364
59+
356
60+
348
61+
346
62+
342
63+
323
64+
317
65+
314
66+
305
67+
302
68+
291
69+
289
70+
287
71+
267
72+
251
73+
247
74+
245
75+
243
76+
240
77+
239
78+
222
79+
198
80+
176
81+
174
82+
170
83+
166
84+
163
85+
155
86+
153
87+
152
88+
137
89+
132
90+
129
91+
117
92+
113
93+
101
94+
92
95+
89
96+
81
97+
62
98+
51
99+
35
100+
29
101+
20

testdata/get_statuses.map.1.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

testdata/get_statuses.map.2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id": {"20": {"contributors": null, "truncated": false, "text": "just setting up my twttr", "is_quote_status": false, "in_reply_to_status_id": null, "id": 20, "favorite_count": 76284, "source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 103722, "id_str": "20", "favorited": false, "user": {"follow_request_sent": false, "has_extended_profile": true, "profile_use_background_image": true, "default_profile_image": false, "id": 12, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme7/bg.gif", "verified": true, "translator_type": "regular", "profile_text_color": "333333", "profile_image_url_https": "https://pbs.twimg.com/profile_images/839863609345794048/mkpdB9Tf_normal.jpg", "profile_sidebar_fill_color": "F3F3F3", "entities": {"description": {"urls": []}}, "followers_count": 4039068, "profile_sidebar_border_color": "DFDFDF", "id_str": "12", "profile_background_color": "EBEBEB", "listed_count": 27172, "is_translation_enabled": false, "utc_offset": -25200, "statuses_count": 21829, "description": "", "friends_count": 2696, "location": "California, USA", "profile_link_color": "990000", "profile_image_url": "http://pbs.twimg.com/profile_images/839863609345794048/mkpdB9Tf_normal.jpg", "following": false, "geo_enabled": true, "profile_banner_url": "https://pbs.twimg.com/profile_banners/12/1483046077", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme7/bg.gif", "screen_name": "jack", "lang": "en", "profile_background_tile": false, "favourites_count": 16961, "name": "jack", "notifications": false, "url": null, "created_at": "Tue Mar 21 20:50:14 +0000 2006", "contributors_enabled": false, "time_zone": "Pacific Time (US & Canada)", "protected": false, "default_profile": false, "is_translator": false}, "geo": null, "in_reply_to_user_id_str": null, "lang": "en", "created_at": "Tue Mar 21 20:50:14 +0000 2006", "in_reply_to_status_id_str": null, "place": null}}}

tests/test_api_30.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,53 @@ def testGetStatusExtraParams(self):
12781278
resp = self.api.GetStatus(status_id=397, trim_user=True, include_entities=False)
12791279
self.assertFalse(resp.user.screen_name)
12801280

1281+
@responses.activate
1282+
def testGetStatuses(self):
1283+
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
1284+
with open('testdata/get_statuses.1.json') as f:
1285+
resp_data = f.read()
1286+
rsps.add(GET, DEFAULT_URL, body=resp_data)
1287+
with open('testdata/get_statuses.2.json') as f:
1288+
resp_data = f.read()
1289+
rsps.add(GET, DEFAULT_URL, body=resp_data)
1290+
1291+
with open('testdata/get_statuses.ids.txt') as f:
1292+
status_ids = [int(l) for l in f]
1293+
1294+
resp = self.api.GetStatuses(status_ids)
1295+
1296+
self.assertTrue(type(resp) is list)
1297+
print(resp)
1298+
self.assertEqual(set(respitem.id for respitem in resp), set(status_ids))
1299+
self.assertFalse(resp != resp)
1300+
1301+
self.assertRaises(
1302+
twitter.TwitterError,
1303+
lambda: self.api.GetStatuses(['test']))
1304+
1305+
@responses.activate
1306+
def testGetStatusesMap(self):
1307+
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
1308+
with open('testdata/get_statuses.map.1.json') as f:
1309+
resp_data = f.read()
1310+
rsps.add(GET, DEFAULT_URL, body=resp_data)
1311+
with open('testdata/get_statuses.map.2.json') as f:
1312+
resp_data = f.read()
1313+
rsps.add(GET, DEFAULT_URL, body=resp_data)
1314+
1315+
with open('testdata/get_statuses.ids.txt') as f:
1316+
status_ids = [int(l) for l in f]
1317+
1318+
resp = self.api.GetStatuses(status_ids, map=True)
1319+
1320+
self.assertTrue(type(resp) is dict)
1321+
self.assertTrue(all([resp.get(status_id) for status_id in status_ids]))
1322+
self.assertFalse(resp != resp)
1323+
1324+
self.assertRaises(
1325+
twitter.TwitterError,
1326+
lambda: self.api.GetStatuses(['test'], map=True))
1327+
12811328
@responses.activate
12821329
def testGetStatusOembed(self):
12831330
with open('testdata/get_status_oembed.json') as f:

twitter/api.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class Api(object):
127127
>>> api.GetUserTimeline(user)
128128
>>> api.GetHomeTimeline()
129129
>>> api.GetStatus(status_id)
130+
>>> def GetStatuses(status_ids)
130131
>>> api.DestroyStatus(status_id)
131132
>>> api.GetFriends(user)
132133
>>> api.GetFollowers()
@@ -852,6 +853,62 @@ def GetStatus(self,
852853

853854
return Status.NewFromJsonDict(data)
854855

856+
def GetStatuses(self,
857+
status_ids,
858+
trim_user=False,
859+
include_entities=True,
860+
map=False):
861+
"""Returns a list of status messages, specified by the status_ids parameter.
862+
863+
Args:
864+
status_ids:
865+
A list of the numeric ID of the statuses you are trying to retrieve.
866+
trim_user:
867+
When set to True, each tweet returned in a timeline will include
868+
a user object including only the status authors numerical ID.
869+
Omit this parameter to receive the complete user object. [Optional]
870+
include_entities:
871+
If False, the entities node will be disincluded.
872+
This node offers a variety of metadata about the tweet in a
873+
discreet structure, including: user_mentions, urls, and
874+
hashtags. [Optional]
875+
map:
876+
If True, returns a dictionary with status id as key and returned
877+
status data (or None if tweet does not exist or is inaccessible)
878+
as value. Otherwise returns an unordered list of successfully
879+
retrieved Tweets. [Optional]
880+
Returns:
881+
A dictionary or unordered list (depending on the parameter 'map') of
882+
twitter Status instances representing the status messages.
883+
"""
884+
url = '%s/statuses/lookup.json' % (self.base_url)
885+
886+
map = enf_type('map', bool, map)
887+
888+
if map:
889+
result = {}
890+
else:
891+
result = []
892+
offset = 0
893+
parameters = {
894+
'trim_user': enf_type('trim_user', bool, trim_user),
895+
'include_entities': enf_type('include_entities', bool, include_entities),
896+
'map': map
897+
}
898+
while offset < len(status_ids):
899+
parameters['id'] = ','.join([str(enf_type('status_id', int, status_id)) for status_id in status_ids[offset:offset+100]])
900+
901+
resp = self._RequestUrl(url, 'GET', data=parameters)
902+
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
903+
if map:
904+
result.update({int(key):(Status.NewFromJsonDict(value) if value else None) for key,value in data['id'].items()})
905+
else:
906+
result += [Status.NewFromJsonDict(dataitem) for dataitem in data]
907+
908+
offset += 100
909+
910+
return result
911+
855912
def GetStatusOembed(self,
856913
status_id=None,
857914
url=None,

0 commit comments

Comments
 (0)
X Tutup