Beispiel #1
0
def test_peony_client_init_tasks_oauth2(event_loop):
    client = PeonyClient("", "", loop=event_loop, auth=oauth.OAuth2Headers)
    conf_n = random.randrange(1 << 16)
    with patch.object(peony.BasePeonyClient, 'init_tasks',
                      return_value=[conf_n]):
        tasks = client.init_tasks()
        assert conf_n in tasks
        assert len(tasks) == 1
def client_oauth(key, event_loop):
    global clients

    if clients[key] is None:
        creds = {
            k: os.environ[envk]
            for k, envk in zip(creds_oauth[key], keys_oauth[key])
        }

        clients[key] = PeonyClient(auth=headers[key], **creds)

    clients[key].loop = event_loop
    clients[key]._session = aiohttp.ClientSession(loop=event_loop)
    return clients[key]
Beispiel #3
0
 def __init__(self, bot):
     self.bot = bot
     self.tweetson = True
     with open("twitter.json") as f:
         self.track = json.load(f)
     self.pclient = PeonyClient(**self.bot.credentials['Twitter'])
     self.bot.twitask = self.bot.loop.create_task(self.twat())
Beispiel #4
0
class Cron(commands.Cog):
    _fider_colour = 0x8B572A

    def __init__(self, bot, config):
        self.bot = bot
        self.config = config

        # this will be the channels the cron updates are sent to
        self.fider_channel = None
        self.twitter_channel = None

        if intv := self.config.get('interval'):
            logger.info(f'Changing task interval to {intv} seconds')
            self.fider.change_interval(seconds=intv)
            self.twitter.change_interval(seconds=intv)

        if 'twitter' in self.config and (
                creds := self.config['twitter'].get('credentials')):
            # silence peony
            logging.getLogger('peony.data_processing').setLevel(
                logging.WARNING)
            self.twitter_client = PeonyClient(**creds,
                                              loop=self.bot.loop,
                                              session=self.bot.session)
            logger.info('Starting twitter cronjob...')
            self.twitter.add_exception_type(PeonyException)
            self.twitter.start()
async def test_oauth2_bearer_token(oauth2_client):
    await oauth2_client.headers.sign()

    token = oauth2_client.headers.token

    client2 = PeonyClient("", "", bearer_token=token, auth=oauth.OAuth2Headers)
    assert client2.headers.token == oauth2_client.headers.token
Beispiel #6
0
def initialize_twitter():
    global client

    missing = False

    required_keys = [
        ("consumer key", TWITTER_CONSUMER_KEY),
        ("consumer secret", TWITTER_CONSUMER_SECRET),
        ("access token", TWITTER_ACCESS_TOKEN),
        ("access token secret", TWITTER_ACCESS_TOKEN_SECRET),
    ]

    for name in [x for x, y in required_keys if y is None]:
        logger.info(f"Twitter {name} not provided.")
        missing = True

    if missing:
        logger.info("Twitter disabled.")
    else:
        client = PeonyClient(
            consumer_key=TWITTER_CONSUMER_KEY,
            consumer_secret=TWITTER_CONSUMER_SECRET,
            access_token=TWITTER_ACCESS_TOKEN,
            access_token_secret=TWITTER_ACCESS_TOKEN_SECRET,
        )
def _get_twitter_client() -> PeonyClient:
    creds = dict(
        consumer_key=CONSUMER_KEY,
        consumer_secret=CONSUMER_SECRET,
        access_token=ACCESS_TOKEN,
        access_token_secret=ACCESS_TOKEN_SECRET,
    )

    return PeonyClient(**creds)
Beispiel #8
0
 def __init__(self, bot):
     self.bot = bot
     self.client = PeonyClient(
         consumer_key=TWITTER_API_KEY,
         consumer_secret=TWITTER_API_S,
         access_token=TWITTER_ACCESS_TOKEN,
         access_token_secret=TWITTER_ACCESS_TOKEN_S,
     )
     self.twitter.start()
Beispiel #9
0
 def __init__(self, api_creds):
     self._index = 0
     self._clients = clients = [
         PeonyClient(consumer_key=creds['consumer_key'],
                     consumer_secret=creds['consumer_secret'],
                     access_token=creds['access_token'],
                     access_token_secret=creds['access_token_secret'])
         for creds in api_creds
     ]
     self.client_num = len(self._clients)
