<commit_before>from discord.ext import commands
import asyncio
from cogs.utils import twitconn
from cogs.utils import checks
from discord.errors import Forbidden
import json
import os


class Streams:

    bot = None

    def __init__(self, bot):
        self.bot = bot
        self.loop = None
        self.destinations = None

        path = os.path.join(os.getcwd(), 'files', 'tweets.json')
        with open(path) as f:
            self.destinations = json.load(f)

        stalk = self.get_stalks()

        twitconn.init_stream(stalk)

        @bot.event
        async def on_ready():
            print('Ready')
            await self.stream()

        @bot.event
        async def on_resumed():
            print('Resumed')
            await self.reboot_stream()

    async def stream(self):
        if self.loop is None:
            await self.start()
        else:
            await self.reboot_stream()

    async def start(self):
        print('Now stalking')
        self.loop = asyncio.get_event_loop()
        self.loop.create_task(self.tweet_retriever())

    async def end(self):
        print('Ending stalking')
        self.stop_loop = True
        self.loop.stop()
        self.loop = None

    async def tweet_retriever(self):
        print('kek')
        await self.bot.wait_until_ready()
        self.stop_loop = False
        while not self.stop_loop:
            if not twitconn.poster.running:
                print('Disconnected')
                await self.reboot_stream()
                print('Reconnected')
                await asyncio.sleep(20)
                continue
            statuses = twitconn.stream_new_tweets()
            while len(statuses) > 0:
                fstatus = statuses.pop(0)
                id = str(fstatus.user.id)

                status = twitconn.encode_status(fstatus)

                targets = self.destinations['destinations']
                try:
                    channels = targets[id]
                except KeyError:
                    continue

                if channel == None:
                    continue

                for channel in channels:
                    if channel in self.destinations['blacklist']:
                        continue
                    try:
                        send = self.bot.get_channel(channel)
                        await self.bot.send_message(send, status)
                    except Forbidden as e:
                        print(channel)
                        print(e)
            await asyncio.sleep(5)

    @commands.command(hidden=True)
    @checks.is_owner()
    async def reboot(self):
        print('command????')
        await self.bot.say('Attempting to restart stream!')
        await self.kill_stream()
        await self.bot.say('Stream killed!')
        await self.bot.say('Restarting stream...')
        await self.restart_stream()
        await self.bot.say('Restart successful...?')<commit_msg>Never push without testing.<commit_after>from discord.ext import commands
import asyncio
from cogs.utils import twitconn
from cogs.utils import checks
from discord.errors import Forbidden
import json
import os


class Streams:

    bot = None

    def __init__(self, bot):
        self.bot = bot
        self.loop = None
        self.destinations = None

        path = os.path.join(os.getcwd(), 'files', 'tweets.json')
        with open(path) as f:
            self.destinations = json.load(f)

        stalk = self.get_stalks()

        twitconn.init_stream(stalk)

        @bot.event
        async def on_ready():
            print('Ready')
            await self.stream()

        @bot.event
        async def on_resumed():
            print('Resumed')
            await self.reboot_stream()

    async def stream(self):
        if self.loop is None:
            await self.start()
        else:
            await self.reboot_stream()

    async def start(self):
        print('Now stalking')
        self.loop = asyncio.get_event_loop()
        self.loop.create_task(self.tweet_retriever())

    async def end(self):
        print('Ending stalking')
        self.stop_loop = True
        self.loop.stop()
        self.loop = None

    async def tweet_retriever(self):
        print('kek')
        await self.bot.wait_until_ready()
        self.stop_loop = False
        while not self.stop_loop:
            if not twitconn.poster.running:
                print('Disconnected')
                await self.reboot_stream()
                print('Reconnected')
                await asyncio.sleep(20)
                continue
            statuses = twitconn.stream_new_tweets()
            while len(statuses) > 0:
                fstatus = statuses.pop(0)
                id = str(fstatus.user.id)

                status = twitconn.encode_status(fstatus)

                targets = self.destinations['destinations']
                try:
                    channels = targets[id]
                except KeyError:
                    continue

                if channels == None:
                    continue

                for channel in channels:
                    if channel in self.destinations['blacklist']:
                        continue
                    try:
                        send = self.bot.get_channel(channel)
                        await self.bot.send_message(send, status)
                    except Forbidden as e:
                        print(send.name)
                        print(e)
            await asyncio.sleep(5)

    @commands.command(hidden=True)
    @checks.is_owner()
    async def reboot(self):
        print('command????')
        await self.bot.say('Attempting to restart stream!')
        await self.kill_stream()
        await self.bot.say('Stream killed!')
        await self.bot.say('Restarting stream...')
        await asyncio.sleep(60)
        await self.restart_stream()
        await self.bot.say('Restart successful...?')
{'fraction_non_alphanumeric': 0.09244549602839276, 'fraction_numerical': 0.002028054757478452, 'mean_word_length': 1.5475678002582867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2096709', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1582, 'n_words': 370}