Skip to content

Instantly share code, notes, and snippets.

@rafaelribeiroo
Created September 23, 2022 04:18
Show Gist options
  • Save rafaelribeiroo/880f79bcf6cbb06416cfcc225fc22b4e to your computer and use it in GitHub Desktop.
Save rafaelribeiroo/880f79bcf6cbb06416cfcc225fc22b4e to your computer and use it in GitHub Desktop.
# pip install webdriver-manager selenium psycopg2
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup as beauty
from selenium import webdriver
from psycopg2 import connect
from decouple import config
from datetime import date
from smtplib import SMTP
from re import search
today = date.today()
connection = connect(
host='localhost',
database='rarbg',
user='postgres',
password=config('PASSWORD_DB')
)
snd = config('FROM_MAIL')
rcv = config('TO_MAIL')
sbj = f'Movies Added in RARbg ({today.strftime("%d/%m/%Y")})'
passwd = config('PASSWORD')
# https://stackoverflow.com/questions/64717302/deprecationwarning-executable-path-has-been-deprecated-selenium-python
chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(
options=chrome_options,
service=Service(ChromeDriverManager().install())
)
driver.get('https://rarbgproxied.org/torrents.php')
try:
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'lista')))
except TimeoutException:
print("Loading took too much time!")
html = driver.page_source
soup = beauty(html, 'html.parser')
# import pdb; pdb.set_trace()
cursor = connection.cursor()
attachment = []
for iterate in range(0, 8):
title = soup.find_all("td", {
"class": "lista",
"align": "left",
"valign": "top"
})[iterate].a['title']
url = soup.find_all("td", {
"class": "lista",
"align": "left",
"valign": "top"
})[iterate].a['href']
clear_title = title[0:title.find(
search('20[0-9][0-9]', title)[0]) - 1].replace('.', ' ')
cursor.execute(
'SELECT EXISTS ( SELECT 1 FROM torrent WHERE title = %s )',
(f'{clear_title}',))
if cursor.fetchone()[0] is False:
attachment.append(clear_title)
cursor.execute(f"INSERT INTO torrent (title, url) VALUES ('{clear_title}', '{url}')")
cursor.close()
connection.commit()
connection.close()
driver.close()
if attachment:
with open('/tmp/list_movies.txt', 'w') as writer:
writer.write('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8">
<style>
ul li:before {
background: linear-gradient(45deg, #f69ec4, #f9dd94);
}
</style>
</head>
<body>
<ol>
''')
for item in attachment:
writer.write(f' <li>{item}</li>\n')
writer.write(''' </ol>
</body>
</html>''')
file = open('/tmp/list_movies.txt')
msg = file.read()
msg = f'Subject: {sbj}\nFrom: {snd}\nTo: {rcv}\nContent-Type: text/html\n{msg}'
try:
server = SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(snd, passwd)
server.sendmail(snd, rcv, msg)
server.close()
except Exception as e:
print("Error sending mail", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment