Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncStreamingClient examples #1970

Open
nono-london opened this issue Oct 1, 2022 · 5 comments
Open

AsyncStreamingClient examples #1970

nono-london opened this issue Oct 1, 2022 · 5 comments
Labels
Documentation This is regarding the library's documentation

Comments

@nono-london
Copy link

Hello,
and thanks for developping this lib.
I am struggling to find any example of use of the async streaming V2 version: AsyncStreamingClient.
If you could poinit me to some website with exampe of connection and streaming, rules handling, that would be great.
(the example folder of the lib, doesn't seem to have been updated since 2017, or anyway I couldn't find an AsyncStreamingClient example)
Best

@Harmon758
Copy link
Member

AsyncStreamingClient's interface mirrors that of StreamingClient, for which there is a guide with examples.
The main difference is that many of AsyncStreamingClient's methods are coroutines, which is documented.

You can see that the last commit for the examples folder was in March of this year, not 2017.

@Harmon758 Harmon758 added the Documentation This is regarding the library's documentation label Oct 1, 2022
@nono-london
Copy link
Author

nono-london commented Oct 3, 2022

Just in case this code helps somone who wants to get started.
It bugs DELIBERATELY in order to point out what seems to be a potential commum error, when handling rules.
This code inspired or corrected from the TweePy Community: tks to @Harmon758 specifically on the Rule handling.
Corrections/improvements are welcomed.

import asyncio
import logging
import sys
from typing import Union

from tweepy.asynchronous.streaming import (AsyncStreamingClient,
                                           StreamRule)

from twitter_news_tickers.app_config_secret2 import (TWITTER_API_BEARER_KEY, ) # just put your own BEARER_KEY


class RunForeverClient(AsyncStreamingClient):
    def __init__(self, bearer_token, **kwargs):
        super().__init__(bearer_token, **kwargs)
        self.request_tweeter_fields: list = ['author_id', 'text', 'id', 'created_at', ]

        # set tickers to follow
        self.follow_tickers: str = '$BBBY OR $NDX OR $FIGS OR $SPLK OR $0293.HK OR $TSLA OR $SNN.L OR $USO OR $NVAX OR $PLUG OR $VWAPY OR $AMZN OR $SCHB.OL OR $VWAGY OR $VLTSA.PA OR $CB OR $NVDA OR $SQM OR $AF.PA OR $BRK-B OR $MYPS OR $SPX OR $QQQ OR $SGFY OR $AAC.PA OR $AACW.PA OR $ADE.OL OR $FB OR $LPK.DE OR $ARKK OR $MKS.L OR $ZM OR $PLTR OR $BKKT OR $AAPL OR $SNAP OR $BDT.DE OR $CAT OR $BAR OR $EPHYU OR $DAL OR $UAL OR $MSFT OR $ES OR $TWTR OR $PFE OR $LVS OR $VOW3.DE OR $ANET OR $GOOGL OR $ILMN OR $SPY OR $VLA.PA OR $AMD OR $AAL'

        # set users to follow
        self.users_to_follow: Union[
            str, None] = 'from:KingOfAllStonks OR from:HedgeMind OR from:HalfersPower OR from:BreakingStocks_ OR from:OptionsMike OR from:OptionAlert OR from:Vtradez OR from:compound248 OR from:smashelito OR from:daniburgz OR from:FNStonerman OR from:vixologist OR from:BankofVol OR from:UKCovid19Stats OR from:realwillmeade OR from:NateGeraci OR from:perushka OR from:TimTheMM OR from:laurenthomas OR from:federalreserve OR from:bankofengland OR from:A_Setup_Calls OR from:hipster_trader OR from:TrendSpider OR from:unusual_whales OR from:tradebotAI OR from:CNN OR from:fintwit_synapse OR from:trader_debo OR from:DmytroKuleba OR from:turbonator_23 OR from:R_Zandi OR from:LionelHutz_Esq OR from:stockresearchhq OR from:MarketRebels OR from:congresstrading OR from:LongBullInfo OR from:CitronResearch OR from:JessicaNutt96 OR from:Bladerunner871 OR from:davidfaber OR from:arbitratuer OR from:algobotnews OR from:turbo_quantum OR from:CheddarFlow OR from:ripster47 OR from:ElonJet OR from:TwitterIR OR from:StrizziJ OR from:TlmTheMM OR from:WsbmodR OR from:ZelenskyyUa OR from:annmarie OR from:Bank_of_Japan_e OR from:TickerHistory OR from:thebirdabides OR from:SelectOptions OR from:HindenburgRes OR from:GammaEdges OR from:NATOpress OR from:turboleistung OR from:SeekingAlpha OR from:chancery_daily OR from:Doctabones OR from:OptionsAction OR from:SoberLook OR from:Amena__Bakr OR from:JulianKlymochko OR from:squawksquare OR from:C_Barraud OR from:JimScalpert OR from:DivesTech OR from:AdaptivCharts OR from:EmmanuelMacron OR from:MariaBartiromo OR from:Mayhem4Markets OR from:tmts_media OR from:androsForm OR from:DeItaone OR from:muddywatersre OR from:POTUS OR from:FTMarkets OR from:RyansTrading_ OR from:dampedspring OR from:bankofcanada OR from:BullishBearz OR from:MrDarnton OR from:iaeaorg OR from:The_Real_Fly OR from:AAPLinsights OR from:FortuneOptions OR from:Fxhedgers OR from:spotgamma OR from:ProfRobAnderson OR from:SpacBobby OR from:staunovo OR from:KyivIndependent OR from:OptionTakes OR from:labeltrader1122 OR from:traderboog OR from:markets OR from:AnnMLipton OR from:Hafizebot OR from:Reuters OR from:EricBalchunas OR from:ProfEricTalley OR from:WSJmarkets OR from:StockMKTNewz OR from:WholeMarsBlog OR from:Mike_Hunt_Sr OR from:zerohedge OR from:kateconger OR from:T_L_B_S_ OR from:business OR from:Lagarde OR from:financialjuice OR from:ftbreakingnews OR from:MuhammadLila OR from:CommodityImpVol OR from:TradeWithAlerts OR from:PalantirTech OR from:newsfilterio OR from:fly4dat OR from:hyperconvexity OR from:WSJ OR from:matt_levine'

    async def on_exception(self, exception):
        print(f"Exception while running Tweeter streamer V2:\n"
              f"{exception.__class__.__name__}")
        print(exception)
        exit(exception)

    async def on_disconnect(self):
        print(f'Disconnected')

    async def on_includes(self, includes):
        print(includes)
        tweets: list = includes['tweets']
        users: list = includes['users']
        if len(users) != len(tweets):
            print(f'Not same size between users and tweets')
            # user replied to himself=> 1 user, multiple tweets
            # user[0] replied to more than one [1:] user involved in the discussion

    async def on_connect(self):
        print(f'Connected')

    async def on_response(self, response):
        tweet_id: int = response.data['id']
        tweeter_user_id = response.data['author_id'] or -1
        # TODO:
        # handle truncated text for re-tweets
        # https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9
        # https://docs.tweepy.org/en/stable/extended_tweets.html
        print(response)
        tweet_str: str = response.data['text']
        if tweet_str[-3] == "…":
            print(tweet_str)
        print("#" * 100)
        print(f'Response tweet_id: {tweet_id}')
        print(f'Response author_id: {tweeter_user_id}')

        print(f'Response data:\n{tweet_str}')
        print(response.errors)

    async def run_forever(self):
        response = await self.add_rules(add=[StreamRule(value=self.follow_tickers, id="equities"),
                                             StreamRule(value=self.users_to_follow, id="followed"),
                                             ])
        if len(response.errors) > 0:
            print(f'Error while adding rules: {response}')
            exit(0)
        print(response)
        self.filter(tweet_fields=self.request_tweeter_fields,
                    expansions=[
                        'author_id',
                        'referenced_tweets.id']
                    )


async def main():
    client = RunForeverClient(TWITTER_API_BEARER_KEY)

    await client.run_forever()


if __name__ == "__main__":
    # Use to debug  your app
    logging.basicConfig(level=logging.DEBUG)
    
    # asyncio check for OS limitations related to windows
    if sys.platform == "win32" and sys.version_info.minor >= 8:
        asyncio.set_event_loop_policy(
            asyncio.WindowsSelectorEventLoopPolicy()  # pylint: disable=no-member
        )
    event_loop = asyncio.get_event_loop()
    
    event_loop.run_until_complete(main())

Returns a Response error pointing to your mistake. In this case:

.... exceeds the maximum length of a rule. It is 2533 characters while the maximum allowed is 512 (at position 1)"]

@ArtemBernatskyy
Copy link

omg, this lib's docs are TRASH,
abandonware (specifically for using API v2 with async tweepy)

@ArtemBernatskyy
Copy link

tipped 50$ for docs improvement, let's make tweepy great again!

@ArtemBernatskyy
Copy link

OMG, twitter API is soooo mess (or I am stupid afk),

it seems that you need a PRO PLAN TO ACCESS STREAMING API but it's not mentioned in errors/docs etc. The only fking place where you can deduce it it's Twitter Portal where it lists what endpoints are available for each plan...

So frustrating (@!#)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation This is regarding the library's documentation
Projects
None yet
Development

No branches or pull requests

3 participants