Skip to content

Instantly share code, notes, and snippets.

@athphane
Last active February 23, 2018 07:16
Show Gist options
  • Save athphane/bc8d6866de30de347406919df8599998 to your computer and use it in GitHub Desktop.
Save athphane/bc8d6866de30de347406919df8599998 to your computer and use it in GitHub Desktop.
Python script to send message through telegram about qBitTorrent download completion.
import os , sys, string, time, random
from datetime import datetime
from os import linesep
from decimal import *
# Telepot library from pip
import telepot
#Random Sys.Argv replacements for testing purposes.
#This comes in handy more than I thought it would.
names = ['ubuntu-16.04.1-desktop-amd64.iso', 'Storks (2016) [1080p] [YTS.AG]', 'Mom.S04E08.HDTV.x264-LOL[eztv].mkv', '20160915_madsonic-6.1.8700-setup-x86.zip', 'Peace Is The Mission']
labels = ['Movie', 'TV Show', 'Music', 'Program' ,'Game', 'eBook']
sizes = ['500000', '20000', '777758998']
name_rand = (random.choice(names))
label_rand = (random.choice(labels))
sizes_rand = (random.choice(sizes))
# System arguements passed from qBittorrent to this script
torrent_name = sys.argv[1]
torrent_category = sys.argv[2]
torrent_size = sys.argv[3]
# Change torrent_size into something understandable by the script
size = Decimal(torrent_size)
# The following is a thing I did so that I could see if
# I needed the final message to say MB or GB
# And also to play with IF statements.
#megabyte divisor
megabyte_divisor = Decimal(1048576)
gigabyte_divisor = Decimal(1073741824)
# You know all of this made sense when i first made
# this script. Now it's just so complicated lmao.
# If im not wrong, this checks if the bytes are smaller
# than a GB and then converts to actual MB. and same thing
# for the GB statement.
if size < 1073741824:
megabyte_value = size / megabyte_divisor
final = str(round(megabyte_value,2))
tg_size = final + " MB"
else:
if size >= 1073741824:
gigabyte_value = size / gigabyte_divisor
final = str(round(gigabyte_value,2))
tg_size = final + " GB"
#Telegram API Key and authentication sequence.
api_key = "API_KEY"
bot = telepot.Bot(api_key)
myID = YOUR_ID
#Telegram message templates
t = time.localtime()
date_run = time.asctime(t)
tg_title = "*" + torrent_category + " Download Completed" + "*"
tg_message = tg_title + "\n" + "Torrent: " + torrent_name + "\n" + "Size: " + tg_size + "\n" + "Label: " + torrent_category + "\n" + "Run on: " + date_run
tg_message_debug = "Torrent: " + torrent_name + "\n" + "Size: " + tg_size + "\n" + "Label: " + torrent_category + "\n" + "Run on: " + date_run + "\n"
csv_file_write = torrent_name + "," + tg_size + "," + torrent_category + "," + date_run
# Relative paths were needed cause the script was throwing out
# errors and the files were not being written/created when
# the script that running.
def write_file():
file = open("C:\Scripts\debug.txt", "a")
file.write(tg_message_debug + "\n")
file.close()
def write_database():
database = open ("C:\Scripts\database.csv", "a")
database.write(csv_file_write + "\n")
database.close()
def send_telegram():
bot.sendMessage(myID, tg_message ,parse_mode="Markdown", disable_web_page_preview=1)
write_file()
write_database()
send_telegram()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment