/
TwitterBotLike.py
65 lines (58 loc) · 2.48 KB
/
TwitterBotLike.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
# ---------------------------------------------------------
# Search Twitter via a Hashtag and then like the post
# Thank you Interwebs for coding help and troubleshooting hints
# Requirements:
# Python is a given
# Firefox, you could use Chrome but I haven't tested it yet
# Gecko Driver: https://github.com/mozilla/geckodriver/releases - Extract and place exe in root of Python
# Gecko is a Proxy for using W3C WebDriver compatible clients to interact with Gecko-based browsers.
# Selenium (webdriver): Simply 'pip install selenium', this allows Python to install it for you.
# ---------------------------------------------------------
import time
from getpass import getpass
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
strUserName = 'name@email.com'
# strUserPass = 'PassWord!'
strUserPass = getpass() # So the password isn't plain text but it's a choice now
# strHashTag = 'HashtagName'
strHashTag = input('HashTag? ')
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
self.bot = webdriver.Firefox()
def login(self):
bot = self.bot
bot.get('https://twitter.com/')
time.sleep(3)
email = bot.find_element_by_class_name('email-input')
password = bot.find_element_by_name('session[password]')
email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
password.send_keys(Keys.RETURN)
time.sleep(3)
def like_tweet(self,hashtag):
bot = self.bot
bot.get('https://twitter.com/search?q='+hashtag+'&src=typd')
time.sleep(3)
for _i in range(1,3):
bot.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(2)
tweets = bot.find_elements_by_class_name('tweet')
links = [elem.get_attribute('data-permalink-path')
for elem in tweets]
for link in links:
bot.get('https://twitter.com' + link)
print(link)
try:
bot.find_element_by_class_name('HeartAnimation').click()
time.sleep(10)
except Exception as _ex:
time.sleep(60)
# Create Instance
BotCrawl = TwitterBot(strUserName, strUserPass)
BotCrawl.login()
BotCrawl.like_tweet(strHashTag)