· 5 years ago · Mar 04, 2020, 06:28 PM
1import requests
2from requests.packages.urllib3.exceptions import InsecureRequestWarning
3import json
4import time
5import requests
6import time
7import datetime
8from bs4 import BeautifulSoup, Comment
9import cfscrape
10from dhooks import Webhook, Embed
11from threading import Thread
12import regex
13import json
14import sys,os
15from colorama import Fore, Back, Style,init
16init()
17from selenium import webdriver
18import random
19import sqlite3
20
21requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
22
23print('Inserisci il link che vuoi monitorare: ')
24link = input()
25link1 = link.replace('https://www.docenti.unina.it/#!/professor/','https://www.docenti.unina.it/webdocenti-be/docenti/')
26avviso = 0
27#links = []
28class Product():
29 def __init__(self, titolo, link, categoria):
30 '''
31 (str, str, bool, str) -> None
32 Creates an instance of the Product class.
33 '''
34
35 # Setup product attributes
36 self.titolo = titolo
37 self.categoria = categoria
38 self.link = link
39
40
41def add_to_db(product):
42 '''
43 (Product) -> bool
44 Given a product <product>, the product is added to a database <products.db>
45 and whether or not a Discord alert should be sent out is returned. Discord
46 alerts are sent out based on whether or not a new product matching
47 keywords is found.
48 '''
49
50 # Initialize variables
51 titolo = product.titolo
52 categoria = str(product.categoria)
53 link = product.link
54
55 alert = False
56
57 # Create database
58 conn = sqlite3.connect('docenti.db')
59 c = conn.cursor()
60 c.execute("""CREATE TABLE IF NOT EXISTS docenti(titolo TEXT, link TEXT UNIQUE, categoria TEXT)""")
61
62 # Add product to database if it's unique
63 try:
64 c.execute("""INSERT INTO docenti (titolo, link, categoria) VALUES (?, ?, ?)""", (titolo, link, categoria))
65 #log('s', "Found new product with keyword " + categoria + ". Link = " + link)
66 alert = True
67 except:
68 #print(e)
69 # Product already exists
70 pass
71 #log('i', "Product at URL <" + link + "> already exists in the database.")
72
73 # Close connection to the database
74 conn.commit()
75 c.close()
76 conn.close()
77
78 # Return whether or not it's a new product
79 return alert
80
81
82while True:
83 headers = {
84 'Host': 'www.docenti.unina.it',
85 'Cache-Control': 'max-age=0',
86 'Upgrade-Insecure-Requests': '1',
87 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
88 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
89 'Referer': 'https://www.google.it/',
90 'Accept-Language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6,es;q=0.5',
91 }
92 try:
93
94
95 response = requests.get(link1, headers=headers,verify=False,timeout=3)
96
97
98
99 jsondata = json.loads(response.text)
100 avviso+=1
101 print('Richiesta numero:',avviso,'Ho trovato i seguenti avvisi: ')
102
103 for i in jsondata['content']:
104 id = i['id']
105 categoria = i['categoria']
106 titolo = i['titolo']
107 print('id ', id)
108 print('categoria', i['categoria'])
109 print('titolo', i['titolo'])
110 variabile = link+'/'+str(id)
111 variabile = variabile.replace("avvisi","avviso")
112 print(datetime.datetime.now(),'link avviso', variabile)
113
114 #links.append(variabile)
115
116 product = Product(titolo, variabile, categoria)
117 alert = add_to_db(product)
118
119 if(alert):
120
121 print('Oracoloooo cazzzoooo nuovo avvisooo')
122
123
124
125 time.sleep(4)
126 except Exception as e:
127 print(e)
128 print('connection error')