-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (39 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sqlite3
import time
import requests
import selectorlib
from send_email import send_email
URL = "https://programmer100.pythonanywhere.com/tours/"
connection = sqlite3.connect("data.db")
def scraper(url):
response = requests.get(url)
response = response.text
return response
def extract(source):
extractor = selectorlib.Extractor.from_yaml_file("extract.yaml")
value = extractor.extract(source)["tours"]
return value
def store(extracted_data):
values = extracted_data.split(',')
values = [item.strip() for item in values]
cursor = connection.cursor()
cursor.execute("INSERT INTO events VALUES(?,?,?)", values)
connection.commit()
def read_extracted_data(extracted_data):
values = extracted_data.split(',')
values = [item.strip() for item in values]
band, city, date = values
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM events WHERE band=? AND city=? AND date=?", (band, city, date))
rows = cursor.fetchall()
return rows
if __name__ == "__main__":
while True:
source = scraper(URL)
extracted_vals = extract(source)
if extracted_vals != "No upcoming tours":
row = read_extracted_data(extracted_vals)
if not row:
store(extracted_vals)
send_email(extracted_vals)
time.sleep(2)