Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.common.exceptions import TimeoutException
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- import time, random, pickle, buffer_config, praw, reddit_config
- #Python program by Javier Marti
- '''THIS PROGRAM '''
- global posts_from_reddit
- posts_from_reddit = []
- global things_to_tweet
- things_to_tweet = []
- global temporary_container
- temporary_container = []
- global final_container_to_export_to_text_file
- final_container_to_export_to_text_file = []
- global already_posted_in_buffer
- already_posted_in_buffer = []
- global items_on_queue
- items_on_queue = []
- def process_reddit_submission():
- #LOG INTO REDDIT
- reddit = praw.Reddit(user_agent=reddit_config.user_agent,
- client_id=reddit_config.client_id,
- client_secret=reddit_config.client_secret,
- username=reddit_config.username,
- password=reddit_config.password)
- print('\n')
- print('------------------------------------------')
- print('Starting program and fetching submissions to subreddits...')
- print('------------------------------------------\n')
- # CHOOSE SUBREDDIT AND WHAT TO DO WITH EACH SUBMISSION
- # limit is the NUMBER OF POSTS we want to include
- subreddits_to_check = ['bitcoin_uncensored','futurology']
- for el in subreddits_to_check:
- print('TOP LINKS IN', el, 'subreddit')
- print('------------------------------------------------------------')
- for submission in reddit.subreddit(el).top(time_filter='day', limit=4):
- # check submission is not stickied or self text
- if submission.stickied == False and submission.is_self == False:
- shortened_submission = submission.title[0:55], '...', submission.url
- print(shortened_submission)
- print('-------------------------------------')
- posts_from_reddit.append(shortened_submission)
- time.sleep(1)
- print('This is the content of posts_from_reddit after adding all extracted posts from reddit\n', posts_from_reddit)
- print('............................................')
- # # WRITE TO FILE
- def compare_to_file_and_write_if_not_present():
- #DUMP CONTENTS OF TEXT FILE ON TEMPORARY CONTAINER
- with open('buffer_twitter_records.txt', 'r') as cache:
- for el in cache.read().splitlines():
- # print(el)
- temporary_container.append(el)
- #final_container_to_export_to_text_file.append(el)
- print('THIS IS TEMPORARY CONTAINER WHEN WE OPEN IT: \n')
- print(temporary_container)
- print('...............................................')
- # OBTAIN POSTS NOT ALREADY IN TEXT FILE
- for el in posts_from_reddit:
- if el not in temporary_container:
- things_to_tweet.append(el)
- # ADD TO FINAL CONTAINER TO EXPORT TO TEXT FILE
- for el in things_to_tweet:
- if el not in final_container_to_export_to_text_file:
- #print('I am tweeting ', el)
- final_container_to_export_to_text_file.append(el)
- def dump_into_text_file():
- # DUMP TEMPORARY LIST TO TEXT FILE
- with open('buffer_twitter_records.txt', 'w') as cache:
- cache.seek(0)
- for el in final_container_to_export_to_text_file:
- cache.write(str(el))
- cache.write(str('\n'))
- # cache.write(bytes("write me to the file\n", 'UTF-8')
- print('-----------------------------------------------')
- print('FINAL CONTAINER TO EXPORT TO TEXT FILE CONTAINS: \n')
- print(final_container_to_export_to_text_file)
- print('FINAL THINGS TO TWEET: \n')
- print(things_to_tweet)
- def check_queue():
- time.sleep(random.uniform(1,2))
- queue_button = browser.find_element_by_partial_link_text('ueue')
- queue_button.click()
- conten_items = browser.find_elements_by_class_name('content')
- for el in conten_items:
- items_on_queue.append(el)
- print('There are currently', len(items_on_queue),'items in the queue')
- ##################################################################
- ##################################################################
- ##################################################################
- #TESTING..........................trying to figure out how to delete duplicates that are already posted on buffer and the things_to_tweet list with things we're just about to tweet
- print('\n These are the items on items_on_queue')
- for el in items_on_queue:
- shortened_ioq = el.text[0:14]
- print(el.text[0:14])
- #already_posted_in_buffer.append(shortened_ioq)
- print('\n These are the items on things_to_tweet')
- for el in things_to_tweet:
- shortened_ttt = el[0][0:14]
- print(el[0][0:14])
- # define a helper function that checks if any string in a list
- # starts with another string
- # we will use this to check if any string in items_on_queue starts
- # with a string from things_to_tweet
- def is_prefix_of_any(prefix, strings):
- for string in strings:
- if string.startswith(prefix):
- return True
- return False
- # build a new list containing only the strings we want
- things = []
- for thing in things_to_tweet:
- if not is_prefix_of_any(thing, items_on_queue):
- things.append(thing)
- print('\n This is the printing of THINGS')
- print(things)
- print('\nThese are the items on things_to_tweet AFTER COMPARISON WITH ITEMS ON ITEMS_ON_QUEUE')
- print(things_to_tweet)
- # for item1 in list1:
- # for item2 in list2:
- # if item1 == item2:
- # print
- # item1
- # for el.text[0:14] in items_on_queue:
- # for el[0][0:14] in things_to_tweet:
- # if el.text[0:14] == el[0][0:14]:
- # print(el.text[0:14], 'is in both lists')
- ##################################################################
- ##################################################################
- ##################################################################
- def verify_or_log_in():
- #check if already logged in
- login_button = browser.find_element_by_class_name('btn-action')
- time.sleep(random.uniform(1, 2))
- if login_button:
- #if not logged in, log in
- try:
- time.sleep(random.uniform(1,2))
- login_button.click()
- username = browser.find_element_by_id("email")
- username.send_keys(buffer_config.username)
- # wait between key presses
- time.sleep(random.uniform(1,1.5))
- password = browser.find_element_by_id("password")
- password.send_keys(buffer_config.password)
- time.sleep(random.uniform(0.8,1.5))
- browser.find_element_by_name('submit').click()
- print("Finished logging in...")
- time.sleep(random.uniform(1,2))
- except:
- pass
- print("Something went wrong OR WE'RE ALREADY LOGGED IN")
- #post to Buffer
- def post_message_to_website():
- counter = 0
- print('Filling fields with message...')
- for el in range(9 - (len(items_on_queue))):
- if el not in already_posted_in_buffer:
- for el in things_to_tweet:
- try:
- #post tweet
- time.sleep(random.uniform(1,2))
- content_button = browser.find_element_by_partial_link_text('ontent')
- content_button.click()
- time.sleep(random.uniform(2,3))
- #text_box.sendKeys(Keys.chord(Keys.ALT, "b"))
- if browser.find_element_by_class_name('js-queue-header'):
- browser.find_element_by_class_name('js-queue-header').click()
- text_box = browser.find_element_by_id('composer')
- # text_box.sendKeys(Keys.chord(Keys.CONTROL, "a"))
- # text_box.sendKeys(Keys.chord(Keys.DELETE))
- #Material to post
- tweet_to_post = el
- time.sleep(random.uniform(4,6))
- text_box.clear()
- text_box.send_keys(tweet_to_post)
- #text_box.sendKeys(Keys.chord(Keys.ENTER))
- time.sleep(random.uniform(9,12))
- #select first image
- # try:
- # post_pic = browser.find_element_by_class_name('scraped-images')
- # print('A1')
- # post_pic[0].click()
- # print(A2)
- # time.sleep(random.uniform(5,7))
- # except:
- # continue
- #press submit
- add_to_queue_button = browser.find_element_by_id('share-later')
- add_to_queue_button.click()
- print('Message successfully posted')
- #post in this list so as not to try to post the same thing again
- already_posted_in_buffer.append(el)
- if el not in final_container_to_export_to_text_file:
- final_container_to_export_to_text_file.append(el)
- except:
- print('Exception. Something went wrong when trying to post to website. Message has NOT been posted')
- del things_to_tweet[str(el)]
- counter = counter + 1
- if counter == 2:
- break
- #send email to report of problem
- def shuffle_posts_on_exit():
- time.sleep(random.uniform(1, 2))
- queue_button = browser.find_element_by_partial_link_text('ueue')
- queue_button.click()
- time.sleep(random.uniform(1, 2))
- queue_button = browser.find_element_by_partial_link_text('uffle')
- queue_button.click()
- time.sleep(1)
- #confirm shuffle
- button = browser.find_element_by_xpath('//*[@id="modal-buttons"]/a[2]')
- button.click()
- print('Shuffle has been done')
- ###################################
- process_reddit_submission()
- compare_to_file_and_write_if_not_present()
- chromedriver = 'D:\\Program files\\chromedriver_win32\\chromedriver.exe'
- browser = webdriver.Chrome(chromedriver)
- browser.get('https://buffer.com/')
- #Login to buffer
- verify_or_log_in()
- #Verify how many posts in queue
- check_queue()
- #Substract and send posts to complete 10 posts in queue
- post_message_to_website()
- shuffle_posts_on_exit()
- dump_into_text_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement