-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
160 lines (123 loc) · 4.3 KB
/
scraper.py
File metadata and controls
160 lines (123 loc) · 4.3 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import pandas as pd
import smtplib
import os
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from selenium.webdriver.common.action_chains import ActionChains
YOUTUBE_TRENDING_URL = "https://www.youtube.com/feed/trending"
def get_driver():
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
return webdriver.Chrome(options=chrome_options)
def get_videos(driver):
print('Get Video divs')
VIDEO_TAG = 'ytd-video-renderer'
videos = driver.find_elements(By.TAG_NAME,VIDEO_TAG)
return videos
def parse_video(video):
actions = ActionChains(driver)
target = video.find_element(By.ID,'video-title')
actions.move_to_element(target)
actions.perform()
title_tag = video.find_element(By.ID,'video-title')
title = title_tag.text
url = title_tag.get_attribute('href')
thumbnail_url = video.find_element(By.TAG_NAME,'img').get_attribute('src')
channel_name = video.find_element(By.CLASS_NAME,'ytd-channel-name').text
views = video.find_elements(By.CLASS_NAME,'ytd-video-meta-block')[0].find_elements(By.TAG_NAME,'span')[1].text
upload_time = video.find_elements(By.CLASS_NAME,'ytd-video-meta-block')[0].find_elements(By.TAG_NAME,'span')[-1].text
description = video.find_element(By.ID,'description-text').text
return {
'title':title,
'url':url,
'thumbnail url':thumbnail_url,
'channel name':channel_name,
'views':views,
'upload time':upload_time,
'description':description,
}
def send_email(body):
try:
server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server_ssl.ehlo() # optional
# ...send emails
SENDER_EMAIL='batracodes@gmail.com'
RECEIVER_EMAIL='batracodes@gmail.com'
SENDER_PASSWORD=os.environ['GMAIL_PASSWORD']
subject = 'Text Message from Replit'
email_text = f"""\
From: {SENDER_EMAIL}
To: {RECEIVER_EMAIL}
Subject: {subject}
{body}
"""
server_ssl.login(SENDER_EMAIL, SENDER_PASSWORD)
server_ssl.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, email_text)
server_ssl.close()
print('Email sent!')
except:
print('Something went wrong...' )
def send_email_attached(body):
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
SENDER_EMAIL='batracodes@gmail.com'
RECEIVER_EMAIL='batracodes@gmail.com'
SENDER_PASSWORD=os.environ['GMAIL_PASSWORD']
# storing the subject
msg['Subject'] = "Top 10 Trending Youtube videos"
# string to store the body of the mail
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "trending.csv"
attachment = open("./trending.csv", "rb")
# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(SENDER_EMAIL, SENDER_PASSWORD)
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, text)
print('Email sent')
# terminating the session
s.quit()
if(__name__=='__main__'):
print('Creating driver')
driver = get_driver()
print('Fetching Page')
driver.get(YOUTUBE_TRENDING_URL)
print('Page title',driver.title)
videos = get_videos(driver)
print(f'Found {len(videos)} videos')
print('Parsing top ten videos')
video_data = [parse_video(video) for video in videos[:10]]
print('Saving Data to CSV')
video_df = pd.DataFrame(video_data)
print(video_df)
video_df.to_csv('trending.csv',index=None)
body="Please find attached top 10 trending videos in Youttube"
send_email_attached(body)
# print(parse_video(videos[0]))