Package: python-twitter-v2
python-twitterA simple Python wrapper for Twitter API v2 :sparkles: :cake: :sparkles:.IntroductionTwitter has published new versionTwitter API V2for developer at Aug 13, 2020.This library provides a service to easily use this new version Twitter API.DocumentationYou can get all API descriptionsTwitter API v2 Documentation.Docs for this library onhereInstallingYou can install this library easily bypypi:$pipinstallpython-twitter-v2Code is hosted athttps://github.com/sns-sdks/python-twitter.Checkout latest development version with:$gitclonehttps://github.com/sns-sdks/python-twitter.git$cdpython-twitterInstall dependencies with:$makeenvRun tests with:$maketestRun tests with coverage:$makecov-term$makecov-htmlUsingThe API is exposed via thepytwitter.Apiclass.Now covers these features:TweetsTweet lookupManage TweetsQuote TweetsRetweet TweetsTimelinesSearch TweetsTweet countsFiltered streamVolume streamsRetweetsLikesBookmarksHide repliesUsersUser lookupFollowsBlocksMutesSpacesSpaces lookupSearch SpacesComplianceBatch complianceListsList lookupManage listsList Tweets lookupList membersList followsPinned ListsDirect MessagesDirect Messages lookupManage Direct MessagesUsageTweetsMedia UploadMedia Simple uploadMedia Chunked uploadINSTANTIATEYou can initialize with an bearer token:>>>frompytwitterimportApi>>>api=Api(bearer_token="Your bearer token")With OAuth 1.0A user context token:>>>api=Api(consumer_key="consumer key",consumer_secret="consumer secret",access_token="access token",access_secret="access secret")Or with authorization done by user:>>>api=Api(consumer_key="consumer key",consumer_secret="consumer secret",oauth_flow=True)# get url for user to authorize>>>api.get_authorize_url()# copy the response url>>>api.generate_access_token("https://localhost/?oauth_token=oauth_token&oauth_verifier=oauth_verifier"){'oauth_token':'oauth_token','oauth_token_secret':'oauth_token_secret','user_id':'123456','screen_name':'screen name'}Twitter hasannounced OAuth 2.0 user authentication support with fine-grained scopesNow if you have app withOAuth2.0client ID. you can do authorize withOAuth2.>>>api=Api(client_id="You client ID",oauth_flow=True)# get the url and code verifier for user to authorize>>>url,code_verifier,_=api.get_oauth2_authorize_url()# copy the response url>>>api.generate_oauth2_access_token("https://localhost/?state=state&code=code",code_verifier){'token_type':'bearer','expires_in':7200,'access_token':'access_token','scope':'users.read tweet.read','expires_at':1631775928}Users-lookupYou can get information about a user or group of users, specified by a user ID or a username.Get group of users:# By ids>>>api.get_users(ids=["783214","2244994945"])Response(data=[User(id='2244994945',name='Twitter Dev',username='TwitterDev'),User(id='783214',name='Twitter',username='Twitter')])# By username>>>api.get_users(usernames="Twitter,TwitterDev")Response(data=[User(id='2244994945',name='Twitter Dev',username='TwitterDev'),User(id='783214',name='Twitter',username='Twitter')])Get single user:# By id>>>api.get_user(user_id="783214")Response(data=User(id='783214',name='Twitter',username='Twitter'))# By username>>>api.get_user(username="Twitter")Response(data=User(id='783214',name='Twitter',username='Twitter'))Get user following:>>>api.get_following(user_id="2244994945",max_results=5)Response(data=[User(id='459860328',name='julie✨',username='JulieMendoza206'),User(id='273830767',name='🄿🅄🅂🄷',username='rahul_pushkarna')...])Get user followers:>>>api.get_followers(user_id="2244994945",max_results=5)Response(data=[User(id='715131097332518912',name='Daniel',username='RGIDaniel'),User(id='1176323137757048832',name='Joyce Wang',username='joycew67')...])You can follow or unfollow user if you have User context.follow user:>>>api.follow_user(user_id="123456",target_user_id="654321"){'data':{'following':True,'pending_follow':False}}unfollow user:>>>api.unfollow_user(user_id="123456",target_user_id="654321"){'data':{'following':False}}Tweets-lookupYou can get information about a tweet or group of tweets by tweet id(s).Get single tweet:>>>api.get_tweet("1354143047324299264",expansions=["attachments.media_keys"],media_fields=["type","duration_ms"])Response(data=Tweet(id=1354143047324299264,text=Academicsareoneofthebiggestgroupsusing...))Get group of tweets:>>>api.get_tweets(["1261326399320715264","1278347468690915330"],expansions="author_id",tweet_fields=["created_at"],user_fields=["username","verified"])Response(data=[Tweet(id=1261326399320715264,text=Tuneintothe@MongoDB@Twitchstream...),Tweet(id=1278347468690915330,text=Goodnewsandbadnews:2020ishalfover)])Streaming APIFor Streaming, this provideStreamApiindependent. Same as mainApi, You need initialize it first.>>>frompytwitterimportStreamApi>>>stream_api=StreamApi(bearer_token="bearer token")# or use consumer key and secret>>>stream_api=StreamApi(consumer_key="consumer key",consumer_secret="consumer secret")For Sample Stream tweets, you can use thesample_streamfunction to build a connection.>>>stream_api.sample_stream()For Filtered Stream, you can create rules.Get your current rules.>>>stream_api.get_rules()Response(data=[StreamRule(id='1369580714056843266',value='twitter api ')])Delete your rules.>>>stream_api.manage_rules(rules={"delete":{"ids":["1369580714056843266"]}})Response(data=[])Add new rules. If you setdry_runto True, will only validate rules, and not create them.>>>np={"add":[{"value":"cat has:media","tag":"cats with media"},{"value":"cat has:media -grumpy","tag":"happy cats with media"}]}>>>stream_api.manage_rules(rules=np,dry_run=True)Response(data=[StreamRule(id='1370406958721732610',value='cat has:media -grumpy'),StreamRule(id='1370406958721732609',value='cat has:media')])Then you can usesearch_streamto get tweets matching your rules.>>>stream_api.search_stream()You can go to theExample folderfor streaming examples.TODOMore Api waiting twitter