From 4e9668a91f514b82c644f2ba9b90ff2fef0b3a58 Mon Sep 17 00:00:00 2001 From: Tahmim Ahmed Shibli Date: Thu, 23 Jul 2020 14:53:55 +0600 Subject: [PATCH] Adds random quotes from forismatic.com --- README.md | 2 +- main.py | 129 ++++++++++++++++++++++++++--------------------- requirements.txt | 6 +++ 3 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index 4a63485..ca56025 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.py b/main.py index 1c56174..4429e20 100644 --- a/main.py +++ b/main.py @@ -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) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f149d77 --- /dev/null +++ b/requirements.txt @@ -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