|
@@ -12,8 +12,11 @@ import json
|
|
|
import time
|
|
|
import subprocess
|
|
|
import random
|
|
|
-from telegram.ext import Updater, MessageHandler, Filters
|
|
|
import configparser
|
|
|
+from bs4 import BeautifulSoup
|
|
|
+
|
|
|
+from telegram.ext import Updater, MessageHandler, Filters
|
|
|
+from mastodon import Mastodon, StreamListener
|
|
|
|
|
|
scriptpath = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
@@ -31,7 +34,7 @@ if not os.path.exists("archive"):
|
|
|
configfile = 'playlistalo.conf'
|
|
|
if not os.path.exists(configfile):
|
|
|
config = configparser.ConfigParser()
|
|
|
- config['playlistalo'] = {'ShuffleUsers': False, 'ShuffleFallback': False, 'Telegram_token': "", 'Mastodon_token': ""}
|
|
|
+ config['playlistalo'] = {'ShuffleUsers': False, 'ShuffleSongs': False, 'ShuffleFallback': False, 'Telegram_token': "", 'Mastodon_token': "", 'Mastodon_url': ""}
|
|
|
with open(configfile, 'w') as f:
|
|
|
config.write(f)
|
|
|
|
|
@@ -40,11 +43,15 @@ config = configparser.ConfigParser()
|
|
|
config.read(configfile)
|
|
|
playlistaloconf = config['playlistalo']
|
|
|
SHUFFLEUSERS = playlistaloconf.getboolean('ShuffleUsers')
|
|
|
-SHUFFLEFALLBACK = playlistaloconf.getboolean('ShuffleUsers')
|
|
|
+SHUFFLESONGS = playlistaloconf.getboolean('ShuffleSongs')
|
|
|
+SHUFFLEFALLBACK = playlistaloconf.getboolean('ShuffleFallback')
|
|
|
TELEGRAM_TOKEN = playlistaloconf.get('Telegram_token')
|
|
|
MASTODON_TOKEN = playlistaloconf.get('Mastodon_token')
|
|
|
+MASTODON_URL = playlistaloconf.get('Mastodon_url')
|
|
|
|
|
|
|
|
|
+#e' qui, brutto, dovrebbe andare dentro a mastodon_bot()
|
|
|
+mastodon = Mastodon(access_token = MASTODON_TOKEN, api_base_url = MASTODON_URL)
|
|
|
|
|
|
|
|
|
|
|
@@ -53,8 +60,7 @@ MASTODON_TOKEN = playlistaloconf.get('Mastodon_token')
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-def add(url, user = "-unknown-", sortrandom = False):
|
|
|
+def add(url, user = "-unknown-"):
|
|
|
#print ('--- Inizio ---')
|
|
|
|
|
|
ydl_opts = {
|
|
@@ -109,7 +115,7 @@ def add(url, user = "-unknown-", sortrandom = False):
|
|
|
if not os.path.exists("playlist/" + user):
|
|
|
os.makedirs("playlist/" + user)
|
|
|
#qui compone il nome del file
|
|
|
- if sortrandom:
|
|
|
+ if SHUFFLESONGS:
|
|
|
fileout = str(random.randrange(10**6)).zfill(14) + "|" + title + "|" + id + ".m4a"
|
|
|
else:
|
|
|
fileout = time.strftime("%Y%m%d%H%M%S") + "|" + title + "|" + id + ".m4a"
|
|
@@ -127,7 +133,7 @@ def add(url, user = "-unknown-", sortrandom = False):
|
|
|
#print ('--- Fine ---')
|
|
|
print ("")
|
|
|
|
|
|
- return ("OK: %s [%s] aggiunto alla playlist in posizione %s" %(title, id, pos))
|
|
|
+ return ("OK: %s [%s] aggiunto alla playlist in posizione #%s" %(title, id, pos))
|
|
|
|
|
|
|
|
|
|
|
@@ -274,21 +280,28 @@ def getposition(file):
|
|
|
|
|
|
def __telegram_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)
|
|
|
+ msg = update.message.text
|
|
|
+ id = str(update.message.from_user.id)
|
|
|
+ username = update.message.from_user.username
|
|
|
+
|
|
|
+ urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)
|
|
|
+ user = "t_" + "-".join([i for i in [id, username] if i])
|
|
|
+ #print (urls)
|
|
|
#print (user)
|
|
|
|
|
|
- #update.message.reply_text("Ciao " + update.message.from_user.username)
|
|
|
+ if not urls:
|
|
|
+ update.message.reply_text("Non ho trovato indirizzi validi...")
|
|
|
+ return()
|
|
|
+
|
|
|
update.message.reply_text("Messaggio ricevuto. Elaboro...")
|
|
|
|
|
|
- for url in [s for s in urlz.splitlines() if s.strip() != ""]:
|
|
|
+ for url in urls:
|
|
|
#update.message.reply_text("Scarico %s" %(url))
|
|
|
# start the download
|
|
|
dl = add(url, user)
|
|
|
update.message.reply_text(dl)
|
|
|
|
|
|
+
|
|
|
def telegram_bot():
|
|
|
print ("Bot avviato")
|
|
|
# Create the EventHandler and pass it your bot's token.
|
|
@@ -303,6 +316,41 @@ def telegram_bot():
|
|
|
updater.idle()
|
|
|
|
|
|
|
|
|
+class __MastodonListener(StreamListener):
|
|
|
+ def on_notification(self, notification):
|
|
|
+ print("Messaggio ricevuto")
|
|
|
+ #try:
|
|
|
+ msg = notification["status"]["content"]
|
|
|
+ id = str(notification["account"]["id"])
|
|
|
+ username = notification["account"]["acct"]
|
|
|
+ #except KeyError:
|
|
|
+ # return
|
|
|
+
|
|
|
+ msg = BeautifulSoup(msg, features="lxml").get_text()
|
|
|
+ urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)
|
|
|
+ user = "m_" + "-".join([i for i in [id, username] if i])
|
|
|
+ print (urls)
|
|
|
+ print (user)
|
|
|
+
|
|
|
+ if not urls:
|
|
|
+ mastodon.status_post("Non ho trovato indirizzi validi...",visibility="direct")
|
|
|
+ return()
|
|
|
+
|
|
|
+ mastodon.status_post("Messaggio ricevuto. Elaboro...",visibility="direct")
|
|
|
+
|
|
|
+ for url in urls:
|
|
|
+ # start the download
|
|
|
+ dl = add(url, user)
|
|
|
+ mastodon.status_post(dl,visibility="direct")
|
|
|
+
|
|
|
+
|
|
|
+def mastodon_bot():
|
|
|
+ print ("Bot avviato")
|
|
|
+ #mastodon = Mastodon(access_token = MASTODON_TOKEN, api_base_url = MASTODON_URL)
|
|
|
+ listener = __MastodonListener()
|
|
|
+ mastodon.stream_user(listener)
|
|
|
+
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
print ("This is a package, use other commands to run it")
|