View Single Post
Old 12-27-2021, 07:48 PM   #1
Tommalka
Connoisseur
Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.Tommalka ought to be getting tired of karma fortunes by now.
 
Posts: 50
Karma: 1088351
Join Date: Dec 2021
Device: Kobo Libra 2
[Concept] Telegram Bot for converting epubs to kepubs (and transferring)

I just wanted to share with you an approach to easily convert ePubs to Kepubs and a possibility to transfer them very easy wirelessly to a cloud: A Telegram Bot!

The simple script below will use Kepubify and is written in python using telegram-python-bot (can be installed with pip).
Function summarised: If you send an .epub in the Telegram chat of the bot, a Kepub will come back.
One could now very easy implement an upload to a cloud and use the KoboCloud hack on his Kobo for the perfect wireless expericence.

(I don't want to go into details about Telegram bots in general, just wanted to share the idea. There are many tutorials out there about hosting such a bot if you don't know how to)

Code:
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os

telegramToken = "1234567Placeholder"
nameOfKepubifyBinary = "kepubify-linux-64bit" #binary in the same folder as this script

def conversion(update, context):
    try:
        fileName = update.message.document.file_name #get name of document
        fileExtension = os.path.splitext(fileName)[1] #get only the extension
        if fileExtension.lower() == ".epub": 
            fileEpub = context.bot.getFile(update.message.document.file_id)
            update.message.reply_text("Processing the following file:\n\n" + fileName)
            fileEpub.download(fileName) #download the epub to the server
            #do the conversion to kepub
            os.system("./"+nameOfKepubifyBinary + " '" + fileName + "'") 
            #get the kepub
            fileKepub = fileName.replace(".epub", "_converted.kepub.epub")
            #send the kepub
            context.bot.send_document(chat_id=update.message.chat_id, document=open(fileKepub, 'rb'), caption="Here is your Kepub.")
            
            #now you can upload it to any cloud...
            
            #remove the files from the server
            os.remove(fileName)
            os.remove(fileKepub)
        else:
              update.message.reply_text("Error. You send me a "+fileExtension+"\nbut only .epubs are supported.")
    except: 
        update.message.reply_text("Unknown error")
    
def main():
    print("Bot started")
    updater = Updater(token=telegramToken, use_context=True)
    dp = updater.dispatcher
   
    #we get a document
    dp.add_handler(MessageHandler(Filters.document, conversion))

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
  main()

Last edited by Tommalka; 12-27-2021 at 07:52 PM.
Tommalka is offline   Reply With Quote