En raison de la nouvelle infection à coronavirus (COVID-19), l'école a été fermée et il serait bien de mettre à jour les dernières informations sur le site Web de l'école à tout moment, j'ai donc automatiquement obtenu la mise à jour et créé un système de notification pour la classe LINE.
ubuntu19.10
Python 3.7.5
res = requests.get(url1)
res2 = requests.get(url2)
res.encoding = res.apparent_encoding
res2.encoding = res2.apparent_encoding
m = "\n" + link
result = list(set(list1) - set(list2))
print("raw" + str(result))
if len(result) > 30:
result = []
elif len(result) >= 30:
result = "Limite de caractères ...(¡>Д<¡)・ ゜ ・"
result = m + "\n" + result
elif result != []:
for s in result:
m = m + "\n" + s
result = m
url = "https://notify-api.line.me/api/notify"
headers = {'Authorization': 'Bearer ' + access_token}
payload = {'message': message}
r = requests.post(url, headers=headers, params=payload,)
L'ensemble
import re
import requests
import copy
import time
import requests
import datetime
def get_data(url1, url2):
global res, res2
res = requests.get(url1)
res2 = requests.get(url2)
res.encoding = res.apparent_encoding
res2.encoding = res2.apparent_encoding
def format_text(text):
text = re.sub(r'[a-zA-Z0-9]', '', text)
text = re.sub(r'[!-/:-@[-`{-~]', '', text)
text = text.replace(' ', '')
return text
def task1():
print(datetime.datetime.now())
text = format_text(res.text)
text2 = format_text(res2.text)
global l1, l2
l1 = text.split()
l2 = text2.split()
global result, result2
result = difference(l1, l_b1, URL1)
result2 = difference(l2, l_b2, URL2)
print(result, result2)
def task2():
global l_b1, l_b2
l_b1 = copy.deepcopy(l1)
l_b2 = copy.deepcopy(l2)
def difference(list1, list2, link):
m = "\n" + link
result = list(set(list1) - set(list2))
print("raw" + str(result))
if len(result) > 30:
result = []
elif len(result) >= 30:
result = "Limite de caractères ...(¡>Д<¡)・ ゜ ・"
result = m + "\n" + result
elif result != []:
for s in result:
m = m + "\n" + s
result = m
return result
def line(message, access_token):
url = "https://notify-api.line.me/api/notify"
headers = {'Authorization': 'Bearer ' + access_token}
payload = {'message': message}
r = requests.post(url, headers=headers, params=payload,)
def sent(name, message, token1, token2):
if message != []:
line(message, token1)
line(message, token2)
else:
line(name + "==[]", token2)
def main():
global l_b1, l_b2, URL1, URL2
l_b1 = []
l_b2 = []
URL1 = "URL de HP que vous souhaitez obtenir"
URL2 = "URL de HP que vous souhaitez obtenir"
class_line = "Jeton de classe LINE"
admin_line = "Mon jeton"
while True:
get_data(URL1, URL2)
task1()
sent("result", result, class_line, admin_line)
sent("result2", result2, class_line, admin_line)
task2()
for i in range(6):
line("\n" + str(datetime.datetime.now()) + "\n" +
"Ça marche bien.Peut-être..." + "\n" + str(i), admin_line)
time.sleep(300)
if __name__ == '__main__':
main()
Je me suis envoyé le message "Ça marche bien. Peut-être ..." une fois toutes les 5 minutes. Je vais garder le secret, je n'en ai pas vraiment besoin. (Rires)
Pour être honnête, la limite de caractères est appropriée.
J'ai changé main () comme suit et j'ai traité l'erreur.
def main():
global l_b1, l_b2, URL1, URL2
l_b1 = []
l_b2 = []
URL1 = "URL de HP que vous souhaitez obtenir"
URL2 = "URL de HP que vous souhaitez obtenir"
class_line = "Jeton de classe LINE"
admin_line = "Mon jeton"
while True:
try:
get_data(URL1, URL2)
task1()
sent("result", result, class_line, admin_line)
sent("result2", result2, class_line, admin_line)
task2()
for i in range(6):
line("\n" + str(datetime.datetime.now()) + "\n" +
"Ça marche bien.Peut-être..." + "\n" + str(i), admin_line)
time.sleep(300)
except Exception as e:
try:
line("\n" + str(e) + "error occurred", admin_line)
except Exception as e:
print(e, "error occurred")
time.sleep(30)
Changé envoyé () comme suit.
def sent(name, message, token1, token2):
try:
if message != []:
line(message, token1)
line(message, token2)
else:
line(name + "==[]", token2)
except Exception as e:
try:
line("\n" + str(e) + "error occurred",token2)
except Exception as e:
print(e, "error occurred")
Différence modifiée () comme suit
def difference(list1, list2, link):
m = "\n" + link
result = list(set(list1) - set(list2))
print("raw" + str(result))
if result != []:
for s in result:
m = m + "\n" + s
result = m
return result
<détails> <résumé> Programme final </ résumé>
import re
import requests
import copy
import time
import requests
import datetime
def get_data(URL):
res = requests.get(URL)
res.encoding = res.apparent_encoding
return res
def format_text(text):
text = re.sub(r"[a-zA-Z0-9]", "", text)
text = re.sub(r"[!-/:-@[-`{-~]", "", text)
text = text.replace(" ", "")
return text
def task1(res, l_b, URL):
text = format_text(res.text)
list = text.split()
result = difference(list, l_b, URL)
print(result)
return result, list
def difference(list1, list2, link):
m = "\n" + link
result = list(set(list1) - set(list2))
print("raw" + str(result))
if result != []:
for s in result:
m = m + "\n" + s
result = m
return result
def line(message, access_token):
try:
url = "https://notify-api.line.me/api/notify"
headers = {"Authorization": "Bearer " + access_token}
payload = {"message": message}
r = requests.post(url, headers=headers, params=payload,)
except Exception as e:
try:
url = "https://notify-api.line.me/api/notify"
headers = {"Authorization": "Bearer " +Mon jeton}
payload = {"message": e}
r = requests.post(url, headers=headers, params=payload,)
except:
print(e, "\nerror occurred")
def sent(name, message, token1, token2):
if message != []:
line(message, token1)
line(message, token2)
else:
line(name + "==[]", token2)
def main():
l_b1 = []
l_b2 = []
URL1 = "URL de HP que vous souhaitez obtenir"
URL2 = "URL de HP que vous souhaitez obtenir"
class_line = "Jeton de classe LINE"
admin_line = "Mon jeton"
while True:
try:
res = get_data(URL1)
res2 = get_data(URL2)
print(datetime.datetime.now())
result, l1 = task1(res, l_b1, URL1)
result2, l2 = task1(res2, l_b2, URL2)
sent("result", result, class_line, admin_line)
sent("result2", result2, class_line, admin_line)
l_b1 = copy.deepcopy(l1)
l_b2 = copy.deepcopy(l2)
for i in range(6):
line("\n" + str(datetime.datetime.now()) + "\n" +
"Ça marche bien.Peut-être..." + "\n" + str(i), admin_line)
time.sleep(300)
except Exception as e:
line("\n" + str(e) + "\nerror occurred", admin_line)
time.sleep(30)
if __name__ == "__main__":
main()
Je pense que le procédé n'est pas beau car il a été développé instantanément. Veuillez signaler toute erreur. Les symboles alphanumériques et demi-largeur ayant été supprimés, la date, etc. ne sera pas affichée. Si vous avez une solution, veuillez vous rendre dans la section commentaires m (_ _) m
Recommended Posts