Beispiel #10
0
async def follow_stream():
    client = PeonyClient(
        consumer_key=cfg.consumer_key,
        consumer_secret=cfg.consumer_secret,
        access_token=cfg.access_token_key,
        access_token_secret=cfg.access_token_secret,
        proxy=cfg.proxy,
    )
    router = TweetRouter()
    router.load(cfg.follows, cfg.media_only_users)
    user_id_cache = UserIdCache()
    async with client:
        follow_ids = await user_id_cache.convert(client, router.follows)
        sv.logger.info(f"订阅推主={router.follows.keys()}, {follow_ids=}")
        stream = client.stream.statuses.filter.post(follow=follow_ids)

        async for tweet in stream:
            sv.logger.info("Got twitter event.")
            if peony.events.tweet(tweet):
                screen_name = tweet.user.screen_name
                if screen_name not in router.follows:
                    continue  # 推主不在订阅列表
                if peony.events.retweet(tweet):
                    continue  # 忽略纯转推
                reply_to = tweet.get("in_reply_to_screen_name")
                if reply_to and reply_to != screen_name:
                    continue  # 忽略对他人的评论,保留自评论

                entry = router.follows[screen_name]
                media = tweet.get("extended_entities", {}).get("media", [])
                if entry.media_only and not media:
                    continue  # 无附带媒体,订阅选项media_only=True时忽略

                msg = format_tweet(tweet)

                if "quoted_status" in tweet:
                    quoted_msg = format_tweet(tweet.quoted_status)
                    msg = f"{msg}\n\n>>>>>\n{quoted_msg}"

                old_profile_img = entry.profile_image
                entry.profile_image = tweet.user.get(
                    "profile_image_url_https") or entry.profile_image
                if old_profile_img and entry.profile_image != old_profile_img:
                    big_img = re.sub(
                        r'_normal(\.(jpg|jpeg|png|gif|jfif|webp))$', r'\1',
                        entry.profile_image, re.I)
                    msg = [msg, f"@{screen_name} 更换了头像\n{ms.image(big_img)}"]

                sv.logger.info(f"推送推文:\n{msg}")
                for s in entry.services:
                    await s.broadcast(msg, f" @{screen_name} 推文", 0.2)

            else:
                sv.logger.debug("Ignore non-tweet event.")
Beispiel #11
0
 def __init__(
     self,
     *,
     auth: OAuth10aTokens,
 ) -> None:
     self._peony_client = PeonyClient(
         consumer_key=auth.consumer_key,
         consumer_secret=auth.consumer_secret,
         access_token=auth.access_token,
         access_token_secret=auth.access_token_secret,
     )
Beispiel #12
0
 def get_twitter_client(cls):
     try:
         return cls._twitter_client
     except AttributeError:
         cls._twitter_client = PeonyClient(
             consumer_key=conf.TWITTER_CONSUMER_KEY,
             consumer_secret=conf.TWITTER_CONSUMER_SECRET,
             access_token=conf.TWITTER_ACCESS_KEY,
             access_token_secret=conf.TWITTER_ACCESS_SECRET,
             session=SessionManager.get(),
             loop=LOOP)
         return cls._twitter_client
Beispiel #13
0
    def __init__(self, getter, locales):
        if config.ACCESS_TOKEN == 'access_token':
            self.client = None
        else:
            self.client = PeonyClient(
                consumer_key=config.CONSUMER_KEY,
                consumer_secret=config.CONSUMER_SECRET,
                access_token=config.ACCESS_TOKEN,
                access_token_secret=config.ACCESS_TOKEN_SECRET)

        self.get_param = getter
        self.locales = locales
Beispiel #14
0
async def get_client():
    settings = await utils.get_settings(
        'consumer-key',
        'consumer-secret',
        'access-token',
        'access-secret')

    return PeonyClient(
        consumer_key=settings['consumer-key'],
        consumer_secret=settings['consumer-secret'],
        access_token=settings['access-token'],
        access_token_secret=settings['access-secret'])
