Browse Source

Fixed telegram bot username

itec 4 years ago
parent
commit
7ccd224cab
2 changed files with 20 additions and 19 deletions
  1. 6 2
      playlistalo-telegram.py
  2. 14 17
      playlistalo.py

+ 6 - 2
playlistalo-telegram.py

@@ -10,14 +10,18 @@ import playlistalo
 def msg_parser(bot, update):
     print("Messaggio ricevuto")
     urlz = update.message.text
-
+    u = update.message.from_user
+    user = "t_" + "-".join([i for i in [str(u.id), u.username] if i])
+    #print (urlz)
+    #print (user)
+    
     #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 = playlistalo.add(url, "t_" + update.message.from_user.username)
+        dl = playlistalo.add(url, user)
         update.message.reply_text(dl)
 
 

+ 14 - 17
playlistalo.py

@@ -17,18 +17,19 @@ scriptpath = os.path.dirname(os.path.realpath(__file__))
 
 
 def add(url, user = "-unknown-", sortrandom = False):
-    print ('--- Inizio ---')
+    #print ('--- Inizio ---')
     init()
 
     ydl_opts = {
         'format': 'bestaudio[ext=m4a]',       
         'outtmpl': 'cache/%(id)s.m4a',        
         'noplaylist': True,
+        'quiet': True,
     }
 
     url = url.strip()
-    print (url)
-    print (user)
+    print ("url:   " + url)
+    print ("user:  " + user)
     
     if not validators.url(url):
        print ('--- URL malformato ---')
@@ -46,8 +47,8 @@ def add(url, user = "-unknown-", sortrandom = False):
     id = meta.get('id').strip()
     title = __normalizetext(meta.get('title'))
             
-    print ('id          : %s' %(id))
-    print ('title       : %s' %(title))
+    print ('id:    %s' %(id))
+    print ('title: %s' %(title))
 
     #scrivo  il json
     with open(os.path.join("cache", id + ".json"), 'w') as outfile:
@@ -60,29 +61,25 @@ def add(url, user = "-unknown-", sortrandom = False):
         print ('--- Scarico ---')
         ydl.download([url]) #non ho capito perche' ma senza [] fa un carattere per volta
     
+    if not os.path.isfile(filetemp):
+        return("Err: file non scaricato")
+
     #se il file esiste gia' in playlist salto (potrebbe esserci, anche rinominato)
     if glob(scriptpath + "/playlist/**/*|" + id + ".*"):
          print ('--- File già presente ---')
          return ("Err: %s [%s] già presente" %(title, id))
 
-    print ('--- Scarico ---')      
     if not os.path.exists("playlist/" + user):
-        os.makedirs("playlist/" + user)    #qui compone il nome del file
+        os.makedirs("playlist/" + user)
+    #qui compone il nome del file
     if sortrandom:
         fileout = str(random.randrange(10**6)).zfill(14) + "|" + title + "|" + id + ".m4a"
     else:
         fileout = time.strftime("%Y%m%d%H%M%S") + "|" + title + "|" + id + ".m4a"
-
-        
-    print (fileout)
     fileout = os.path.join("playlist/" + user, fileout)
     
-    if not os.path.isfile(filetemp):
-        return("Err: file non scaricato")
-    #copia il file nella cartella playlist
-    #shutil.copy(filetemp, fileout)
-
-    print ('--- Converto ---')      
+    print ('--- Converto ---')
+    print (fileout) 
     subprocess.call([scriptpath + "/trimaudio.sh", filetemp, fileout])
     if not os.path.isfile(fileout):
         return("Err: file non convertito")
@@ -90,7 +87,7 @@ def add(url, user = "-unknown-", sortrandom = False):
     #cerca la posizione del pezzo appena inserito
     pos = getposition(fileout)
         
-    print ('--- Fine ---')
+    #print ('--- Fine ---')
     print ("")
 
     return ("OK: %s [%s] aggiunto alla playlist in posizione %s" %(title, id, pos))