GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — twitter/README at master · python-twitter-tools/twitter
This repository was archived by the owner on Feb 10, 2025. It is now read-only.
Expand file tree/ Copy pathREADMEMore file actions 373 lines (267 loc) · 12 KB / Copy pathREADMEFile metadata and controls373 lines (267 loc) · 12 KB 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 Python Twitter Tools ==================== > [!IMPORTANT] > This project is no longer maintained and has been archived. > The existing releases and code remain available in their current state. > Thank you to everyone who used and contributed to the success of this project. The Minimalist Twitter API for Python is a Python API for Twitter, everyone's favorite Web 2.0 Facebook-style status updater for people on the go. Also included is a Twitter command-line tool for getting your friends' tweets and setting your own tweet from the safety and security of your favorite shell and an IRC bot that can announce Twitter updates to an IRC channel. For more information: * install the [package](https://pypi.org/project/twitter/) `pip install twitter` * import the `twitter` package and run `help()` on it * run `twitter -h` for command-line tool help twitter - The Command-Line Tool ------------------------------- The command-line tool lets you do some awesome things: * view your tweets, recent replies, and tweets in lists * view the public timeline * follow and unfollow (leave) friends * various output formats for tweet information The bottom line: type `twitter`, receive tweets. twitterbot - The IRC Bot ------------------------ The IRC bot is associated with a Twitter account (either your own account or an account you create for the bot). The bot announces all tweets from friends it is following. It can be made to follow or leave friends through IRC /msg commands. `twitter-log` ------------- `twitter-log` is a simple command-line tool that dumps all public tweets from a given user in a simple text format. It is useful to get a complete offsite backup of all your tweets. Run `twitter-log` and read the instructions. `twitter-archiver` and `twitter-follow` --------------------------------------- twitter-archiver will log all the tweets posted by any user since they started posting. twitter-follow will print a list of all of all the followers of a user (or all the users that user follows). Programming with the Twitter API classes ======================================== The `Twitter` and `TwitterStream` classes are the key to building your own Twitter-enabled applications. The `Twitter` class ------------------- The minimalist yet fully featured Twitter API class. Get RESTful data by accessing members of this class. The result is decoded python objects (lists and dicts). The Twitter API is documented at: **[https://developer.twitter.com/en/docs](https://developer.twitter.com/en/docs)** The list of most accessible functions is listed at: **[https://developer.twitter.com/en/docs/api-reference-index](https://developer.twitter.com/en/docs/api-reference-index)** Examples: ```python from twitter import * t = Twitter( auth=OAuth(token, token_secret, consumer_key, consumer_secret)) # Get your "home" timeline t.statuses.home_timeline() # Get a particular friend's timeline t.statuses.user_timeline(screen_name="boogheta") # to pass in GET/POST parameters, such as `count` t.statuses.home_timeline(count=5) # to pass in the GET/POST parameter `id` you need to use `_id` t.statuses.show(_id=1234567890) # Update your status t.statuses.update( status="Using @boogheta's sweet Python Twitter Tools.") # Send a direct message t.direct_messages.events.new( _json={ "event": { "type": "message_create", "message_create": { "target": { "recipient_id": t.users.show(screen_name="boogheta")["id"]}, "message_data": { "text": "I think yer swell!"}}}}) # Get the members of maxmunnecke's list "network analysis tools" (grab the list_id within the url) https://twitter.com/i/lists/1130857490764091392 t.lists.members(owner_screen_name="maxmunnecke", list_id="1130857490764091392") # Favorite/like a status status = t.statuses.home_timeline()[0] if not status['favorited']: t.favorites.create(_id=status['id']) # An *optional* `_timeout` parameter can also be used for API # calls which take much more time than normal or twitter stops # responding for some reason: t.users.lookup( screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1) # Overriding Method: GET/POST # you should not need to use this method as this library properly # detects whether GET or POST should be used, Nevertheless # to force a particular method, use `_method` t.statuses.oembed(_id=1234567890, _method='GET') # Send images along with your tweets: # - first just read images from the web or from files the regular way: with open("example.png", "rb") as imagefile: imagedata = imagefile.read() # - then upload medias one by one on Twitter's dedicated server # and collect each one's id: t_upload = Twitter(domain='upload.twitter.com', auth=OAuth(token, token_secret, consumer_key, consumer_secret)) id_img1 = t_upload.media.upload(media=imagedata)["media_id_string"] id_img2 = t_upload.media.upload(media=imagedata)["media_id_string"] # - finally send your tweet with the list of media ids: t.statuses.update(status="PTT ★", media_ids=",".join([id_img1, id_img2])) # Or send a tweet with an image (or set a logo/banner similarly) # using the old deprecated method that will probably disappear some day params = {"media[]": imagedata, "status": "PTT ★"} # Or for an image encoded as base64: params = {"media[]": base64_image, "status": "PTT ★", "_base64": True} t.statuses.update_with_media(**params) # Attach text metadata to medias sent, using the upload.twitter.com route # using the _json workaround to send json arguments as POST body # (warning: to be done before attaching the media to a tweet) t_upload.media.metadata.create(_json={ "media_id": id_img1, "alt_text": { "text": "metadata generated via PTT!" } }) # or with the shortcut arguments ("alt_text" and "text" work): t_upload.media.metadata.create(media_id=id_img1, text="metadata generated via PTT!") # Alternatively, you can reuse the originally instantiated object, # changing the domain, that is: t.domain = 'upload.twitter.com' # Now you can upload the image (or images). id_img1 = t.media.upload(media=imagedata)['media_id_string'] id_img2 = t.media.upload(media=imagedata)["media_id_string"] # You now can reset the domain to the original one: t.domain = 'api.twitter.com' # And you can send the update: t.statuses.update(status="PTT ★", media_ids=",".join([id_img1, id_img2])) ``` Searching Twitter: ```python # Search for the latest tweets about #pycon t.search.tweets(q="#pycon") # Search for the latest tweets about #pycon, using [extended mode](https://developer.twitter.com/en/docs/tweets/tweet-updates) t.search.tweets(q="#pycon", tweet_mode='extended') ``` Retrying after reaching the API rate limit ------------------------------------------ Simply create the `Twitter` instance with the argument `retry=True`, then the HTTP error codes `429`, `502`, `503`, and `504` will cause a retry of the last request. If `retry` is an integer, it defines the maximum number of retry attempts. Using the data returned ----------------------- Twitter API calls return decoded JSON. This is converted into a bunch of Python lists, dicts, ints, and strings. For example: ```python x = twitter.statuses.home_timeline() # The first 'tweet' in the timeline x[0] # The screen name of the user who wrote the first 'tweet' x[0]['user']['screen_name'] ``` Getting raw XML data -------------------- If you prefer to get your Twitter data in XML format, pass `format="xml"` to the `Twitter` object when you instantiate it: ```python twitter = Twitter(format="xml") ``` The output will not be parsed in any way. It will be a raw string of XML. The `TwitterStream` class ------------------------- The `TwitterStream` object is an interface to the Twitter Stream API. This can be used pretty much the same as the `Twitter` class, except the result of calling a method will be an iterator that yields objects decoded from the stream. For example:: ```python twitter_stream = TwitterStream(auth=OAuth(...)) iterator = twitter_stream.statuses.sample() for tweet in iterator: ...do something with this tweet... ``` Per default the `TwitterStream` object uses [public streams](https://dev.twitter.com/docs/streaming-apis/streams/public). If you want to use one of the other [streaming APIs](https://dev.twitter.com/docs/streaming-apis), specify the URL manually. The iterator will `yield` until the TCP connection breaks. When the connection breaks, the iterator yields `{'hangup': True}` (and raises `StopIteration` if iterated again). Similarly, if the stream does not produce heartbeats for more than 90 seconds, the iterator yields `{'hangup': True, 'heartbeat_timeout': True}` (and raises `StopIteration` if iterated again). The `timeout` parameter controls the maximum time between yields. If it is nonzero, then the iterator will yield either stream data or `{'timeout': True}` within the timeout period. This is useful if you want your program to do other stuff in between waiting for tweets. The `block` parameter sets the stream to be fully non-blocking. In this mode, the iterator always yields immediately. It returns stream data, or `None`. Note that `timeout` supercedes this argument, so it should also be set `None` to use this mode, and non-blocking can potentially lead to 100% CPU usage. Twitter `Response` Objects -------------------------- Response from a Twitter request. Behaves like a list or a string (depending on requested format), but it has a few other interesting attributes. `headers` gives you access to the response headers as an `httplib.HTTPHeaders` instance. Use `response.headers.get('h')` to retrieve a header. Authentication -------------- You can authenticate with Twitter in three ways: NoAuth, OAuth, or OAuth2 (app-only). Get `help()` on these classes to learn how to use them. OAuth and OAuth2 are probably the most useful. Working with OAuth ------------------ Visit the Twitter developer page and create a new application: **[https://dev.twitter.com/apps/new](https://dev.twitter.com/apps/new)** This will get you a `CONSUMER_KEY` and `CONSUMER_SECRET`. When users run your application they have to authenticate your app with their Twitter account. A few HTTP calls to Twitter are required to do this. Please see the `twitter.oauth_dance` module to see how this is done. If you are making a command-line app, you can use the `oauth_dance()` function directly. Performing the "oauth dance" gets you an oauth token and oauth secret that authenticate the user with Twitter. You should save these for later, so that the user doesn't have to do the oauth dance again. `read_token_file` and `write_token_file` are utility methods to read and write OAuth `token` and `secret` key values. The values are stored as strings in the file. Not terribly exciting. Finally, you can use the `OAuth` authenticator to connect to Twitter. In code it all goes like this: ```python from twitter import * MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials') if not os.path.exists(MY_TWITTER_CREDS): oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET, MY_TWITTER_CREDS) oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS) twitter = Twitter(auth=OAuth( oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET)) # Now work with Twitter twitter.statuses.update(status='Hello, world!') ``` Working with `OAuth2` --------------------- Twitter only supports the application-only flow of OAuth2 for certain API endpoints. This OAuth2 authenticator only supports the application-only flow right now. To authenticate with OAuth2, visit the Twitter developer page and create a new application: **[https://dev.twitter.com/apps/new](https://dev.twitter.com/apps/new)** This will get you a `CONSUMER_KEY` and `CONSUMER_SECRET`. Exchange your `CONSUMER_KEY` and `CONSUMER_SECRET` for a bearer token using the `oauth2_dance` function. Finally, you can use the `OAuth2` authenticator and your bearer token to connect to Twitter. In code it goes like this:: ```python twitter = Twitter(auth=OAuth2(bearer_token=BEARER_TOKEN)) # Now work with Twitter twitter.search.tweets(q='keyword') ``` License ======= Python Twitter Tools are released under an MIT License. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |