def __init__(self, bot, consumer: str, consumer_secret: str, access: str, access_secret: str, discord_channel_id: int, twitter_user_id): self.client = peony.PeonyClient(consumer_key=consumer, consumer_secret=consumer_secret, access_token=access, access_token_secret=access_secret) self.bot = bot self.channel_id = int(discord_channel_id) self.follow_id = twitter_user_id
async def post_tweets(self): channel = self.eris.get_server(config['discord_serverid']).get_channel( config['discord_channel_announcements']) if channel is None: log.error("No announcements channel") return twitter = peony.PeonyClient( consumer_key=config['twitter_api_key'], consumer_secret=config['twitter_api_secret']) users = await twitter.api.users.lookup.get( screen_name=",".join(config["twitter_users"])) user_ids = {user.id for user in users} while True: try: for user in users: since_id_key = STATE_TWITTER_LAST_TWEET_ID_KEY % user.id since_id = state.get(self.engine, self.metadata, since_id_key) if since_id is None: try: last_tweet_id = list( await twitter.api.statuses.user_timeline.get( user_id=user.id, include_rts=True))[0].id except IndexError: last_tweet_id = 1 else: last_tweet_id = since_id for tweet in list( await twitter.api.statuses.user_timeline.get( user_id=user.id, since_id=since_id, include_rts=True))[::-1]: last_tweet_id = tweet.id if tweet.in_reply_to_user_id is None or tweet.in_reply_to_user_id in user_ids: await self.eris.send_message( channel, "New tweet from %(name)s: https://twitter.com/%(screen_name)s/status/%(tweet_id)s" % { "name": tweet.user.name, "screen_name": tweet.user.screen_name, "tweet_id": tweet.id, }) state.set(self.engine, self.metadata, since_id_key, last_tweet_id) except utils.PASSTHROUGH_EXCEPTIONS: raise except Exception: log.exception("Exception in Twitter announcement task") continue await asyncio.sleep(15)
def __init__(self, bot, event_manager, config_manager, twitter_repository): self.configManager = config_manager self.eventManager = event_manager self.bot = bot self.twitterRepository = twitter_repository self.client = peony.PeonyClient( **{ "consumer_key": config_manager.get_field("consumer_key"), "consumer_secret": config_manager.get_field("consumer_secret"), "access_token": config_manager.get_field("access_token"), "access_token_secret": config_manager.get_field("access_token_secret"), } ) self.prepare_events() self.bot.loop.create_task(self.track())
def __init__(self, bot): self.bot = bot self.conf = config.Config('conf/twitter.json', encoding='utf-8') self.twitter_client = peony.PeonyClient(**self.conf.credentials) self.stream_task = None self.stream_start()
def __init__(self): self.twitter = peony.PeonyClient( consumer_key=config.get("deresute.app_key"), consumer_secret=config.get("deresute.app_secret"), access_token=config.get("deresute.token"), access_token_secret=config.get("deresute.token_secret"))
def __new__(cls, *args, **kwargs): return peony.PeonyClient(*args, **kwargs)
def setup_twitter_client(self): self.client = peony.PeonyClient(consumer_key=config.CONSUMER_KEY, consumer_secret=config.CONSUMER_SECRET, **self.config['auth'])
async def _init_client(self): auth = await self.conf.auth() self.client = peony.PeonyClient(**auth)
def peony_client(self, **kwargs): """Return Twitter API instance.""" return peony.PeonyClient(**self.settings['twitter_api'], **kwargs)
log.warning( f"The minimum value for the lookback time is 45 seconds. Time of {RADIO_MONITOR_LOOKBACK}" " second(s) is less than 45 seconds and will be set to 45 seconds automatically." ) RADIO_MONITOR_LOOKBACK = 45 # Checking to ensure user ids are in the proper format, raise error if not. for tweeter in TRUSTED_TWEETERS: if tweeter[0] == "@": raise ValueError( "TRUSTER_TWEETERS must be user IDs and not handles. Please visit http://gettwitterid.com/" " to find the user ID of a user's handle.") ################################################################################ # Environment State Variables ################################################################################ STATE = _State(AUTOSCAN_STATE_FILE_PATH) TWITTER_TO_SIGNAL_QUEUE: Queue = Queue( maxsize=10000) # shooting from the hip here... ################################################################################ # Peony Twitter Event Stream client ################################################################################ API_KEYS = { "consumer_key": TWITTER_API_KEY, "consumer_secret": TWITTER_API_SECRET, "access_token": TWITTER_ACCESS_TOKEN, "access_token_secret": TWITTER_TOKEN_SECRET, } CLIENT = peony.PeonyClient(**API_KEYS)
import collections import peony from aioreactive.core import AsyncStream from feed import settings client = peony.PeonyClient(**settings.TWITTER) latest_tweets = collections.deque(maxlen=settings.LATEST_TWEETS_COUNT) new_tweets_stream = AsyncStream() async def track_tweets(): async with client.stream.statuses.filter.post( track=settings.TWITTER_TRACK) as stream: async for tweet in stream: if 'error' not in tweet and 'text' in tweet: latest_tweets.appendleft(tweet) await new_tweets_stream.asend(tweet) async def fetch_tweets(): response = await client.api.search.tweets.get(q=settings.TWITTER_TRACK) latest_tweets.extend(response['statuses']) async def initialize(app): await fetch_tweets() app.loop.create_task(track_tweets())