Beispiel #15
0
async def track_stream():
    client = PeonyClient(
        consumer_key=cfg.consumer_key,
        consumer_secret=cfg.consumer_secret,
        access_token=cfg.access_token_key,
        access_token_secret=cfg.access_token_secret,
        proxy=cfg.proxy,
    )
    async with client:
        stream = client.stream.statuses.filter.post(track=['URA9'])

        async for tweet in stream:
            sv.logger.info("Got twitter event.")
            if peony.events.tweet(tweet):
                screen_name = tweet.user.screen_name
                if screen_name in cfg.uma_ura9_black_list:
                    continue  # black list
                if peony.events.retweet(tweet):
                    continue  # 忽略纯转推
                if tweet.get("quoted_status"):
                    continue  # 忽略引用转推
                if tweet.get("in_reply_to_screen_name"):
                    continue  # 忽略评论

                if not re.search(r'\d{9}', tweet.text):
                    continue  # 忽略无id的推特
                if re.search(r'ura(シナリオ)?([::])?\s*[0-5012345]', tweet.text,
                             re.I):
                    continue  # 忽略低ura因子
                if re.search(r'目指|狙|チャレンジ|微妙', tweet.text, re.I):
                    continue  # 忽略未达成
                if re.search(r'青(因子)?\s*[01245780124578]', tweet.text, re.I):
                    continue  # 忽略低星蓝
                if re.search(
                        r'(スピ(ード)?|スタ(ミナ)?|パワー?|根性?|賢さ?)\s*[01245780124578]',
                        tweet.text, re.I):
                    continue  # 忽略低星蓝
                if re.search(r'ura(シナリオ)?([::])?9(では|じゃ|周回)', tweet.text,
                             re.I):
                    continue  # 忽略否定型
                if re.search(r'青(因子)?9(では|じゃ|周回)', tweet.text, re.I):
                    continue  # 忽略否定型

                msg = format_tweet(tweet)

                sv.logger.info(f"推送推文:\n{msg}")
                await sv.broadcast(msg, f" @{screen_name} 推文", 0.2)

            else:
                sv.logger.debug("Ignore non-tweet event.")
Beispiel #16
0
    def __init__(self, config):
        super().__init__(
            command_prefix=_prefix_callable,
            case_insensitive=True,
            intents=intents,
            help_command=commands.DefaultHelpCommand(no_category="Assorted",
                                                     dm_help=True,
                                                     verify_checks=False),
        )

        self.config = config
        self.database = Database(config.database_url, config.database_conns)

        self.destiny = Pydest(
            api_key=config.destiny.api_key,
            client_id=config.destiny.client_id,
            client_secret=config.destiny.client_secret,
        )

        self.the100 = The100(config.the100.api_key, config.the100.base_url)

        self.twitter = None
        if (config.twitter.consumer_key and config.twitter.consumer_secret
                and config.twitter.access_token
                and config.twitter.access_token_secret):
            self.twitter = PeonyClient(**config.twitter.asdict())

        self.ext_conns = {
            "database": self.database,
            "destiny": self.destiny,
            "twitter": self.twitter,
            "the100": self.the100,
            "redis_cache": None,
            "redis_jobs": None,
        }

        for extension in STARTUP_EXTENSIONS:
            try:
                self.load_extension(extension)
            except Exception as e:
                exc = traceback.format_exception(type(e), e, e.__traceback__)
                log.error(f"Failed to load extension {extension}: {exc}")

        if config.enable_activity_tracking:
            self.update_members.start()

        self.cache_clan_members.start()
