Пример #1
0
class BotCommands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.db = DB()
        self.tz_name = 'America/New_York'
        self.tz = pytz.timezone(self.tz_name)

    @commands.command(name='today')
    async def today(self, ctx):
        d = dt.now()
        date_today = f'{d.year}/{d.month}/{d.day}'
        events = self.db.get_events_by_date(date_today)
        embed = Embed()
        embed.title = 'Today\'s Events'
        if len(events) == 0:
            embed.description = 'No Restricted Events Today!'
        for event in events:
            year = int(event[2].split('/')[0])
            month = int(event[2].split('/')[1])
            day = int(event[2].split('/')[2])
            hour = int(event[3].split(':')[0])
            minute = int(event[3].split(':')[1])
            event_time = dt(year, month, day, hour, minute, tzinfo=self.tz)
            left = event_time - dt.now(tz=self.tz)
            left = str(left).split('.')[0]
            embed.add_field(name='Title', value=event[1])
            embed.add_field(name='Currency', value=event[4])
            embed.add_field(name='Time', value=f'{event[2]} {event[3]}')
            embed.add_field(name='Time Left', value=left, inline=False)
        await ctx.send(embed=embed)
Пример #2
0
 def __init__(self, bot):
     self.config = json.load(open('config.json'))
     self.db = DB()
     self.bot = bot
     self.loop = asyncio.new_event_loop()
     self.URL = 'https://ftmo.com/en/calendar/'
     self.itter_time = 300
     # self.jobstore = {
     #     'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
     # }
     self.schedular = AsyncIOScheduler(event_loop=self.loop)
     self.schedular.start()
     # self.schedular.print_jobs()
     self.tz_name = 'America/New_York'
     self.tz = pytz.timezone(self.tz_name)
     self.start_driver()
class EventHandlers:
    def __init__(self, bot: TelegramClient):
        self.bot = bot
        self.twitter_api = TwitterAPI()
        self.db = DB()

    async def start(self, event):
        btns = [[Button.text(ADD_USER_BTN),
                 Button.text(REMOVE_USER_BTN)], [Button.text(LIST_USER_BTN)]]
        await event.respond('Main Menu', buttons=btns)

    async def add_user(self, event):
        await event.delete()
        async with self.bot.conversation(event.message.peer_id) as self.conv:
            await self.conv.send_message('Please enter new twitter username?')
            resp = await self.conv.get_response()
            username = resp.text
            try:
                user = self.twitter_api.get_user_if_exists(username)
                self.db.add_new_user(user._json["screen_name"],
                                     user._json["id"])
                await self.conv.send_message(
                    'New user added to database.\nIt will start getting tracked soon.'
                )
                self.conv.cancel()
                return
            except UserNotFound:
                await self.conv.send_message(
                    'Username not found on the twitter.\nPlease check if username is correct.'
                )
                self.conv.cancel()
                return

    async def list_user(self, event):
        await event.delete()
        usernames = self.db.get_all_usernames()
        message = 'List of Users\n'
        for username in usernames:
            message = f'{message}\n{username}'
        await event.respond(message)

    async def remove_user(self, event):
        await event.delete()
        async with self.bot.conversation(event.message.peer_id) as self.conv:
            await self.conv.send_message(
                'Please enter new twitter username you want to remove?')
            resp = await self.conv.get_response()
            username = resp.text
            if not self.db.user_exists(username):
                await self.conv.send_message(
                    'This username does not exist on the bot.')
                return
            self.db.remove_user(username)
            await self.conv.send_message(f'{username} has been removed!')
            self.conv.cancel()
