<commit_before># -*- coding: utf-8 -*-
import socket
import threading
import time
import configparser
import os

import schedule
import telegram
from flask import Flask, request
from rtorrent import RTorrent

SECTION = 'main'

__author__ = 'z'

ANT_COUNT = 1
job = None

config = configparser.RawConfigParser()
config.read('config.cfg')


def get_rtorrent():
    return RTorrent()


def get_config_key(key):
    try:
        return config.get(section=SECTION, option=key)
    except configparser.NoOptionError:
        return None


def set_config_key(key, value):
    return config.set(section=SECTION, option=key, value=value)


LAST_UPDATE_ID = get_config_key('last_update_id')

rt = RTorrent(url="http://localhost", _verbose=True)


# torrents = rt.torrents()

# print(torrents)


# CERT = get_crt()
# CERT_KEY = get_key()
# CHAT_ID = get_chat_id()


def create_bot():
    global bot
    bot = telegram.Bot(token=(get_config_key('token')))
    # print(bot.getMe())
    print(bot.setWebhook(webhook_url=get_config_key('url'), certificate=open(get_config_key('crt'), 'rb')))


create_bot()


# print(bot.getUpdates()[-1].message.text)


app = Flask(__name__)


def other_job():
    bot.sendMessage(chat_id=get_config_key('chat_id'), text=os.popen('date').read())


def test_job():
    global ANT_COUNT
    bot.sendMessage(chat_id=get_config_key('chat_id'), text=telegram.Emoji.ANT * ANT_COUNT)


def schedule_start():
    global cease_continuous_run
    # schedule.every(10).seconds.do(test_job)
    cease_continuous_run = threading.Event()

    class ScheduleThread(threading.Thread):
        @classmethod
        def run(cls):
            while not cease_continuous_run.is_set():
                schedule.run_pending()
                time.sleep(1)

    continuous_thread = ScheduleThread()
    continuous_thread.start()


schedule_start()


@app.route("/")
def hello():
    print("Hello")
    return 'hello'


@app.route("/hook", methods=['POST'])
def hook():
    # print("hook")
    if request.method == "POST":
        global ANT_COUNT
        global job
        # retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(request.get_json(force=True))
        set_config_key('last_update_id', update.update_id)

        print(update.message)
        chat_id = update.message.chat.id
        msg_id = update.message.message_id

        # Telegram understands UTF-8, so encode text for unicode compatibility
        text = update.message.text.encode('utf-8')
        if text:
            # print(text[0], text[1], text[2], text[3])
            # print(text[0:4])
            print(text)
            if text.lower() == '/cmd':
                custom_keyboard = [
                    [telegram.Emoji.THUMBS_UP_SIGN, telegram.Emoji.THUMBS_DOWN_SIGN, telegram.Emoji.ALARM_CLOCK,
                     telegram.Emoji.ALIEN_MONSTER]]
                reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
                bot.sendMessage(chat_id=chat_id, text=socket.gethostname(), reply_to_message_id=msg_id,
                                reply_markup=reply_markup)
            elif text.lower() == 'status':
                bot.sendMessage(chat_id=chat_id, text='Online. I\'m ' + bot.getMe().username)
            elif text.lower() == "ls":
                t = os.popen("find /misc/age/Подкасты/ -type f -newer /misc/age/Подкасты/last").read()
                bot.sendMessage(chat_id=chat_id, text=t)
            elif text == telegram.Emoji.ALARM_CLOCK:
                job = schedule.every(5).seconds.do(other_job)
                bot.sendMessage(chat_id=chat_id, text="set alarmClock")
            elif text == telegram.Emoji.ALIEN_MONSTER:
                for job in schedule.jobs:
                    schedule.cancel_job(job)
            elif text == telegram.Emoji.THUMBS_UP_SIGN:
                ANT_COUNT += 1
                bot.sendMessage(chat_id=chat_id, text=str(ANT_COUNT))<commit_msg>add status rtorrent<commit_after># -*- coding: utf-8 -*-
import socket
import threading
import time
import configparser
import os

import schedule
import telegram
from flask import Flask, request
from rtorrent import RTorrent

SECTION = 'main'

__author__ = 'z'

ANT_COUNT = 1
job = None

config = configparser.RawConfigParser()
config.read('config.cfg')


def get_rtorrent():
    return RTorrent()


def get_config_key(key):
    try:
        return config.get(section=SECTION, option=key)
    except configparser.NoOptionError:
        return None


