moved to archive

This commit is contained in:
jnoack@itsblue.de 2019-02-06 17:03:47 +01:00
parent 8f32167e3f
commit 83788dde4e

View file

@ -1,383 +0,0 @@
#!/usr/local/bin/python3
my_song1 = "Zusammen"
my_interpret1 = "Fantastischen Vier"
my_song2 = "Beat it"
my_interpret2 = "Michael Jackson"
whatsapp_clients = ["+491752076485","+4917670870053"]
#########################################################
verbose_start = "06:00"
verbose_stop = "23:00"
song1 = "Zusammen"
interpret1 = "Fantastischen Vier"
song2 = "Beat it"
interpret2 = "Michael Jackson"
currentmsg = ""
run_thread = 1
lastchecktime = ""
fullcurrenttime = ""
file_name = "./bully_search.txt"
import sys
import os.path
import threading
from twilio.rest import Client
import requests
import re
from bs4 import BeautifulSoup
import time
from datetime import datetime
from datetime import timedelta
import random
from flask import Flask, request
from twilio.twiml.messaging_response import Body, Message, Redirect, MessagingResponse
app = Flask(__name__)
@app.route("/wa", methods=['POST', 'GET'])
def wa_ahoy_reply():
global song1
global interpret1
global song2
global interpret2
global currentmsg
global lastchecktime
# Start our response
body = request.values.get('Body', None)
resp = MessagingResponse()
if((re.search("^list$", body, re.IGNORECASE)!=None) | (re.search("^l$", body, re.IGNORECASE)!=None)):
soup_page(15)
resp.message("Latest list is:\n" + currentmsg)
lastchecktime = ""
else:
if(body == "?"):
resp.message("#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2: '" + interpret2 + "'\n - '" + song2 + "'\n\n")
else:
if(re.search("^s1=(.+)", body, re.IGNORECASE)):
song1 = re.search("^s1=(.+)",body, re.IGNORECASE).group(1)
resp.message("Song1 changed to: '" + song1 + "'\n" +
"Interpret1-Song1:\n'" + interpret1 + "' - '" + song1 + "'\n\n" + "Interpret2-Song2:\n'" + interpret2 + "' - '" + song2 + "'\n\n")
else:
if(re.search("^i1=(.+)", body, re.IGNORECASE)):
interpret1 = re.search("^i1=(.+)",body, re.IGNORECASE).group(1)
resp.message("Interpret1 changed to: '" + interpret1 + "'\n" +
"#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2: '" + interpret2 + "' - '" + song2 + "'\n\n")
else:
if (re.search("^s2=(.+)", body, re.IGNORECASE)):
song2 = re.search("^s2=(.+)", body, re.IGNORECASE).group(1)
resp.message("Song2 changed to: '" + song2 + "'\n" +
"#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2 :'" + interpret2 + "'\n - '" + song2 + "'\n\n")
else:
if (re.search("^i2=(.+)", body, re.IGNORECASE)):
interpret2 = re.search("^i2=(.+)", body, re.IGNORECASE).group(1)
resp.message("Interpret2 changed to: '" + interpret2 + "'\n" +
"#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2: '" + interpret2 + "'\n - '" + song2 + "'\n\n")
else:
if (re.search("^w$", body, re.IGNORECASE)):
write_to_file()
resp.message("Written songs and interprets to file.\n" +
"You can read it with 'r'.\n" +
"#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2: '" + interpret2 + "'\n - '" + song2 + "'\n\n")
else:
if (re.search("^r$", body, re.IGNORECASE)):
read_from_file()
resp.message("Read songs and interprets from file.\n" +
"You can read it with 'w'.\n" +
"#1: '" + interpret1 + "'\n - '" + song1 + "'\n\n" + "#2: '" + interpret2 + "'\n - '" + song2 + "'\n\n")
else:
if (re.search("^is1=(.+),(.+)", body, re.IGNORECASE)):
interpret1 = re.search("^is1=(.+),(.+)", body, re.IGNORECASE).group(1)
song1 = re.search("^is1=(.+),(.+)", body, re.IGNORECASE).group(2)
resp.message("#1 changed to: '" + interpret1 + "'-'" + song1 + "'\n" +
"Interpret1-Song1:\n'" + interpret1 + "' - '" + song1 + "'\n\n" + "Interpret2-Song2:\n'" + interpret2 + "' - '" + song2 + "'\n\n")
else:
if (re.search("^is2=(.+),(.+)", body, re.IGNORECASE)):
interpret2 = re.search("^is2=(.+),(.+)", body, re.IGNORECASE).group(1)
song2 = re.search("^is2=(.+),(.+)", body, re.IGNORECASE).group(2)
resp.message("#2 changed to: '" + interpret2 + "'-'" + song2 + "'\n" +
"Interpret1-Song1:\n'" + interpret1 + "' - '" + song1 + "'\n\n" + "Interpret2-Song2:\n'" + interpret2 + "' - '" + song2 + "'\n\n")
else:
resp.message("Send: \n\n'list' or 'l' for latest playlist.\n '?' for songs/title looking for.\n 's1=<songname>' to change song1.\n" +
" 'i1=<artist>' to change interpret1.\n 's2=<songname>' to change song2.\n 'i2=<artist>' to change interpret2.\n" +
" 'is1=<artist>,<song> to change #1 \n 'is2=<artist>,<song> to change #2 \n " +
" 'w' write i&s to file.\n 'r' read i&s from file\n")
return str(resp)
# ngrok starten ausserhalb des virtuellen env: ngrok http 5000
# in twilio WebHooks die Https Adresse die ngrok beim startup zeigt eintragen inkl. /wa am Ende
# python virtualles env aufbauen und darin Flask installieren
# für windows: http://timmyreilly.azurewebsites.net/python-flask-windows-development-environment-setup/
# python searchwebsite_wa.py starten ...
def write_to_file():
global file_name
global song1, interpret1, song2, interpret2
file = open(file_name, "w")
file.write(interpret1 + "\n")
file.write(song1 + "\n")
file.write(interpret2 + "\n")
file.write(song2 + "\n")
file.close()
def read_from_file():
global song1, interpret1, song2, interpret2, my_song1, my_interpret1, my_song2, my_interpret2
global file_name
if (os.path.isfile(file_name)):
file = open(file_name,"r")
interpret1 = file.readline().replace('\n', '')
song1 = file.readline().replace('\n', '')
interpret2 = file.readline().replace('\n', '')
song2 = file.readline().replace('\n', '')
file.close()
else:
song1 = my_song1
interpret1 =my_interpret1
song2 = my_song2
interpret2 = my_interpret2
def send_wa_msg(client, resultschanged, sendmsg, callermsg):
global whatsapp_clients
if (resultschanged == True):
if (sendmsg == True):
for waclient in whatsapp_clients:
client.messages.create(to="whatsapp:"+ waclient,
from_="whatsapp:+14155238886",
body=callermsg)
def check_for_alarm_case(found_interpret_song_1, found_interpret_song_1_time, interpret1, song1, found_interpret_song_2, found_interpret_song_2_time, interpret2, song2, sendall, wait):
global fullcurrenttime
global lastchecktime
callermsg = ""
waittime = wait
sendmsg = False
if (((found_interpret_song_1 > 0) | (found_interpret_song_1 > 0))):
if ((found_interpret_song_1 == 2) & (found_interpret_song_2 == 1)):
callermsg = fullcurrenttime + " ++ BULLI DOPPEL ALARM! ++\n"
callermsg = callermsg + "\n"
callermsg = callermsg + "+ JETZT ANRUFEN!!! 01375/100 100 +\n"
callermsg = callermsg + "\n"
callermsg = callermsg + "Beide Songs (sind ge)laufen!"
callermsg = callermsg + "\n"
# send taht everytime ...
sendmsg = True
else:
if (found_interpret_song_1 == 1):
waittime = 15 + random.randint(-5, 5)
callermsg = fullcurrenttime + " ++ BULLI ERSTER ALARM! ++\n"
callermsg = callermsg + found_interpret_song_1_time + ": der ERSTE Song läuft!\n"
callermsg = callermsg + "Es läuft:\n"
callermsg = callermsg + interpret1 + " '" + song1 + "'\n"
callermsg = callermsg + "Es fehlt noch:\n"
callermsg = callermsg + interpret2 + " '" + song2 + "'\n\n"
callermsg = callermsg + "Wenn der kommt 01375/100 100 anrufen!\n"
callermsg = callermsg + "https://www.antenne.de/musik/song-suche/ \n"
callermsg = callermsg + "https://www.webradio.de/antenne-bayern/\n"
callermsg = callermsg + "\n"
if (sendall == True):
# dont send that at night ...
sendmsg = True
else:
if ((found_interpret_song_1 == 2) & (found_interpret_song_2 != 1)):
callermsg = fullcurrenttime + " -- FEHLALARM! 2.Song falsch --\n"
if (sendall == True):
# dont send that at night ...
sendmsg = True
else:
if (found_interpret_song_2 == 1):
callermsg = fullcurrenttime + " -- ENTSPANNEN! 2.Song ohne 1. --\n"
callermsg = callermsg + found_interpret_song_2_time + ": läuft der 2.Song!\n"
callermsg = callermsg + "Es läuft:\n"
callermsg = callermsg + interpret2 + " '" + song2 + "'\n"
callermsg = callermsg + "Es sollte aber zuerst laufen:\n"
callermsg = callermsg + interpret1 + " '" + song1 + "'\n"
callermsg = callermsg + "\n"
callermsg = callermsg + "Kein Grund anzurufen.\n"
if (sendall == True):
# dont send that at night ...
sendmsg = True
return(callermsg, waittime, sendmsg)
def soup_page(max_chars):
global interpret1
global interpret2
global song1
global song2
global currentmsg
global lastchecktime
global fullcurrenttime
i1 = re.compile(interpret1, re.IGNORECASE)
t1 = re.compile(song1, re.IGNORECASE)
i2 = re.compile(interpret2, re.IGNORECASE)
t2 = re.compile(song2, re.IGNORECASE)
currtime_hour = time.strftime("%H")
currtime_min = time.strftime("%M")
currtime_sec = time.strftime("%S")
fullcurrenttime = currtime_hour + ":" + currtime_min + ":" + currtime_sec
page = requests.get('https://www.antenne.de/musik/song-suche/')
found_interpret_song_1 = 0
found_interpret_song_1_time = 0
found_interpret_song_2 = 0
found_interpret_song_2_time = 0
itemnr = 0
soup = BeautifulSoup(page.content, 'html.parser')
music_results = soup.find(class_="music_results")
music_results_items = music_results.find_all(class_="music_results__content")
whatsappmsg = "Checking time: " + fullcurrenttime+ " Uhr.\n\n"
for found_item in music_results_items:
itemnr = itemnr + 1
single_musik_result = found_item.find(class_="medium-8 columns")
song = single_musik_result.find(class_="song_title").get_text()
interpret = single_musik_result.find(class_="artist").get_text()
ps = single_musik_result.find_all('p')
lief = ps[1].get_text()
# finde den Zeitstempel
zeit = re.search("um (.+?) Uhr", lief).group(1)
if (itemnr == 1):
if (lastchecktime == zeit):
resultschanged = False
else:
resultschanged = True
lastchecktime = zeit
if (i1.search(interpret)):
if (t1.search(song)):
whatsappmsg = whatsappmsg + "1>>"
found_interpret_song_1 = itemnr
found_interpret_song_1_time = zeit
else:
whatsappmsg = whatsappmsg + "1 >"
if (i2.search(interpret)):
if (t2.search(song)):
whatsappmsg = whatsappmsg + "2>>"
found_interpret_song_2 = itemnr
found_interpret_song_2_time = zeit
else:
whatsappmsg = whatsappmsg + "2 >"
if (interpret[:max_chars] != interpret):
interpret = interpret[:max_chars-3] + "..."
if (song[:max_chars] != song):
song = song[:max_chars-3] + "..."
whatsappmsg = whatsappmsg + zeit + " " + interpret + " '" + song + "'\n"
currentmsg = whatsappmsg
return( whatsappmsg, resultschanged, found_interpret_song_1,found_interpret_song_1_time, found_interpret_song_2,found_interpret_song_2_time )
def parse_www():
global run_thread
global currentmsg
global file_name
read_from_file()
# wait between two requests
waittime_s = 70
nexttime_hour = time.strftime("%H")
nexttime_min = time.strftime("%M")
global lastchecktime
accountsid = "AC06de53c014c4bd394e7c7173aed73533"
authtoken = "94816b05b0ac71d3f6227726969e8e21"
client = Client(accountsid, authtoken)
startime = time.strftime("%H:%M:%S")
whatsappmsg = "Scan of Bully-page started " + startime + " Uhr.\n\n"
whatsappmsg = whatsappmsg + "-> Song1:\n '" + interpret1 + "'-'" + song1 + "'\n-> Song2:\n '" + interpret2 + "'-'" + song2 + "'\n"
print(whatsappmsg)
send_wa_msg(client, True, True, whatsappmsg)
while run_thread == 1:
(whatsappmsg, resultschanged, found_interpret_song_1, found_interpret_song_1_time, found_interpret_song_2,
found_interpret_song_2_time) = soup_page(40)
waittime = waittime_s + random.randint(-20, 20)
if ((lastchecktime > verbose_start) & (lastchecktime < verbose_stop)):
sendall = True
else:
sendall = False
(callermsg, waittime, sendmsg ) = check_for_alarm_case(found_interpret_song_1, found_interpret_song_1_time, interpret1, song1,
found_interpret_song_2, found_interpret_song_2_time, interpret2, song2, sendall, waittime)
# convert timestring into time()
time_object = datetime.now() + timedelta(seconds=waittime)
nexttime_hour = time_object.strftime("%H")
nexttime_min = time_object.strftime("%M")
nexttime_sec = time_object.strftime("%S")
currentmsg = whatsappmsg + "\nNext checking time: " + nexttime_hour + ":" + nexttime_min + " Uhr.\n"
if(resultschanged == True):
whatsappmsg = callermsg + whatsappmsg + "\n"
else:
whatsappmsg = "\nNo changes. Titel at " + lastchecktime + " Uhr is the last..\n"
if(sendall == True):
whatsappmsg = whatsappmsg + "Wait for " + str(waittime) + " seconds before next check. \n"
whatsappmsg = whatsappmsg + "My next checking time: " + nexttime_hour + ":" + nexttime_min + ":" + nexttime_sec + " Uhr. "
print(whatsappmsg)
send_wa_msg(client, resultschanged, sendmsg, callermsg)
time.sleep(waittime)
stoptime = time.strftime("%H:%M:%S")
whatsappmsg = "Scan of Bully-page stopped " + stoptime + " Uhr. Bye.\n"
print(whatsappmsg)
send_wa_msg(client, True, True, whatsappmsg)
class myThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print ("----> Starting " + self.name)
parse_www()
print ("----> Exiting " + self.name)
if __name__ == "__main__":
scanwwthread = myThread(1,parse_www)
scanwwthread.start()
app.run()
#while 0==0:
print("Detected CTRL-C ... stopping the process. Please wait ...\n")
run_thread = 0
scanwwthread.join()
sys.exit()