|
@@ -13,7 +13,6 @@ import time
|
|
|
import subprocess
|
|
|
import random
|
|
|
import configparser
|
|
|
-from bs4 import BeautifulSoup
|
|
|
|
|
|
from telegram.ext import Updater, MessageHandler, Filters
|
|
|
from mastodon import Mastodon, StreamListener
|
|
@@ -50,13 +49,6 @@ 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)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
@@ -130,7 +122,7 @@ def add(url, user = "-unknown-"):
|
|
|
#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))
|
|
@@ -278,7 +270,7 @@ def getposition(file):
|
|
|
|
|
|
|
|
|
|
|
|
-def __telegram_msg_parser(bot, update):
|
|
|
+def telegram_msg_parser(bot, update):
|
|
|
print("Messaggio ricevuto")
|
|
|
msg = update.message.text
|
|
|
id = str(update.message.from_user.id)
|
|
@@ -309,14 +301,17 @@ def telegram_bot():
|
|
|
# Get the dispatcher to register handlers
|
|
|
dp = updater.dispatcher
|
|
|
# parse message
|
|
|
- dp.add_handler(MessageHandler(Filters.text, __telegram_msg_parser))
|
|
|
+ dp.add_handler(MessageHandler(Filters.text, telegram_msg_parser))
|
|
|
# Start the Bot
|
|
|
updater.start_polling()
|
|
|
# Run the bot until you press Ctrl-C
|
|
|
updater.idle()
|
|
|
|
|
|
|
|
|
-class __MastodonListener(StreamListener):
|
|
|
+class MastodonListener(StreamListener):
|
|
|
+ # andiamo a definire il metodo __init__, che prende una istanza di Mastodon come parametro opzionale e lo setta nella prop. self.mastodon
|
|
|
+ def __init__(self, mastodonInstance=None):
|
|
|
+ self.mastodon = mastodonInstance
|
|
|
def on_notification(self, notification):
|
|
|
print("Messaggio ricevuto")
|
|
|
#try:
|
|
@@ -326,28 +321,32 @@ class __MastodonListener(StreamListener):
|
|
|
#except KeyError:
|
|
|
# return
|
|
|
|
|
|
- msg = BeautifulSoup(msg, features="lxml").get_text()
|
|
|
+ msg = msg.replace("<br>", "\n")
|
|
|
+ msg = re.compile(r'<.*?>').sub('', msg)
|
|
|
+
|
|
|
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)
|
|
|
-
|
|
|
+ #print (urls)
|
|
|
+ #print (user)
|
|
|
+
|
|
|
+ #visibility = notification['status']['visibility']
|
|
|
+ statusid = notification['status']['id']
|
|
|
if not urls:
|
|
|
- mastodon.status_post("Non ho trovato indirizzi validi...",visibility="direct")
|
|
|
+ self.mastodon.status_post("@" + username + " " + "Non ho trovato indirizzi validi...", in_reply_to_id = statusid, visibility="direct")
|
|
|
return()
|
|
|
|
|
|
- mastodon.status_post("Messaggio ricevuto. Elaboro...",visibility="direct")
|
|
|
-
|
|
|
+ self.mastodon.status_post("@" + username + " " + "Messaggio ricevuto. Elaboro...", in_reply_to_id = statusid, visibility="direct")
|
|
|
+
|
|
|
for url in urls:
|
|
|
# start the download
|
|
|
dl = add(url, user)
|
|
|
- mastodon.status_post(dl,visibility="direct")
|
|
|
+ self.mastodon.status_post("@" + username + " " + dl, in_reply_to_id = statusid, visibility="direct")
|
|
|
|
|
|
|
|
|
def mastodon_bot():
|
|
|
print ("Bot avviato")
|
|
|
- #mastodon = Mastodon(access_token = MASTODON_TOKEN, api_base_url = MASTODON_URL)
|
|
|
- listener = __MastodonListener()
|
|
|
+ mastodon = Mastodon(access_token = MASTODON_TOKEN, api_base_url = MASTODON_URL)
|
|
|
+ listener = MastodonListener(mastodon)
|
|
|
mastodon.stream_user(listener)
|
|
|
|
|
|
|