def set_config_key(key, value):
    return config.set(section=SECTION, option=key, value=value)


LAST_UPDATE_ID = get_config_key('last_update_id')

rt = RTorrent(url="http://localhost", _verbose=True)


# rt.load_torrent_simple(
#     "magnet:?xt=urn:btih:d5bc08ec2c2a8f1c2a133850e126cc9922214399"
#     "&dn=Spotless%20%20-%20Season%201%20%28AlexFilm%29%20BDRip%201080p", "url", True, True)


# torrents = rt.torrents()

# print(torrents)


# CERT = get_crt()
# CERT_KEY = get_key()
# CHAT_ID = get_chat_id()


def create_bot():
    global bot
    bot = telegram.Bot(token=(get_config_key('token')))
    # print(bot.getMe())
    print(bot.setWebhook(webhook_url=get_config_key('url'), certificate=open(get_config_key('crt'), 'rb')))


create_bot()


# print(bot.getUpdates()[-1].message.text)


app = Flask(__name__)


def other_job():
    bot.sendMessage(chat_id=get_config_key('chat_id'), text=os.popen('date').read())


def test_job():
    global ANT_COUNT
    bot.sendMessage(chat_id=get_config_key('chat_id'), text=telegram.Emoji.ANT * ANT_COUNT)


def schedule_start():
    global cease_continuous_run
    # schedule.every(10).seconds.do(test_job)
    cease_continuous_run = threading.Event()

    class ScheduleThread(threading.Thread):
        @classmethod
        def run(cls):
            while not cease_continuous_run.is_set():
                schedule.run_pending()
                time.sleep(1)

    continuous_thread = ScheduleThread()
    continuous_thread.start()


schedule_start()


@app.route("/")
def hello():
    print("Hello")
    return 'hello'


@app.route("/hook", methods=['POST'])
def hook():
    # print("hook")
    if request.method == "POST":
        global ANT_COUNT
        global job
        # retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(request.get_json(force=True))
        set_config_key('last_update_id', update.update_id)

        print(update.message)
        chat_id = update.message.chat.id
        msg_id = update.message.message_id

        # Telegram understands UTF-8, so encode text for unicode compatibility
        text = update.message.text.encode('utf-8')
        if text:
            # print(text[0], text[1], text[2], text[3])
            # print(text[0:4])
            print(text)
            if text.lower() == '/cmd':
                custom_keyboard = [
                    [telegram.Emoji.THUMBS_UP_SIGN, telegram.Emoji.THUMBS_DOWN_SIGN, telegram.Emoji.ALARM_CLOCK,
                     telegram.Emoji.ALIEN_MONSTER]]
                reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
                bot.sendMessage(chat_id=chat_id, text=socket.gethostname(), reply_to_message_id=msg_id,
                                reply_markup=reply_markup)
            elif text.lower() == 'status':
                msg = 'Online. I\'''m ' + bot.getMe().username + '\n'
                msg += 'Torrents:\n'
                for torrent in rt.torrents:
                    percent = float(torrent.get_completed_bytes()) / float(torrent.get_size_bytes()) * 100.0
                    msg += '*** %s  [%2.2f%%] ***\n' % (torrent.get_name(), percent)
                bot.sendMessage(chat_id=chat_id, text=msg)
            elif text[0:7] == "magnet:":
                rt.load_torrent_simple(text, "url", True, True)
                bot.sendMessage(chat_id=chat_id, text=text[0:7])
            elif text.lower() == "ls":
                t = os.popen("find /misc/age/Подкасты/ -type f -newer /misc/age/Подкасты/last").read()
                bot.sendMessage(chat_id=chat_id, text=t)
            elif text == telegram.Emoji.ALARM_CLOCK:
                job = schedule.every(5).seconds.do(other_job)
                bot.sendMessage(chat_id=chat_id, text="set alarmClock")
            elif text == telegram.Emoji.ALIEN_MONSTER:
                for job in schedule.jobs:
                    schedule.cancel_job(job)
            elif text == telegram.Emoji.THUMBS_UP_SIGN:
                ANT_COUNT += 1
                bot.sendMessage(chat_id=chat_id, text=str(ANT_COUNT))
{'fraction_non_alphanumeric': 0.1247236765561373, 'fraction_numerical': 0.010703897614892379, 'mean_word_length': 2.7228237332178433, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, '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': '1801013', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2794, 'n_words': 499}