Beispiel #17
0
async def tweet(session, logger):
    twitter_client = PeonyClient(
        consumer_key=os.getenv("TWITTER_CONSUMER_KEY"),
        consumer_secret=os.getenv("TWITTER_CONSUMER_SECRET"),
        access_token=os.getenv("TWITTER_ACCESS_TOKEN"),
        access_token_secret=os.getenv("TWITTER_ACCESS_TOKEN_SECRET"),
        session=session)

    # Need to do a manual connection to order by random
    conn = Tortoise.get_connection("default")
    haiku_rows = await conn.execute_query_dict(
        "select id from haiku where tweet_id IS NULL AND article_id NOT IN (select article_id from haiku where tweet_id IS NOT NULL and tweeted_at > datetime('now','-2 hour')) ORDER BY RANDOM() limit 1"
    )

    if len(haiku_rows) > 0:
        haiku = await models.Haiku.get(id=haiku_rows[0]["id"])
        await haiku.fetch_related('article')
        haiku.tweet = tweet_from_haiku(haiku)

        response = await twitter_client.api.statuses.update.post(
            status=haiku.tweet, trim_user=True)
        haiku.tweet_id = response['id_str']
        haiku.tweeted_at = parser.parse(response['created_at'])

        log_line = f"{haiku.line0} / {haiku.line1} / {haiku.line2}"
        logger.info(f"TWEET {haiku.tweet_id} {log_line}")

        await haiku.save()

    response = await twitter_client.api.statuses.user_timeline.get(
        count=200, screen_name=os.getenv("TWITTER_USERNAME"), trim_user=True)
    for t in response:
        haiku = await models.Haiku.filter(tweet_id=t['id_str']).first()
        if haiku:
            favorite_count = t['favorite_count']
            retweet_count = t['retweet_count']

            if change_in_counts(haiku, favorite_count, retweet_count):
                logger.info(
                    f"TWEET {t['id_str']} FAVES: {haiku.favorite_count} -> {favorite_count} RT: {haiku.retweet_count} -> {retweet_count}"
                )
                haiku.favorite_count = favorite_count
                haiku.retweet_count = retweet_count
                await haiku.save()

    logger.info("TWEET STATISTICS updated")
Beispiel #18
0
async def twitter_stream_daemon():
    client = PeonyClient(consumer_key=cfg.consumer_key,
                         consumer_secret=cfg.consumer_secret,
                         access_token=cfg.access_token_key,
                         access_token_secret=cfg.access_token_secret)
    async with client:
        while True:
            try:
                await open_stream(client)
            except KeyboardInterrupt:
                return
            except Exception as e:
                salmon.logger.exception(e)
                salmon.logger.error(
                    f"Error {type(e)} Occurred in twitter stream. Restarting stream in 5s."
                )
                await asyncio.sleep(5)
Beispiel #19
0
    async def start(self) -> None:
        self.config: twitter = Edi().config.twitter
        if not all([
                self.config.consumer_key,
                self.config.consumer_secret,
                self.config.access_key,
                self.config.access_secret,
        ]):
            log.debug(f"missing twitter credentials")
            return

        logging.getLogger("peony").setLevel(logging.WARNING)
        self.client = PeonyClient(
            consumer_key=self.config.consumer_key,
            consumer_secret=self.config.consumer_secret,
            access_token=self.config.access_key,
            access_token_secret=self.config.access_secret,
        )

        self.task = asyncio.ensure_future(self.timeline())
Beispiel #20
0
 async def run(self):
     try:
         if not self.twitter_model.verified:
             raise ValueError('Oauth Handshake not completed')
         self.client = PeonyClient(
             consumer_key=config['twitter']['consumer_key'],
             consumer_secret=config['twitter']['consumer_secret'],
             access_token=self.twitter_model.access_token,
             access_token_secret=self.twitter_model.access_token_secret,
         )
         if self.twitter_model.mentions_since_id is None:
             logger.debug(
                 'since_id is None in model, fetch newest mention id')
             await self._poll_mentions()
         if self.twitter_model.dms_since_id is None:
             logger.debug('since_id is None in model, fetch newest dm id')
             await self._poll_direct_messages()
         user = await self.client.user
         if user.screen_name:
             await self.twitter_model.update(username=user.screen_name)
         logger.debug('Starting Twitter bot: %s' %
                      self.twitter_model.__dict__)
         await gather(self.poll(), self.push())
     except CancelledError:
         logger.debug(
             f'Bot {self.twitter_model.hood.name} received Cancellation.')
     except exceptions.Unauthorized:
         logger.debug(
             f'Bot {self.twitter_model.hood.name} has invalid auth token.')
         await self.twitter_model.update(enabled=False)
         self.enabled = self.twitter_model.enabled
     except (KeyError, ValueError, exceptions.NotAuthenticated):
         logger.warning(
             'Missing consumer_keys for Twitter in your configuration.')
         await self.twitter_model.update(enabled=False)
         self.enabled = self.twitter_model.enabled
     finally:
         logger.debug(f'Bot {self.twitter_model.hood.name} stopped.')
