{ "cells": [ { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "import tqdm\n", "import telepot\n", "from tg_tqdm import tg_tqdm\n", "import time\n", "import requests" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "tg_token = '831964163:AAH7SoaoqWzWIcHaS3yfdmMu-H46hhtUaXw'\n", "tg_chat_id = 1147194\n", "ik_chat_id = 94616973\n", "sun_group_id = -321681009" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bot = telepot.Bot(tg_token)\n", "# bot.getUpdates()\n", "# message = bot.sendMessage(chat_id=sun_group_id, text='Я счас запиню этот чат, попробуем!')\n", "bot\n", "send_message_url = \"https://api.telegram.org/bot{}/sendMessage\".format(tg_token)\n", "pin_message_url = \"https://api.telegram.org/bot{}/pinChatMessage\".format(tg_token)\n", "media_message_url = \"https://api.telegram.org/bot{}/sendPhoto\".format(tg_token)\n", "change_media_url = \"https://api.telegram.org/bot{}/editMessageMedia\".format(tg_token)\n", "\n", "# send_responce = requests.get(send_message_url, params={'chat_id':tg_chat_id, 'text':'THIS IS WILL BE PIN'})\n", "# send_responce\n", "\n", "# pin_responce = requests.get(pin_message_url, params={'chat_id':send_responce.json()['result']['chat']['id'],\n", "# 'message_id':send_responce.json()['result']['message_id']}).text\n", "# pin_responce\n", "\n", "# with open('image.png', 'rb') as photo:\n", "# media_responce = requests.post(media_message_url, data={'chat_id':tg_chat_id}, files={'photo':photo})\n", "# media_responce\n", "\n", "with open('image2.png', 'rb') as photo:\n", " change_responce = requests.post(change_media_url,\n", " data={\n", " 'chat_id': media_responce.json()['result']['chat']['id'],\n", " 'message_id': media_responce.json()['result']['message_id'],\n", " 'media': '{\"type\": \"photo\", \"media\": \"attach://image2.png\"}'\n", " }, headers={\"Content-Type\": \"multipart/form-data\"})\n", " \n", "change_responce\n" ] }, { "cell_type": "code", "execution_count": 338, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6445" ] }, "execution_count": 338, "metadata": {}, "output_type": "execute_result" } ], "source": [ "args = {'data':{'media':None}}\n", "from json import JSONEncoder\n", "import json\n", "media = {}\n", "# file = ['image.png']\n", "# type = 'photo'\n", "media = {'type':'photo', 'media': 'attach://image2.png'}\n", "# JSONEncoder().encode(media)\n", "# args['data']['media'] = media\n", "# args\n", "media = JSONEncoder().encode(media)\n", "media\n", "media_responce.json()['result']['message_id']" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ok': False,\n", " 'error_code': 400,\n", " 'description': \"Bad Request: can't parse InputMedia: Can't parse media JSON object\"}" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# media_responce.json()\n", "change_responce.json()\n", "# ??requests.Session" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# telepot.Bot.\n", "bot.sendMessage(ik_chat_id, \"Hi! Why in English? Let's talk in Russian. Потому что я ж русский бот, пусть и курва!\")\n", "bot.sendMessage(ik_chat_id, \"Я бот Тимура, который создан для того, чтобы следить за обученим и всякими длительными процессами.\\\n", " Так что сюда буду присылать всякие обновления, что Тимур посчитатет нужным вам прислать.\")\n", "bot.sendMessage(chat_id=ik_chat_id, text=\"Потому что curve занято и скучно)\", reply_to_message_id=41)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from telepot.loop import MessageLoop\n", "import telepot \n", "bot = telepot.Bot(tg_token)\n", "# msg = bot.sendMessage(tg_chat_id, 'pidor', disable_notification=True)\n", "# telepot.Bot(tg_token).editMessageText((tg_chat_id, id), 'suka')\n", "# telepot.telepot.loop.MessageLoop.cancel()\n", "# with open('im.png', 'rb') as file:\n", "# pic_msg = bot.sendPhoto(tg_chat_id, file)\n", "pic_msg\n", "bot.ed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# bot._api_request('edit')\n", "# bot._api_request('sendMessage', params={'chat_id':tg_chat_id, 'text':'test'})\n", "# msg_id = 37\n", "# with open('image.png.png', 'rb') as photo:\n", "# pic_msg = bot._api_request_with_file('sendPhoto', {'chat_id':tg_chat_id}, 'photo', photo)\n", "# pic_msg\n", "# bot._api_request('editMessageText', {'chat_id':tg_chat_id, 'message_id': msg_id, 'text':'test_edited'})\n", "with open('image2.png') as file:\n", " bot._api_request_with_file('editMessageMedia', {'chat_id':media_responce.json()['result']['chat']['id'], \n", " 'message_id': media_responce.json()['result']['message_id']}, \n", " 'media', {'type': 'photo', 'media':'image2.png'})\n", "# bot._api_request_with_file('editMessageMedia', {'chat_id':tg_chat_id, 'message_id': msg_id}, 'media', 'attach://imf.png')\n", "# with open('im.png', 'rb') as f1: \n", "# with open('imf.png', 'rb') as f2:\n", "# bot.sendMediaGroup(tg_chat_id, ('', f1))\n", "def editMessageMedia(bot, msg_identifier, media,\n", " reply_markup=None):\n", " \"\"\"\n", " See: https://core.telegram.org/bots/api#editmessagemedia\n", "\n", " :param msg_identifier: Same as ``msg_identifier`` in :meth:`telepot.Bot.editMessageText`\n", "\n", " :param media:\n", " Same as ``media`` in :meth:`telepot.Bot.sendMediaGroup`, except that here is a single\n", " `InputMedia `_ object, not an array.\n", " \"\"\"\n", " p,f = bot._split(locals(), discard=['msg_identifier', 'media'])\n", " p.update(bot._dismantle_message_identifier(msg_identifier))\n", "\n", " legal_media, files_to_attach = bot._split_input_media_array([media])\n", " p['media'] = legal_media[0]\n", "\n", " return bot._api_request('editMessageMedia', bot._rectify(p), files_to_attach)\n", "editMessageMedia(bot, (:media_responce.json()['result']['chat']['id'], media_responce.json()['result']['message_id'] ))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time\n", "from tg_tqdm import tg_tqdm\n", "\n", "for _ in tqdm.tqdm(range(5), leave=True, unit_scale = True, desc='MAIN PROGRESS'):\n", " time.sleep(.5)\n", "# tqdm.tqdm.write('pidor')\n", "# print('pidor')\n", "\n", "# for i in tqdm.tqdm(desc=)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del(tg_tqdm)\n", "from tg_tqdm import tg_tqdm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for _ in tg_tqdm(range(5),tg_token, [ik_chat_id, tg_chat_id], decs='MAIN PROGRESS'):\n", " time.sleep(1)" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'image.png'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mphoto\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mbot\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msendMediaGroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmedia_responce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'chat'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'id'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmedia\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'photo'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mphoto\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\telepot\\__init__.py\u001b[0m in \u001b[0;36msendMediaGroup\u001b[1;34m(self, chat_id, media, disable_notification, reply_to_message_id)\u001b[0m\n\u001b[0;32m 645\u001b[0m \"\"\"\n\u001b[0;32m 646\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_strip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlocals\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmore\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'media'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 647\u001b[1;33m \u001b[0mlegal_media\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfiles_to_attach\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_split_input_media_array\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmedia\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 648\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 649\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'media'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlegal_media\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\telepot\\__init__.py\u001b[0m in \u001b[0;36m_split_input_media_array\u001b[1;34m(media_array)\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 282\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 283\u001b[1;33m \u001b[0mms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mensure_dict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmedia_array\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 285\u001b[0m \u001b[0mused_names\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mgiven_attach_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mms\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mgiven_attach_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\telepot\\__init__.py\u001b[0m in \u001b[0;36m\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 282\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 283\u001b[1;33m \u001b[0mms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mensure_dict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmedia_array\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 285\u001b[0m \u001b[0mused_names\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mgiven_attach_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mm\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mms\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mgiven_attach_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\telepot\\__init__.py\u001b[0m in \u001b[0;36mensure_dict\u001b[1;34m(input_media)\u001b[0m\n\u001b[0;32m 244\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0minput_media\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 245\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 246\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 247\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 248\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mgiven_attach_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput_media\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: " ] } ], "source": [ "with open('image.png', 'rb') as photo:\n", " bot.sendMediaGroup(media_responce.json()['result']['chat']['id'], media=('photo', photo))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tqdm.tqdm_notebook(total=5) as t:\n", " for i in [1,2,3,4,5,6]:\n", " time.sleep(1)\n", " t.update(3)\n", " t.write('pidor')\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "with tqdm.tnrange(10) as t:\n", " for i in t:\n", " # Description will be displayed on the left\n", " t.set_description('GEN %i' % i)\n", " # Postfix will be displayed on the right,\n", " # formatted automatically based on argument's datatype\n", " t.set_postfix(loss=random.random(), gen=random.randint(1,999), str='h',\n", " lst=[1, 2])\n", " time.sleep(0.1)\n", "\n", "with tqdm.tqdm_notebook(total=10, bar_format=\"{postfix[0]} {postfix[1][value]:>8.2g}\",\n", " postfix=[\"Batch\", dict(value=0)]) as t:\n", " for i in range(10):\n", " time.sleep(0.1)\n", " t.postfix[1][\"value\"] = i / 2\n", " t.update()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bot.sendMessage(chat_id=ik_chat_id, chat_id=tg_chat_id, 'test')" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "from tg_tqdm import tg_tqdm\n", "with tg_tqdm(range(5), token=tg_token,chat_id=tg_chat_id, desc='DOWNLOAD', dynamic_ncols=False) as t:\n", " for i in t:\n", " time.sleep(1)\n", " t.set_postfix(dom='fon')\n" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "71c982004313486baf488d8cfea222e4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, description='Download', max=5, style=ProgressStyle(description_width='init…" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "with tqdm.tqdm_notebook(desc='Download', total=5, postfix='pidor', dynamic_ncols=0) as t:\n", " for i in range(3):\n", " \n", "# t.display(msg='display!!!', pos=30)\n", " time.sleep(1)\n", " t.set_postfix(govno='pidor')\n", " t.update(i)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e80ec189f8134916a7c1798a2f410811", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import tqdm\n", "t = tqdm.tqdm_notebook(range(10))\n", "\n", "for i in t:\n", " time.sleep(.1)\n", " t.set_postfix_str('podor {}'.format(i*i))" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "GEN 10: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:30<00:00, 3.02s/it, gen=301, loss=0.199, lst=[1, 2], str=h]\n" ] } ], "source": [ "import random\n", "del(t)\n", "with tqdm.tqdm(range(10)) as t:\n", " for i in t:\n", " # Description will be displayed on the left\n", " t.set_description('GEN {}'.format(i+1))\n", " # Postfix will be displayed on the right,\n", " # formatted automatically based on argument's datatype\n", " t.set_postfix(loss=random.random(), gen=random.randint(1,999), str='h',\n", " lst=[1, 2])\n", " time.sleep(3)\n", "# t.update()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "19e323a835ab41f484552bd952242e1f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "with tqdm.tqdm_notebook(total=10, bar_format=\"{postfix[0]} {postfix[1][value]:>8.2g}\",\n", " postfix=[\"Batch\", dict(value=0)]) as t:\n", " for i in range(10):\n", " time.sleep(0.5)\n", "# t.postfix[0] = 'go?'\n", " t.postfix[1][\"value\"] = i / 2\n", " t.update(1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'tg_logger' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mdel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtg_logger\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mdel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlogging\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlogger_tg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpython_telegram_logger\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mtg_logger\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mlogging\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'tg_logger' is not defined" ] } ], "source": [ "del(tg_logger)\n", "del(logging)\n", "del(logger_tg)\n", "import python_telegram_logger as tg_logger\n", "import logging\n", "\n", "logger_tg = logging.getLogger('tg_test')\n", "tg_handler = tg_logger.Handler(tg_token, [sun_group_id])\n", "tg_handler.setFormatter(\n", " tg_logger.HTMLFormatter({\"fmt\": \" *%(levelname)s* _%(name)s : %(funcName)s_\"})\n", ")\n", "logger_tg.addHandler(tg_handler)\n", "logger_tg.setLevel(logging.INFO)\n", "\n", "\n", "logger_tg.info('PIDOR')\n", "time.sleep(3)\n", "logger_tg.error('KURVA')\n" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "import tqdm\n", "import urllib\n", "def hook(t):\n", " \"\"\"Wraps tqdm instance.\n", " Don't forget to close() or __exit__()\n", " the tqdm instance once you're done with it (easiest using `with` syntax).\n", " Example\n", " -------\n", " >>> with tqdm(...) as t:\n", " ... reporthook = my_hook(t)\n", " ... urllib.urlretrieve(..., reporthook=reporthook)\n", " \"\"\"\n", " last_b = [0]\n", "\n", " def update_to(b=1, bsize=1, tsize=None):\n", " \"\"\"\n", " b : int, optional\n", " Number of blocks transferred so far [default: 1].\n", " bsize : int, optional\n", " Size of each block (in tqdm units) [default: 1].\n", " tsize : int, optional\n", " Total size (in tqdm units). If [default: None] remains unchanged.\n", " \"\"\"\n", " if tsize is not None:\n", " t.total = tsize\n", " t.update((b - last_b[0]) * bsize)\n", " last_b[0] = b\n", "\n", " return update_to\n", " " ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [], "source": [ "url = 'http://jsoc.stanford.edu'\n", "urlfits = url+'/SUM7/D157434620/S00007/magnetogram.fits'\n", "urlfits\n", "def tg_down():\n", " with tg_tqdm(range(1), tg_token, tg_chat_id) as t:\n", " myhook = hook(t)\n", " urllib.request.urlretrieve(urlfits, 'test', reporthook=myhook)\n", "def down():\n", " with tqdm.tqdm(range(1)) as t:\n", " myhook = hook(t)\n", " urllib.request.urlretrieve(urlfits, 'test', reporthook=myhook)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.003" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3e-3" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "with tg_tqdm(urls_store, tg_token, tg_chat_id) as t:\n", " for _ in urls_store.record:\n", " time.sleep(0.01)\n", " t.update()" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "record\n", "filename\n", "url\n" ] } ], "source": [ "for _ in urls_store.columns:\n", " print(_)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "744498f5560c4e318a14bd9893f5fc60", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=340), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "for i in tqdm.tqdm_notebook(urls_store.index):\n", "# print(urls_store.loc[i][1])\n", " time.sleep(0.1)\n" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ZeroDivisionError('division by zero')\n", "ZeroDivisionError('division by zero')\n", "ZeroDivisionError('division by zero')\n", "ZeroDivisionError('division by zero')\n" ] } ], "source": [ "for tg_tqdm(range(1000), tg_token, tg_chat_id):\n", " time.sleep(0.1)" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "with tg_tqdm(range(1), token=tg_token, chat_id=tg_chat_id, total=len(urls_store), postfix='batch') as t:\n", " lng = int(len(urls_store))\n", " r = [i for i in range(lng)]\n", " acc=1\n", " while len(r) > 0:\n", " ran = random.randint(40, 70)\n", " r = r[ran:]\n", " time.sleep(2)\n", " print(acc)\n", " t.update(ran)\n", " t.set_postfix(batch=acc)\n", " acc += 1\n", "# print(ran) \n", " \n", " t.update(ran)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "with tg_tqdm(urls_store.index[1:4], token=tg_token, chat_id=tg_chat_id, desc='DOWNLOAD BATCH') as batch_d:\n", " for ind in [1, 2, 3, 4]:\n", " with tg_tqdm(urls_store.index[1:4], token=tg_token, chat_id=tg_chat_id, desc='DOWNLOAD FITS') as url_d:\n", " myhook = hook(url_d)\n", " try:\n", " urllib.request.urlretrieve(urls_store.loc[ind][2], os.path.join('', urls_store.loc[ind][1]), reporthook=myhook)\n", " except Exception as e:\n", " print('Get error while trying download {}: {}'.format(urls_store.loc[ind][1], e))\n", " print('Skip this date')\n", " batch_d.update(i)\n" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "25596b764a8a411d970404953be3ba57", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, description='DOWNLOAD BATCH', max=3, style=ProgressStyle(description_width…" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "with tqdm.tqdm_notebook(urls_store.index[1:4], desc='DOWNLOAD BATCH') as batch_d:\n", " for ind in batch_d:\n", " try:\n", " urllib.request.urlretrieve(urls_store.loc[ind][2], os.path.join('', urls_store.loc[ind][1]))\n", " except Exception as e:\n", " print('Get error while trying download {}: {}'.format(urls_store.loc[ind][1], e))\n", " print('Skip this date')\n", "# batch_d.update(i)\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py:162: FutureWarning: the 'labels' keyword is deprecated, use 'codes' instead\n", " return cls.__new__(cls, **d)\n" ] } ], "source": [ "import pickle\n", "import urllib\n", "sunspots = pickle.load(urllib.request.urlopen('https://raw.githubusercontent.com/iknyazeva/FitsProcessing/master/sunspot_1996_2017.pkl'))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\n", "rc_list is empty - [], acc = 1\n", "rc_list is empty - [], acc = 2\n", "File with this date 2017-11-17 00:00:00 is not exist\n", "3\n", "rc_list is empty - [], acc = 4\n", "File with this date 2017-11-19 00:00:00 is not exist\n", "5\n", "rc_list is empty - [], acc = 6\n", "File with this date 2017-11-27 00:00:00 is not exist\n", "7\n", "rc_list is empty - [], acc = 8\n", "rc_list is empty - [], acc = 9\n", "File with this date 2017-11-30 00:00:00 is not exist\n", "10\n", "File with this date 2017-12-01 00:00:00 is not exist\n", "11\n", "File with this date 2017-12-07 00:00:00 is not exist\n", "12\n", "rc_list is empty - [], acc = 13\n", "File with this date 2017-12-11 00:00:00 is not exist\n", "14\n", "File with this date 2017-12-12 00:00:00 is not exist\n", "15\n", "File with this date 2017-12-13 00:00:00 is not exist\n", "16\n", "rc_list is empty - [], acc = 17\n", "rc_list is empty - [], acc = 18\n", "rc_list is empty - [], acc = 19\n", "File with this date 2017-12-24 00:00:00 is not exist\n", "20\n", "File with this date 2017-12-25 00:00:00 is not exist\n", "21\n", "rc_list is empty - [], acc = 22\n", "File with this date 2017-12-27 00:00:00 is not exist\n", "23\n", "File with this date 2017-12-28 00:00:00 is not exist\n", "24\n" ] } ], "source": [ "import random\n", "import glob\n", "TG =True\n", "SLEEP = 1\n", "crop_df = sunspots[(sunspots.index.get_level_values(0) > '2017-11-01')]\n", "with tg_tqdm(range(1), tg_token, tg_chat_id,\n", " total=len(crop_df.groupby(level=0)), desc='CROPPING PROGRESS') as tgt:\n", "\n", " acc = 0\n", " total = len(crop_df.groupby(level=0))\n", " print(total)\n", " for date, df in crop_df.groupby(level=0):\n", "\n", " if random.randint(0,1):\n", " rc_list = []\n", " else:\n", " rc_list = 'Something'\n", "\n", " if not rc_list:\n", " acc += 1\n", " time.sleep(SLEEP)\n", " tgt.update(acc)\n", " print('rc_list is empty - {}, acc = {}'.format(rc_list, acc))\n", " continue\n", "\n", " filename = 'hmi.m_720s.' + date.strftime('%Y%m%d_%H%M%S_TAI') + '*.fits'\n", " path = 'HMIdataset/'\n", " try:\n", " filepath = glob.glob(path + filename)[0]\n", " if TG:\n", " time.sleep(SLEEP)\n", " print('filepath: {}'.format(filepath))\n", " except IndexError as e:\n", " if TG:\n", " time.sleep(SLEEP)\n", " print('File with this date {} is not exist'.format(str(date)))\n", " acc += 1\n", " print(acc)\n", " continue\n", " except Exception as e:\n", " if TG:\n", " time.sleep(SLEEP)\n", " print('Some error with glob:'.format(e))\n", " acc += 1\n", " print(acc)\n", " continue\n", " type_mag = 'HMI'\n", "\n", " try:\n", " print('Reading filepath', filepath)\n", " print('Croping file', filepath)\n", " except ValueError as e:\n", " if TG:\n", " time.sleep(SLEEP)\n", " print('Get Exception while reading: {}'.format(e))\n", " print('Doing active farther, skip it.')\n", " # acc += 1\n", " # continue\n", " except Exception as e:\n", " if TG:\n", " time.sleep(SLEEP)\n", " print('Get Exception while reading: {}'.format(e))\n", " print('Doing active farther, skip it.')\n", " # acc += 1\n", " # continue\n", "\n", " # tgt.update()\n", " acc += 1\n", " print('acc = {}'.format(acc))\n", " if (acc % PROGRESS == 0) or (acc >= total):\n", " print('In if acc = {}'.format(acc))\n", " time.sleep(SLEEP)\n", " tgt.update(10)\n" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "is this!\n", "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "20\n", "is this!\n", "21\n", "22\n", "23\n", "24\n", "is end!\n" ] } ], "source": [ "# del(tg_tqdm)\n", "import telegram_tqdm\n", "# from telegram_tqdm import tg_tqdm\n", "from importlib import reload\n", "reload(telegram_tqdm)\n", "\n", "\n", "with telegram_tqdm.tg_tqdm(token=tg_token, chat_id=tg_chat_id, #iterable=None,\n", " total=24, desc='CROPPING PROGRESS') as tgt:\n", " for i in range(1,25):\n", " print(i)\n", " time.sleep(1)\n", " if (i % 10 == 0):\n", " print('is this!')\n", " tgt.update(10)\n", " elif (i == 24):\n", " print('is end!')\n", " tgt.update(24 % 10)\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "00:10 in total: 100%|0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000| 10/10 [00:10<00:00, 1.00s/it]\n" ] } ], "source": [ "import time\n", "from tqdm import tqdm\n", "class TqdmExtraFormat(tqdm):\n", " \"\"\"Provides a `total_time` format parameter\"\"\"\n", " @property\n", " def format_dict(self):\n", " d = super(TqdmExtraFormat, self).format_dict\n", " total_time = d[\"elapsed\"] * (d[\"total\"] or 0) / max(d[\"n\"], 1)\n", " d.update(total_time=self.format_interval(total_time) + \" in total\")\n", " return d\n", "\n", "for i in TqdmExtraFormat(\n", " range(10), ascii=\" .oO0\",\n", " bar_format=\"{total_time}: {percentage:.0f}%|{bar}{r_bar}\"):\n", " time.sleep(1)" ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".md", "format_name": "markdown", "format_version": "1.0", "jupytext_version": "0.8.6" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }