Created
December 23, 2015 14:24
-
-
Save BookOwl/b6ef48cc328bd135caf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python3 | |
# selenium_forum_poster.py - Scratch forum bot using Selenium | |
import time, pickle, random, os.path | |
from selenium import webdriver | |
RESPONSES = ( | |
"Support.", | |
"No support.", | |
"workaroundable, no support.", | |
"100% support!", | |
"Eh, could be confusing to new scratchers, no support.", | |
"Great idea!", | |
"50% support.", | |
"[scratchblocks]add [me] to [supporters v][/scratchblocks]", | |
"Semi-support", | |
) | |
def get_suggestion_topic_ids(browser, p): | |
browser.get("https://scratch.mit.edu/discuss/1/?page=%s" % p) | |
ids = [] | |
elems = None | |
while not elems: | |
elems = browser.find_elements_by_css_selector("td .tclcon") | |
for elem in elems: | |
topic_elem = elem.find_element_by_tag_name("a") | |
ids.append(topic_elem.get_attribute("href")[38:-1]) | |
return ids[6:] | |
def login(browser, username, password): | |
browser.get("https://scratch.mit.edu/") | |
time.sleep(2) | |
login_button = browser.find_elements_by_css_selector( | |
"#navigation .inner > ul .right")[1] | |
login_button.click() | |
username_elem = browser.find_element_by_name("username") | |
password_elem = browser.find_element_by_name("password") | |
username_elem.send_keys(username) | |
time.sleep(1) | |
password_elem.send_keys(password) | |
time.sleep(3) | |
username_elem.submit() | |
def post(browser, topic_id, message): | |
browser.get("https://scratch.mit.edu/discuss/topic/%s/" % topic_id) | |
time.sleep(5) | |
textarea = browser.find_element_by_class_name("markItUpEditor") | |
textarea.send_keys(message) | |
submit = browser.find_element_by_name("AddPostForm") | |
time.sleep(1) | |
submit.click() | |
def create_record(): | |
f = open("forum_poster_record.pickle", "wb") | |
pickle.dump([], f) | |
f.close() | |
def get_record(): | |
f = open("forum_poster_record.pickle", "rb") | |
rec = pickle.load(f) | |
f.close() | |
return rec | |
def record_post(topic_id): | |
rec = get_record() | |
rec.append(topic_id) | |
f = open("forum_poster_record.pickle", "wb") | |
pickle.dump(rec, f) | |
f.close() | |
if __name__ == '__main__': | |
if not os.path.exists("forum_poster_record.pickle"): | |
create_record() | |
username = input("Username: ") | |
password = input("Password: ") | |
browser = webdriver.Firefox() | |
login(browser, username, password) | |
time.sleep(4) | |
last_post = None | |
p = 1 | |
for i in range(10): | |
print("Post #%s" % i) | |
print("Getting topic IDs...") | |
topic_ids = get_suggestion_topic_ids(browser, p) | |
time.sleep(1) | |
rec = get_record() | |
while True: | |
topic_id = random.choice(topic_ids) | |
if topic_id not in rec: | |
break | |
topic_ids.remove(topic_id) | |
if len(topic_ids) == 0: | |
print("Getting new topics...") | |
p += 1 | |
topic_ids = get_suggestion_topic_ids(browser, p) | |
print("Picking message...") | |
message = random.choice(RESPONSES) | |
while last_post == message: | |
message = random.choice(RESPONSES) | |
last_post = message | |
print("Message: %s" % message) | |
print("Topic ID: %s" % topic_id) | |
#input("Hit enter to confirm, or control-c to exit") | |
try: | |
post(browser, topic_id, message) | |
print("Success!") | |
except Exception as e: | |
print("ERROR: %s" % e) | |
record_post(topic_id) | |
print("Sleeping...\n") | |
time.sleep(random.randint(180, 240)) | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I edit this to create a similar program for Google+ ? I'd be happy to credit you 👍