-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (57 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
from bs4 import BeautifulSoup
import redis
from secrets import password
import datetime
class Scraper:
def __init__(self, keywords):
self.markup = requests.get('https://news.ycombinator.com/').text
self.keywords = keywords
def parse(self):
soup = BeautifulSoup(self.markup, 'html.parser')
links = soup.findAll("a", {"class": "storylink"})
self.saved_links = []
for link in links:
for keyword in self.keywords:
if keyword in link.text:
self.saved_links.append(link)
def store(self):
r = redis.Redis(host='localhost', port=6379, db=0)
for link in self.saved_links:
r.set(link.text, str(link))
def email(self):
r = redis.Redis(host='localhost', port=6379, db=0)
links = [str(r.get(k)) for k in r.keys()]
# email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromEmail = 'laloukasnews@gmail.com'
toEmail = 'nkslks97@gmail.com'
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg["From"] = fromEmail
msg["To"] = toEmail
html = """
<h4> %s links you might find interesting today:</h4>
%s
""" % (len(links), '<br/><br/>'.join(links))
mime = MIMEText(html, 'html')
msg.attach(mime)
try:
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login(fromEmail, password)
mail.sendmail(fromEmail, toEmail, msg.as_string())
mail.quit()
print('Email sent!')
except Exception as e:
print('Something went wrong... %s' % e)
# flush redis
r.flushdb()
s = Scraper(['database'])
s.parse()
s.store()
if datetime.datetime.now().hour == 13:
s.email()