Beispiel #21
0
    def __init__(self, config):
        super().__init__(command_prefix=_prefix_callable,
                         case_insensitive=True,
                         help_command=commands.DefaultHelpCommand(
                             no_category="Assorted",
                             dm_help=True,
                             verify_checks=False))

        self.config = config
        self.database = Database(config.database_url)
        self.database.initialize()

        self.destiny = Pydest(
            api_key=config.bungie.api_key,
            client_id=config.bungie.client_id,
            client_secret=config.bungie.client_secret,
        )

        self.the100 = The100(config.the100.api_key, config.the100.base_url)

        self.twitter = None
        if (config.twitter.consumer_key and config.twitter.consumer_secret
                and config.twitter.access_token
                and config.twitter.access_token_secret):
            self.twitter = PeonyClient(**config.twitter.asdict())

        for extension in STARTUP_EXTENSIONS:
            try:
                self.load_extension(extension)
            except Exception as e:
                exc = traceback.format_exception(type(e), e, e.__traceback__)
                log.error(f"Failed to load extension {extension}: {exc}")

        self.bungie_maintenance = False

        if config.enable_activity_tracking:
            self.update_last_active.start()
            self.update_member_games.start()
Beispiel #22
0
 async def twitter_update(self):
     await self.bot.wait_until_ready()
     while not self.bot.is_closed():
         try:
             recent = None
             twitter_client = PeonyClient(
                 consumer_key=authDeets.consumer_key,
                 consumer_secret=authDeets.consumer_secret,
                 access_token=authDeets.access_token,
                 access_token_secret=authDeets.access_token_secret)
             data = await twitter_client.api.statuses.user_timeline.get(
                 screen_name='DoorMonster', count=1)
             recent = data[0]['id']
             if recent:
                 if self.last_twitter_update is None:
                     self.last_twitter_update = recent
                     print("Twitter link established, got tweet {}".format(
                         recent))
                 if self.last_twitter_update != recent:
                     self.last_twitter_update = recent
                     if data[0][
                             'in_reply_to_status_id'] is None and not data[
                                 0]['text'].startswith('RT @'):
                         the_channel = discord.utils.get(
                             self.bot.get_all_channels(),
                             guild__name="Door Monster",
                             name="social-media-feed")
                         await the_channel.send(
                             "https://twitter.com/DoorMonster/status/{0}".
                             format(recent))
                         log("Twitter update", None, None,
                             self.last_twitter_update, None)
             await asyncio.sleep(300)
         except:
             print("Error retreiving tweet, sleeping")
             await asyncio.sleep(3000)
Beispiel #23
0
from requests_oauthlib import OAuth1

import hmac, base64, hashlib
import json
from flask import Flask, request
from http import HTTPStatus

import tf_connect
from configuration import Configuration

app = Flask(__name__)

# create the client using the api keys
CLIENT = PeonyClient(
    consumer_key=Configuration.CONSUMER_KEY,
    consumer_secret=Configuration.CONSUMER_SECRET,
    access_token=Configuration.ACCESS_TOKEN,
    access_token_secret=Configuration.ACCESS_TOKEN_SECRET,
)
AUTH = OAuth1(
    Configuration.CONSUMER_KEY,
    Configuration.CONSUMER_SECRET,
    Configuration.ACCESS_TOKEN,
    Configuration.ACCESS_TOKEN_SECRET,
)

TF_SERVER_URL = Configuration.TF_SERVER_URL

CURRENT_USER_ID = None

loop = asyncio.get_event_loop()
# ACCESS_TOKEN = os.getenv("ACCESS_TOKEN", None)
ACCESS_TOKEN_SECRET = ''
# ACCESS_TOKEN_SECRET = os.getenv("ACCESS_TOKEN_SECRET", None)

# Users to watch for should be a list. This will be joined by Twitter and the
# data returned will be for any tweet mentioning:
# @twitter *OR* @twitterapi *OR* @support.
USERS = ['@survivetheark']

# Languages to filter tweets by is a list. This will be joined by Twitter
# to return data mentioning tweets only in the english language.
LANGUAGES = ['en']

# Since we're going to be using a streaming endpoint, there is no need to worry
# about rate limits.
pclient = PeonyClient(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
                      ACCESS_TOKEN_SECRET)

