|
51 | 51 | auth=noauth, |
52 | 52 | api_version='1.1') |
53 | 53 |
|
| 54 | +twitter11_stream = TwitterStream(domain='stream.twitter.com', |
| 55 | + auth=oauth, |
| 56 | + api_version='1.1') |
| 57 | + |
54 | 58 | twitter2 = Twitter(domain='api.twitter.com', |
55 | 59 | auth=oauth, |
56 | 60 | api_version='2', |
|
61 | 65 | api_version='2', |
62 | 66 | format='') |
63 | 67 |
|
| 68 | +twitter2_stream = TwitterStream2(domain='api.twitter.com', |
| 69 | + auth=oauth2, |
| 70 | + api_version='2', |
| 71 | + format='') |
| 72 | + |
64 | 73 | AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
65 | 74 |
|
66 | 75 | b64_image_data = b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94JFhMBAJv5kaUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAA4UlEQVQoz7WSIZLGIAxG6c5OFZjianBcIOfgPkju1DsEBWfAUEcNGGpY8Xe7dDoVFRvHfO8NJGRorZE39UVe1nd/WNfVObcsi3OOEAIASikAmOf5D2q/FWPUWgshKKWfiFIqhNBaxxhPjPQ05/z+Bs557xw9hBC89ymlu5BS8t6HEC5NW2sR8alRRLTWXoRSSinlSejT12M9BAAAgCeoTw9BSimlfBIu6WdYtVZEVErdaaUUItZaL/9wOsaY83YAMMb0dGtt6Jdv3/ec87ZtOWdCCGNsmibG2DiOJzP8+7b+AAOmsiPxyHWCAAAAAElFTkSuQmCC" |
@@ -185,6 +194,54 @@ def test_search(): |
185 | 194 | assert results |
186 | 195 |
|
187 | 196 |
|
| 197 | +def test_stream(): |
| 198 | + # Sometimes Twitter matches tweets with keywords from urls or users |
| 199 | + # which are more complex to check so we allow a few mismatches |
| 200 | + mismatch = 0 |
| 201 | + for tweet in twitter11_stream.statuses.filter(track="gaga,bieber", filter_level='none', stall_warnings='true'): |
| 202 | + if "timeout" in tweet: |
| 203 | + continue |
| 204 | + assert "text" in tweet |
| 205 | + lowtext = tweet["text"].lower() |
| 206 | + if "gaga" in lowtext or "bieber" in lowtext: |
| 207 | + break |
| 208 | + mismatch += 1 |
| 209 | + if mismatch > 5: |
| 210 | + assert "gaga" in lowtext or "bieber" in lowtext |
| 211 | + |
| 212 | + |
| 213 | +def test_stream_v2(): |
| 214 | + rules = twitter2_app.tweets.search.stream.rules() |
| 215 | + assert "data" in rules and "meta" in rules and len(rules["data"]) == rules["meta"].get("result_count", 0) |
| 216 | + |
| 217 | + if len(rules["data"]): |
| 218 | + remove = twitter2_app.tweets.search.stream.rules( |
| 219 | + _json={"delete": {"ids": [rule["id"] for rule in rules["data"]]}} |
| 220 | + ) |
| 221 | + assert "meta" in remove and "summary" in remove["meta"] and remove["meta"]["summary"].get("deleted", 0) == len(rules["data"]) |
| 222 | + |
| 223 | + add = twitter2_app.tweets.search.stream.rules( |
| 224 | + _json={"add": [{"value": '"gaga"'}, {"value": '"bieber"'}]} |
| 225 | + ) |
| 226 | + assert "meta" in add and "summary" in add["meta"] and add["meta"]["summary"].get("created", 0) == 2 |
| 227 | + assert "data" in add and len(add["data"]) == 2 |
| 228 | + |
| 229 | + # Sometimes Twitter matches tweets with keywords from urls or users |
| 230 | + # which are more complex to check so we allow a few mismatches |
| 231 | + mismatch = 0 |
| 232 | + for tweet in twitter2_stream.tweets.search.stream( |
| 233 | + expansions="referenced_tweets.id", |
| 234 | + params={"tweet.fields": "referenced_tweets,text"} |
| 235 | + ): |
| 236 | + assert "data" in tweet and "text" in tweet["data"] |
| 237 | + lowtext = tweet["data"]["text"].lower() |
| 238 | + if "gaga" in lowtext or "bieber" in lowtext: |
| 239 | + break |
| 240 | + mismatch += 1 |
| 241 | + if mismatch > 5: |
| 242 | + assert "gaga" in lowtext or "bieber" in lowtext |
| 243 | + |
| 244 | + |
188 | 245 | def test_get_trends(): |
189 | 246 | # This is one method of inserting parameters, using named |
190 | 247 | # underscore params. |
|
0 commit comments