-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (62 loc) · 2.22 KB
/
main.py
File metadata and controls
75 lines (62 loc) · 2.22 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import requests
import selectorlib
import smtplib, ssl
import os
import time
import sqlite3
URL = "http://programmer100.pythonanywhere.com/tours/"
PASSWORD = os.environ.get("PASSWORD_GMAIL")
SENDER = "alexlerch76@gmail.com"
RECEIVER = "alexlerch76@gmail.com"
class Event:
def scrape(self, url):
"""Scrape the page source from the URL"""
response = requests.get(url)
source = response.text
return source
def extract(self, source):
extractor = selectorlib.Extractor.from_yaml_file("extract.yaml")
value = extractor.extract(source)["tours"]
return value
class Email:
def send(self, message):
host = "smtp.gmail.com"
port = 465
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context) as server:
server.login(SENDER, PASSWORD)
server.sendmail(SENDER, RECEIVER, message)
print("mail sent")
class Database:
def __init__(self, database_path):
# Establish a connection and a cursor
self.connection = sqlite3.connect(database_path)
def store(self, extracted):
row = extracted.split(",")
row = [item.strip() for item in row]
cursor = self.connection.cursor()
cursor.execute("INSERT INTO events VALUES(?,?,?)", row)
self.connection.commit()
def read(self, extracted):
row = extracted.split(",")
row = [item.strip() for item in row]
band, city, date = row
cursor = self.connection.cursor()
cursor.execute("SELECT * FROM events WHERE band=? AND city=? AND date=?", (band, city, date))
rows = cursor.fetchall()
return rows
if __name__ == "__main__":
while True:
event = Event()
scraped = event.scrape(URL)
extracted = event.extract(scraped)
print(extracted)
if extracted != "No upcoming tours":
database = Database(database_path="data.db")
row = database.read(extracted)
row = database.read(extracted)
if not row:
database.store(extracted)
email = Email()
email.send(message="Hey, a new event was found!" + "\n" + extracted)
time.sleep(2)