Browse Source

new file: youtubero-telegram.py
new file: youtubero.py

itec 4 years ago
commit
80dc16efc4
2 changed files with 180 additions and 0 deletions
  1. 49 0
      youtubero-telegram.py
  2. 131 0
      youtubero.py

+ 49 - 0
youtubero-telegram.py

@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+#Youtubero-telegram - simpatico script che esegue youtubero in modalita' bot di telegram
+
+import os
+import validators
+from telegram.ext import Updater, MessageHandler, Filters
+import youtubero
+
+
+def msg_parser(bot, update):
+    print("Messaggio ricevuto")
+    urlz = update.message.text
+
+    #update.message.reply_text("Ciao " + update.message.from_user.username)
+    update.message.reply_text("Messaggio ricevuto. Elaboro...")
+
+    for url in [s for s in urlz.splitlines() if s.strip() != ""]:
+        #update.message.reply_text("Scarico %s" %(url))
+        # start the download
+        dl = youtubero.download_video(url, update.message.from_user.username)
+        update.message.reply_text(dl)
+
+
+
+
+
+
+
+def main():
+    print ("Bot avviato")
+
+    # Create the EventHandler and pass it your bot's token.
+    updater = Updater(os.environ['TELEGRAM_TOKEN'])
+
+    # Get the dispatcher to register handlers
+    dp = updater.dispatcher
+
+    # parse message
+    dp.add_handler(MessageHandler(Filters.text, msg_parser))
+
+    # Start the Bot
+    updater.start_polling()
+
+    # Run the bot until you press Ctrl-C
+    updater.idle()
+
+
+if __name__ == '__main__':
+    main()

+ 131 - 0
youtubero.py

@@ -0,0 +1,131 @@
+#!/usr/bin/env python3
+
+#Youtubero - simpatico script che dato un link di youtube scarica i file rinominandoli a modino.
+
+#esempi presi da:
+#https://www.programcreek.com/python/example/98358/youtube_dl.YoutubeDL
+#https://github.com/ytdl-org/youtube-dl/blob/master/README.md
+
+
+from __future__ import unicode_literals
+import youtube_dl
+import shutil
+import sys
+import re
+import os
+import validators
+import glob
+import json
+
+
+def download_video(url, user = ""):
+    print ('--- Inizio ---')
+    if not os.path.exists("media"):
+        os.makedirs("media")
+    if not os.path.exists("temp"):
+        os.makedirs("temp")
+
+    ydl_opts = {
+        'format': 'bestaudio[ext=m4a]',       
+        'outtmpl': 'temp/%(id)s.m4a',        
+        'noplaylist': True,
+    }
+
+    url = url.strip()
+    print (url)
+    print (user)
+    
+    if not validators.url(url):
+       print ('--- URL malformato ---')
+       return ("Errore: url non valido")
+
+
+    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
+        try:
+            meta = ydl.extract_info(url, download = False)
+        except youtube_dl.DownloadError as detail:
+            print ('--- Errore video non disponibile ---')
+            print(str(detail))
+            return (str(detail))
+
+    id = meta.get('id').strip()
+    title = normalizetext(meta.get('title'))
+    alt_title = normalizetext(meta.get('alt_title'))
+    creator = normalizetext(meta.get('creator'))
+    track = normalizetext(meta.get('track'))
+    artist = normalizetext(meta.get('artist'))
+    album = normalizetext(meta.get('album'))
+        
+    print ('id          : %s' %(id))
+    print ('title       : %s' %(title))
+    print ('artist      : %s' %(artist))
+    print ('track       : %s' %(track))
+
+    #scrivo  il json
+    with open(os.path.join("temp", id + ".json"), 'w') as outfile:
+        json.dump(meta, outfile, indent=4)
+
+    #ho letto le info, ora controllo se il file esiste altrimenti lo scarico
+    #miglioria: controllare se upload_date e' uguale a quella del json gia' esistente
+    filetemp = os.path.join("temp", id + ".m4a")
+    if not glob.glob(filetemp):
+        print ('--- Scarico ---')
+        ydl.download([url]) #non ho capito perche' ma senza [] fa un carattere per volta
+    
+    #se il file esiste gia' in Media salto (potrebbe esserci, anche rinominato)
+    if glob.glob(os.path.join("media/" + user, '*[[]' + id + '[]]*')):
+        print ('--- Skippo ---')
+        return ("Errore: %s [%s] già presente" %(title, id))
+
+    print ('--- Converto ---')                
+    #qui compone il nome del file
+    if not artist is None:
+        fileout = artist + " - " + track + " [" + id + "].mp3"
+        id3title = track
+    else:
+        fileout = "_" + title + " [" + id + "].mp3"
+        id3title = title
+        
+    print (fileout)
+
+    if not os.path.exists("media/" + user):
+        os.makedirs("media/" + user)
+
+    fileout = os.path.join("media/" + user, fileout)
+    
+    if os.path.isfile(filetemp):
+        #copia il file nella cartella media
+        #implementare conversione in mp3 e metadata
+        #shutil.copy(filetemp, fileout)
+        
+        cmd = "ffmpeg -i %s -y -vn -ar 44100 -ac 2 -b:a 192k -acodec libmp3lame \
+            -metadata title=\"%s\" -metadata artist=\"%s\" -metadata album=\"%s\" \
+            \"%s\"" \
+            %(filetemp, id3title, artist, album, fileout)
+        os.system(cmd)
+
+
+    print ('--- Fine ---')
+    print ("")
+
+    return ("Scaricato %s [%s]" %(title, id))
+
+
+
+
+
+def normalizetext(s):
+    if s is None:
+        return None
+    else:
+        s = re.sub(r'[\\|/|:|*|?|"|<|>|\|]',r'',s)
+        s = " ".join(s.split())
+    return s
+    
+    
+    
+    
+
+if __name__ == '__main__':
+    url = sys.argv[1]
+    download_video(url)