Пример #4
0
class Tracker:
    def __init__(self, queue):
        self.config = json.load(open('twitter_config.json'))
        self.queue = queue
        self.loop = asyncio.new_event_loop()
        self.log = logging.getLogger(' Tracker ').info
        self.count = 200

    def start(self):
        self.db = DB()
        self.loop.run_until_complete(self.main())

    async def main(self):
        self.log('Setting up Apps ...')
        await self.setup_apis()
        self.log("Followings tracker is ready!")
        while True:
            all_users = self.db.get_all_users()
            if len(all_users) == 0:
                await asyncio.sleep(10)
                continue
            for user in all_users:
                if not user['tracked']:
                    await self.track_user(user)
                    continue
                new_followings = await self.check_for_new_followings(user)
                for user_id in new_followings:
                    message = await self.create_message_for_tg(
                        user_id, user["username"])
                    self.queue.put(message)
            await asyncio.sleep(self.get_wait_time(len(all_users)))

    def get_wait_time(self, number_of_users):
        if number_of_users <= 5:
            return 300
        elif number_of_users > 5 and number_of_users <= 10:
            return 200
        elif number_of_users > 10:
            return 100

    async def create_message_for_tg(self, following_id, follower_username):
        api = next(self.random_api)
        user = api.get_user(user_id=following_id)
        username = user._json["screen_name"]
        profile_url = f'https://twitter.com/{username}'
        follower_profile_url = f'https://twitter.com/{follower_username}'
        message = f'[{follower_username}]({follower_profile_url}) just started to follow [{username}]({profile_url}).'
        return message

    async def check_for_new_followings(self, user):
        cur = user["cursor"]
        api = next(self.random_api)
        try:
            results = api.friends(user["username"],
                                  cursor=cur,
                                  count=self.count)
            await asyncio.sleep(1)
        except tweepy.error.RateLimitError:
            self.log('Hit rate limit on API, switching to next App.')
            await asyncio.sleep(300)
            return await self.check_for_new_followings(user)
        except tweepy.error.TweepError:
            self.log('Tweepy Error in check for new followings')
            await asyncio.sleep(300)
            return await self.check_for_new_followings(user)
        friends = results[0]
        followings_list = list()
        for friend in friends:
            followings_list.append(friend._json["id"])

        def filter_followings(user_id):
            if user_id in user["followings_list"]:
                return False
            return True

        if results[1][1] != 0:
            new_cur = results[1][1]
            self.db.update_cursor(user["user_id"], new_cur)
        new_followings = list(filter(filter_followings, followings_list))
        if len(new_followings) != 0:
            self.db.extend_users_followings_list(user["user_id"],
                                                 new_followings)
        return new_followings

    def get_random_api(self):
        while True:
            total = len(self.authenticated_apps)
            if total == 1:
                index = 0
            else:
                index = randrange(0, total - 1)
            yield self.authenticated_apps[index]

    async def track_user(self, user):
        username = user["username"]
        cur = -1
        followings_list = list()
        api = next(self.random_api)
        while True:
            try:
                results = api.friends(username, cursor=cur, count=self.count)
                await asyncio.sleep(1)
            except tweepy.error.RateLimitError:
                self.log('Hit rate limit on API, switching to next App.',
                         exc_info=True)
                await asyncio.sleep(300)
                api = next(self.random_api)
                continue
            except tweepy.error.TweepError:
                self.log('Tweepy error in track_user', exc_info=True)
                await asyncio.sleep(300)
                continue
            friends = results[0]
            for friend in friends:
                followings_list.append(friend._json["id"])
            if results[1][1] == 0:
                break
            cur = results[1][1]
        self.db.update_cursor(user["user_id"], cur)
        self.db.extend_users_followings_list(user["user_id"], followings_list)
        self.db.set_user_tracked(user["user_id"])

    async def setup_apis(self):
        creds = list(self.config.get("TWITTER_APPS_CREDS"))
        self.authenticated_apps = []
        for cred in creds:
            API_KEY = cred.get("APP_API_KEY")
            API_KEY_SECRET = cred.get("APP_API_KEY_SECRET")
            ACCESS_TOKEN = cred.get("APP_ACCESS_TOKEN")
            ACCESS_TOKEN_SECRET = cred.get("APP_ACCESS_TOKEN_SECRET")
            auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
            auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
            api = tweepy.API(auth)
            self.authenticated_apps.append(api)
        self.random_api = self.get_random_api()
Пример #5
0
 def start(self):
     self.db = DB()
     self.loop.run_until_complete(self.main())
 def __init__(self, bot: TelegramClient):
     self.bot = bot
     self.twitter_api = TwitterAPI()
     self.db = DB()
Пример #7
0
 def __init__(self, bot):
     self.bot = bot
     self.db = DB()
     self.tz_name = 'America/New_York'
     self.tz = pytz.timezone(self.tz_name)
Пример #8
0
    channel = channel.strip()
    print(f'{guild} --> {channel}')
    if guild in guildsList:
        print('Valid Server')
        channels = config.get("SERVERS").get(guild)
        if channel in channels:
            print('Valid channel')
            if await valid(message.content):
                print('Sending to target!')
                await sendToTarget(message)


async def sendToTarget(message):
    embed = Embed(title='Keyword Detected!')
    embed.add_field(name='Server Name', value=message.guild.name, inline=False)
    embed.add_field(name='Channel Name',
                    value=message.channel.name,
                    inline=False)
    embed.add_field(name='User', value=message.author.name, inline=False)
    embed.description = message.content
    async with aiohttp.ClientSession() as session:
        webhook = Webhook.from_url(config.get("OUTPUT_CHANNEL_WEBHOOK"),
                                   adapter=AsyncWebhookAdapter(session))
        await webhook.send(embed=embed, username='******')


db = DB(dbQueue)
threading.Thread(target=db.start).start()

client.run(config.get("USER_TOKEN"), bot=False)
Пример #9
0
class Scraper:
    def __init__(self, bot):
        self.config = json.load(open('config.json'))
        self.db = DB()
        self.bot = bot
        self.loop = asyncio.new_event_loop()
        self.URL = 'https://ftmo.com/en/calendar/'
        self.itter_time = 300
        # self.jobstore = {
        #     'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
        # }
        self.schedular = AsyncIOScheduler(event_loop=self.loop)
        self.schedular.start()
        # self.schedular.print_jobs()
        self.tz_name = 'America/New_York'
        self.tz = pytz.timezone(self.tz_name)
        self.start_driver()

    @staticmethod
    def kill_chrome():
        name = 'chrom'
        try:
            # iterating through each instance of the proess
            for line in os.popen("ps ax | grep " + name + " | grep -v grep"):
                fields = line.split()
                # extracting Process ID from the output
                pid = fields[0]
                # terminating process
                os.kill(int(pid), signal.SIGKILL)
            print("Process Successfully terminated")
        except:
            pass

    def start_driver(self):
        self.kill_chrome()
        self.options = webdriver.ChromeOptions()
        self.options.add_argument('--no-sandbox')
        self.options.add_argument('--headless')
        self.webdriver_path = self.config.get("WEBDRIVER_PATH")
        self.driver = webdriver.Chrome(executable_path=self.webdriver_path,
                                       options=self.options)
        self.driver.implicitly_wait(10)

    def start(self, queue):
        self.queue = queue
        self.loop.run_until_complete(self.main())

    async def main(self):
        while True:
            events = await self.get_all_events()
            for event in events:
                if not self.db.event_exists(event.id):
                    await self.schedule_notification(event)
                    self.db.add_event(event)
            await asyncio.sleep(self.itter_time)

    async def schedule_notification(self, event):
        trigger = DateTrigger(run_date=event.event_time - timedelta(hours=1),
                              timezone=self.tz_name)
        # trigger = DateTrigger(run_date=dt.now() + timedelta(seconds=30))
        self.schedular.add_job(send_event_notification,
                               trigger=trigger,
                               args=(event, self.queue),
                               replace_existing=True)

    async def get_all_events(self):
        try:
            self.driver.get(self.URL)
        except Exception as e:
            print('Unexpected exception')
            traceback.print_exc()
            print('Restarting the driver')
            self.start_driver()
            return await self.get_all_events()
        try:
            WebDriverWait(self.driver, 30).until(
                EC.presence_of_all_elements_located(
                    (By.CLASS_NAME, 'macroCal')))
        except exceptions.TimeoutException:
            return []
        except Exception as e:
            print('Unexpected exception')
            traceback.print_exc()
            return []
        table = self.driver.find_element_by_class_name("macroCal")
        tbody = table.find_element_by_tag_name("tbody")
        rows = tbody.find_elements_by_class_name("fundament")
        events = list()
        for row in rows:
            event = Event()
            try:
                event.title = row.find_elements_by_tag_name("td")[0].text
                event.currency = row.find_elements_by_tag_name("td")[1].text
                timestamp = row.get_attribute("data-timestamp")
                date_time = dt.fromtimestamp(int(timestamp), tz=self.tz)
                event.date = f'{date_time.year}/{date_time.month}/{date_time.day}'
                event.time = f'{date_time.hour}:{date_time.minute}'
                event.event_time = dt.fromtimestamp(int(timestamp), tz=self.tz)
                event.id = f'{event.title}{event.currency}'.encode(
                    'utf-8').hex()
                events.append(event)
            except exceptions.StaleElementReferenceException:
                return events
            except Exception as e:
                print('Unexpected exception')
                traceback.print_exc()
                return events
        return events