Skip to content

Commit

Permalink
Adds random quotes from forismatic.com
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahmim Ahmed Shibli committed Jul 23, 2020
1 parent c9ccc60 commit 4e9668a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ your_pc$ virtualenv -p /usr/bin/python3 venv
your_pc$ source venv/bin/activate
```
```console
(venv)your_pc$ pip install selenium
(venv)your_pc$ pip install -r requirements.txt
```
- Have firefox installed
- I used geckodriver for linux. If you run windows, install geckodriver for windows.
Expand Down
129 changes: 72 additions & 57 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,88 @@
import random
import string
import os
import requests

# Opening env.json file
f = open('env.json',)

# returns JSON object as a dictionary
credentials = json.load(f)

# Closing file
f.close()

username = credentials['username']
password = credentials['password']
target_username = credentials['target_username']


firefox_options = Options()
firefox_options.add_argument('--dns-prefetch-disable')
firefox_options.add_argument('--no-sandbox')
firefox_options.add_argument('--lang=en-US')
browser = webdriver.Firefox()


browser.get('https://www.facebook.com/')
signup_elem = browser.find_element_by_id('email')
signup_elem.send_keys(username)
def automate_facebook_comment(comment_count=1000):
# Opening env.json file
f = open('env.json',)

# returns JSON object as a dictionary
credentials = json.load(f)

login_elem = browser.find_element_by_id('pass')
login_elem.send_keys(password)
# Closing file
f.close()

ins = browser.find_elements_by_tag_name('input')
for x in ins:
if x.get_attribute('value') == 'Log In':
x.click() # here logged in
break
username = credentials['username']
password = credentials['password']
target_username = credentials['target_username']

#then key here move to mobile version as that doesn't support javascript
browser.get('https://m.facebook.com/'+target_username+'?v=timeline')

firefox_options = Options()
firefox_options.add_argument('--dns-prefetch-disable')
firefox_options.add_argument('--no-sandbox')
firefox_options.add_argument('--lang=en-US')
browser = webdriver.Firefox()

# find last post (occurance of comment)
as_el = browser.find_elements_by_tag_name('a')
for a in as_el:
print(a.text)
if 'omment' in a.text.strip():
a.click()
break
time.sleep(10)

# it will go for 1000 comments
i = 1000
while i > 0:
# do actual comment
ins = browser.find_element_by_id('composerInput')
browser.get('https://www.facebook.com/')
signup_elem = browser.find_element_by_id('email')
signup_elem.send_keys(username)

cmnt = random.choice(string.ascii_letters) # just picked any random letter

ins.send_keys(cmnt)
# submit input
login_elem = browser.find_element_by_id('pass')
login_elem.send_keys(password)

ins = browser.find_elements_by_tag_name('input')
for x in ins:
if 'omment' in x.get_attribute('value'):
x.click()
if x.get_attribute('value') == 'Log In':
x.click() # here logged in
break

#random waiting
r = random.randrange(2,5)
time.sleep(r)

i = i - 1
#then key here move to mobile version as that doesn't support javascript
browser.get('https://m.facebook.com/'+target_username+'?v=timeline')


# find last post (occurance of comment)
as_el = browser.find_elements_by_tag_name('a')
for a in as_el:
print(a.text)
if 'omment' in a.text.strip():
a.click()
break
time.sleep(10)

# it will go for 1000 comments
i = comment_count
while i > 0:
# do actual comment
ins = browser.find_element_by_id('composerInput')

cmnt = getRandomQuote()

ins.send_keys(cmnt)
# submit input

ins = browser.find_elements_by_tag_name('input')
for x in ins:
if 'omment' in x.get_attribute('value'):
x.click()
break

#random waiting
r = random.randrange(2,5)
time.sleep(r)

i = i - 1

def getRandomQuote():
params = {
'method':'getQuote',
'lang':'en',
'format':'json'
}
response = requests.get('http://api.forismatic.com/api/1.0/',params)
jsonText =json.loads(response.text)
return f'{jsonText["quoteText"]} - {jsonText["quoteAuthor"]}'

if __name__ == "__main__":
automate_facebook_comment(1000)
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
certifi==2020.6.20
chardet==3.0.4
idna==2.10
requests==2.24.0
selenium==3.141.0
urllib3==1.25.10

0 comments on commit 4e9668a

Please sign in to comment.