-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
115 lines (79 loc) · 2.99 KB
/
index.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
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
# --------------------- IMPORTS ---------------------
# main selenium webdrive
from selenium import webdriver
# provides support for keyboard keys e.g. mocking a keypress
from selenium.webdriver.common.keys import Keys
# importing options to make the browser headless i.e. doesn't open a window
# while doing all the scraping / crawling stuff
from selenium.webdriver.chrome.options import Options
from getFedora import savePosts
from db import getNewPosts
# --------------------- INIT STUFF ---------------------
options = webdriver.ChromeOptions() # importing for making chrome headless
options.add_argument('headless') # headless means chrome doesn't pop up
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(30)
def login(username, password):
# email id / username
username = username
# password
password = password
# -------- logging in ------
# going to login page
url = 'https://twitter.com/login'
# getting url
driver.get(url)
# email id
# js-username-field -> email
inputElement = content = driver.find_element_by_class_name('js-username-field')
inputElement.send_keys(username)
# password
# js-password-field -> password
inputElement = content = driver.find_element_by_class_name('js-password-field')
inputElement.send_keys(password)
# sending form/input
inputElement.send_keys(Keys.RETURN)
def postTweet(tweetMessage):
# Already Logged In
# message to be tweeted
tweetMessage = tweetMessage
# -------- POSTING TWEET ----------
# going to page meant for posting
url = 'https://twitter.com/compose/tweet'
driver.get(url)
# typing message
inputElement = driver.find_element_by_class_name('public-DraftEditor-content')
inputElement.send_keys(tweetMessage)
# sending tweet
submitButton = driver.find_element_by_css_selector('div[data-testid=tweetButton]')
submitButton.click()
def execute(username, password):
# saving new posts to DB
savePosts()
# Status Print
print('Posts saved')
# getting and saving new posts
# if there are new posts, result will be a list of post dictionaries
# else, i.e. no new posts, result will be a string with the message
# 'No new posts'
result = getNewPosts()
# status print
print('Scraped Fedora site and saved all new posts in db (IF ANY)')
if type(result) == str: # i.e. no new posts
print(result)
print('Exiting the program')
else: # i.e. there are new posts
# logging into twitter
login(username, password)
# iterating through new posts
for post in result:
# generating message to be sent
msg = f"{post['title']}\n\n{post['link']}"
# posting tweet
postTweet(msg)
print('Tweeted all new posts!')
print('Exiting the program')
# type in your twitter username and password as below
# execute(<username>, <password>)
# closing tab/window
driver.close()