Skip to content

Commit c3eadb7

Browse files
authored
Add files via upload
1 parent b0afe41 commit c3eadb7

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

main.pyw

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from bs4 import BeautifulSoup
2+
from email_notification import email_notification
3+
import datetime
4+
import os
5+
import requests
6+
from settings import settings
7+
8+
9+
# checking which books I allready have
10+
try:
11+
with open("booklist.txt") as f:
12+
books = [book.strip("\n") for book in f.readlines()]
13+
except FileNotFoundError:
14+
with open("booklist.txt", "w") as f:
15+
f.write()
16+
books = []
17+
18+
try:
19+
url = "https://www.packtpub.com/packt/offers/free-learning"
20+
21+
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
22+
r = requests.get(url, headers=headers)
23+
soup = BeautifulSoup(r.text, "html5lib")
24+
25+
# title
26+
title = soup.find("div", attrs={"class":"dotd-title"}).text.strip("\t\n")
27+
28+
# Summary
29+
summary_div = soup.find("div", attrs={"class":"dotd-main-book-summary"})
30+
divs = summary_div.find_all("div")
31+
summary = []
32+
for i, div in enumerate(divs):
33+
if i < 2:
34+
pass
35+
elif i == 2:
36+
summary.append(divs[2].text.strip("\n\t"))
37+
else:
38+
if not 'class="dotd-main-book-form cf"' in str(div):
39+
summary.append(divs[i].text.strip("\n\t"))
40+
summary = "\n".join(summary)
41+
42+
# image
43+
img = soup.find("img", attrs={"class" : "bookimage"})["src"].strip("/")
44+
45+
# print([title])
46+
# print([summary])
47+
# print([img])
48+
# print([url])
49+
50+
except KeyError:
51+
# ToDo -- Specify possible errors
52+
email_notification(settings, "packtbub.com | An error occurs...", f"<b>Please visit {url} manually and fix me...</b>")
53+
exit()
54+
55+
56+
if title in books:
57+
58+
subject = f"packtpub.com | {title} | You allready have this book!"
59+
60+
text = """<h2>{title}</h2>
61+
<p>Please visit {url}!</p>
62+
<img src={img}/>
63+
<p>{summary}</p>""".format(title=title, url=url, img=img, summary=summary.replace("\n", "</p><p>"))
64+
65+
email_notification(settings, subject, text)
66+
67+
else:
68+
69+
books.append(title)
70+
71+
subject = f"packtpub.com | {title} | ✭ This book is new! ✭"
72+
73+
text = """<div style="margin-bottom: 10px; padding: 10px; background: Yellow; color: Red;">
74+
<strong>I added this book to your booklist. Please download this book immediately.</strong>
75+
</div>
76+
77+
<h2>{title}</h2>
78+
<p>Please visit {url}!</p>
79+
80+
img src="{img}" title="" />
81+
82+
<p>{summary}</p>""".format(title=title, url=url, img=img, summary=summary.replace("\n", "</p><p>"))
83+
84+
email_notification(settings, subject, text)
85+
86+
87+
# Updating booklist
88+
books.sort(key=str.lower)
89+
with open("booklist.txt", "w") as f:
90+
for book in books:
91+
f.write(book + "\n")

0 commit comments

Comments
 (0)