client = discord.Client()
loop = asyncio.get_event_loop()
app = web.Application()
link = 'https://survivetheark.com/index.php?/forums/topic/388254-pc-patch-notes-client-v31067-server-v31076/'
debug = False


@client.event
async def on_ready():
    print(f'Logged on as {client.user}')
    print('Tracking of Twitter, Youtube, and RSS Feeds Started')


@client.event
from peony import events
from pushbullet import Pushbullet

loop = asyncio.get_event_loop()

PUSHBULLET_TOKEN = os.getenv('PUSHBULLET_TOKEN')

CONSUMER_KEY = os.getenv('CONSUMER_KEY')
CONSUMER_SECRET = os.getenv('CONSUMER_SECRET')
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET')
ALERTS = os.getenv('ALERTS', '').split(',')


client = PeonyClient(consumer_key=CONSUMER_KEY,
                     consumer_secret=CONSUMER_SECRET,
                     access_token=ACCESS_TOKEN,
                     access_token_secret=ACCESS_TOKEN_SECRET)

pushbullet = Pushbullet(PUSHBULLET_TOKEN)


async def send(alert, tweet_text):
    title = f'New {alert} alert'
    body = tweet.text

    await pushbullet.push_note(title, body)

async def process(tweet):
    print(f'Got tweet {tweet.text}')

    for alert in ALERTS:
Beispiel #26
0
#!/usr/bin/env python3

#import 
from peony import PeonyClient
import datetime
from dateutil.parser import parse
from trusted import userid

creds = dict(consumer_key=YOUR_CONSUMER_KEY,
             consumer_secret=YOUR_CONSUMER_SECRET,
             access_token=YOUR_ACCESS_TOKEN,
             access_token_secret=YOUR_ACCESS_TOKEN_SECRET) #Init a async client

client = PeonyClient(**creds)



async def isAlive(bot):

	latest = await client.api.statuses.user_timeline.get(screen_name="screenname")
	now = datetime.datetime.now(datetime.timezone.utc)
	time = datetime.datetime.now(datetime.timezone.utc) - parse(latest.created_at)
	if time.days > 2:
		await bot.sendMessage(bot.sendMessage(msg['chat']['id'],"""
			The Twitter account of `PlaceHolder` has been nonactive for over 2 days, now sending life confirmation request to pre-defined trusted user, please confirm this user life activitiy.
			You may send confirmation email to [email protected] with email titiled 'Confirmed' (Case Sensitive)
			""")

Beispiel #27
0
WORKER_SLEEP = int(os.getenv('COMKC_WORKER_SLEEP', 60 * 60 * 8))
MAX_LIMIT = 20
MAX_OFFSET = 500

if DEBUG:
    level = logging.DEBUG
else:
    level = logging.INFO

format_str = '%(levelname)s %(asctime)s %(module)s %(name)s %(message)s'
logging.basicConfig(level=level, format=format_str)

SENTRY_DSN = os.environ.get('COMKC_SENTRY_DSN')
if SENTRY_DSN:
    from raven.conf import setup_logging
    from raven.handlers.logging import SentryHandler
    handler = SentryHandler(SENTRY_DSN)
    handler.setLevel(logging.ERROR)
    setup_logging(handler)

TWITTER = {
    'consumer_key': os.environ.get('COMKC_TWITTER_CONSUMER_KEY'),
    'consumer_secret': os.environ.get('COMKC_TWITTER_CONSUMER_SECRET'),
    'access_token': os.environ.get('COMKC_TWITTER_ACCESS_TOKEN'),
    'access_token_secret': os.environ.get('COMKC_TWITTER_ACCESS_TOKEN_SECRET'),
}
peony_client = None
if TWITTER['consumer_key']:
    from peony import PeonyClient
    peony_client = PeonyClient(**TWITTER)
Beispiel #28
0
def dummy_peony_client(event_loop):
    client = PeonyClient("", "", loop=event_loop)
    with patch.object(client, 'init_tasks', return_value=[]):
        yield client
Beispiel #29
0
def peony_client(event_loop):
    return PeonyClient("", "", loop=event_loop)
Beispiel #30
0
 async def _ainit(self):
     self.client = PeonyClient(**self.keys, loop=self.bot.loop)
     self.restart_stream_task = asyncio.create_task(
         self.restart_stream_sub())