Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. from selenium import webdriver
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.common.exceptions import TimeoutException
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.webdriver.support import expected_conditions as EC
  8. import time, random, pickle, buffer_config, praw, reddit_config
  9. #Python program by Javier Marti
  10. '''THIS PROGRAM '''
  11.  
  12. global posts_from_reddit
  13. posts_from_reddit = []
  14. global things_to_tweet
  15. things_to_tweet = []
  16. global temporary_container
  17. temporary_container = []
  18. global final_container_to_export_to_text_file
  19. final_container_to_export_to_text_file = []
  20. global already_posted_in_buffer
  21. already_posted_in_buffer = []
  22. global items_on_queue
  23. items_on_queue = []
  24.  
  25. def process_reddit_submission():
  26. #LOG INTO REDDIT
  27. reddit = praw.Reddit(user_agent=reddit_config.user_agent,
  28. client_id=reddit_config.client_id,
  29. client_secret=reddit_config.client_secret,
  30. username=reddit_config.username,
  31. password=reddit_config.password)
  32.  
  33. print('\n')
  34. print('------------------------------------------')
  35. print('Starting program and fetching submissions to subreddits...')
  36. print('------------------------------------------\n')
  37.  
  38. # CHOOSE SUBREDDIT AND WHAT TO DO WITH EACH SUBMISSION
  39. # limit is the NUMBER OF POSTS we want to include
  40. subreddits_to_check = ['bitcoin_uncensored','futurology']
  41.  
  42. for el in subreddits_to_check:
  43. print('TOP LINKS IN', el, 'subreddit')
  44. print('------------------------------------------------------------')
  45. for submission in reddit.subreddit(el).top(time_filter='day', limit=4):
  46. # check submission is not stickied or self text
  47. if submission.stickied == False and submission.is_self == False:
  48. shortened_submission = submission.title[0:55], '...', submission.url
  49. print(shortened_submission)
  50. print('-------------------------------------')
  51. posts_from_reddit.append(shortened_submission)
  52. time.sleep(1)
  53. print('This is the content of posts_from_reddit after adding all extracted posts from reddit\n', posts_from_reddit)
  54. print('............................................')
  55.  
  56.  
  57.  
  58. # # WRITE TO FILE
  59. def compare_to_file_and_write_if_not_present():
  60.  
  61. #DUMP CONTENTS OF TEXT FILE ON TEMPORARY CONTAINER
  62. with open('buffer_twitter_records.txt', 'r') as cache:
  63. for el in cache.read().splitlines():
  64. # print(el)
  65. temporary_container.append(el)
  66. #final_container_to_export_to_text_file.append(el)
  67.  
  68. print('THIS IS TEMPORARY CONTAINER WHEN WE OPEN IT: \n')
  69. print(temporary_container)
  70. print('...............................................')
  71.  
  72. # OBTAIN POSTS NOT ALREADY IN TEXT FILE
  73. for el in posts_from_reddit:
  74. if el not in temporary_container:
  75. things_to_tweet.append(el)
  76.  
  77. # ADD TO FINAL CONTAINER TO EXPORT TO TEXT FILE
  78. for el in things_to_tweet:
  79. if el not in final_container_to_export_to_text_file:
  80. #print('I am tweeting ', el)
  81. final_container_to_export_to_text_file.append(el)
  82.  
  83. def dump_into_text_file():
  84. # DUMP TEMPORARY LIST TO TEXT FILE
  85. with open('buffer_twitter_records.txt', 'w') as cache:
  86. cache.seek(0)
  87. for el in final_container_to_export_to_text_file:
  88. cache.write(str(el))
  89. cache.write(str('\n'))
  90. # cache.write(bytes("write me to the file\n", 'UTF-8')
  91.  
  92. print('-----------------------------------------------')
  93. print('FINAL CONTAINER TO EXPORT TO TEXT FILE CONTAINS: \n')
  94. print(final_container_to_export_to_text_file)
  95. print('FINAL THINGS TO TWEET: \n')
  96. print(things_to_tweet)
  97.  
  98. def check_queue():
  99. time.sleep(random.uniform(1,2))
  100. queue_button = browser.find_element_by_partial_link_text('ueue')
  101. queue_button.click()
  102.  
  103. conten_items = browser.find_elements_by_class_name('content')
  104. for el in conten_items:
  105. items_on_queue.append(el)
  106.  
  107. print('There are currently', len(items_on_queue),'items in the queue')
  108.  
  109. ##################################################################
  110. ##################################################################
  111. ##################################################################
  112. #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
  113. print('\n These are the items on items_on_queue')
  114. for el in items_on_queue:
  115. shortened_ioq = el.text[0:14]
  116. print(el.text[0:14])
  117. #already_posted_in_buffer.append(shortened_ioq)
  118.  
  119. print('\n These are the items on things_to_tweet')
  120. for el in things_to_tweet:
  121. shortened_ttt = el[0][0:14]
  122. print(el[0][0:14])
  123.  
  124. # define a helper function that checks if any string in a list
  125. # starts with another string
  126. # we will use this to check if any string in items_on_queue starts
  127. # with a string from things_to_tweet
  128. def is_prefix_of_any(prefix, strings):
  129. for string in strings:
  130. if string.startswith(prefix):
  131. return True
  132. return False
  133.  
  134. # build a new list containing only the strings we want
  135. things = []
  136. for thing in things_to_tweet:
  137. if not is_prefix_of_any(thing, items_on_queue):
  138. things.append(thing)
  139.  
  140. print('\n This is the printing of THINGS')
  141. print(things)
  142.  
  143.  
  144.  
  145. print('\nThese are the items on things_to_tweet AFTER COMPARISON WITH ITEMS ON ITEMS_ON_QUEUE')
  146. print(things_to_tweet)
  147. # for item1 in list1:
  148. # for item2 in list2:
  149. # if item1 == item2:
  150. # print
  151. # item1
  152. # for el.text[0:14] in items_on_queue:
  153. # for el[0][0:14] in things_to_tweet:
  154. # if el.text[0:14] == el[0][0:14]:
  155. # print(el.text[0:14], 'is in both lists')
  156.  
  157. ##################################################################
  158. ##################################################################
  159. ##################################################################
  160.  
  161.  
  162.  
  163. def verify_or_log_in():
  164. #check if already logged in
  165. login_button = browser.find_element_by_class_name('btn-action')
  166. time.sleep(random.uniform(1, 2))
  167. if login_button:
  168. #if not logged in, log in
  169. try:
  170. time.sleep(random.uniform(1,2))
  171. login_button.click()
  172. username = browser.find_element_by_id("email")
  173. username.send_keys(buffer_config.username)
  174. # wait between key presses
  175. time.sleep(random.uniform(1,1.5))
  176. password = browser.find_element_by_id("password")
  177. password.send_keys(buffer_config.password)
  178. time.sleep(random.uniform(0.8,1.5))
  179. browser.find_element_by_name('submit').click()
  180. print("Finished logging in...")
  181. time.sleep(random.uniform(1,2))
  182.  
  183. except:
  184. pass
  185. print("Something went wrong OR WE'RE ALREADY LOGGED IN")
  186.  
  187. #post to Buffer
  188. def post_message_to_website():
  189. counter = 0
  190. print('Filling fields with message...')
  191. for el in range(9 - (len(items_on_queue))):
  192. if el not in already_posted_in_buffer:
  193. for el in things_to_tweet:
  194. try:
  195. #post tweet
  196. time.sleep(random.uniform(1,2))
  197. content_button = browser.find_element_by_partial_link_text('ontent')
  198. content_button.click()
  199. time.sleep(random.uniform(2,3))
  200. #text_box.sendKeys(Keys.chord(Keys.ALT, "b"))
  201. if browser.find_element_by_class_name('js-queue-header'):
  202. browser.find_element_by_class_name('js-queue-header').click()
  203. text_box = browser.find_element_by_id('composer')
  204.  
  205. # text_box.sendKeys(Keys.chord(Keys.CONTROL, "a"))
  206. # text_box.sendKeys(Keys.chord(Keys.DELETE))
  207.  
  208. #Material to post
  209. tweet_to_post = el
  210. time.sleep(random.uniform(4,6))
  211. text_box.clear()
  212. text_box.send_keys(tweet_to_post)
  213. #text_box.sendKeys(Keys.chord(Keys.ENTER))
  214. time.sleep(random.uniform(9,12))
  215.  
  216. #select first image
  217. # try:
  218. # post_pic = browser.find_element_by_class_name('scraped-images')
  219. # print('A1')
  220. # post_pic[0].click()
  221. # print(A2)
  222. # time.sleep(random.uniform(5,7))
  223. # except:
  224. # continue
  225.  
  226. #press submit
  227. add_to_queue_button = browser.find_element_by_id('share-later')
  228. add_to_queue_button.click()
  229. print('Message successfully posted')
  230. #post in this list so as not to try to post the same thing again
  231. already_posted_in_buffer.append(el)
  232.  
  233. if el not in final_container_to_export_to_text_file:
  234. final_container_to_export_to_text_file.append(el)
  235.  
  236. except:
  237. print('Exception. Something went wrong when trying to post to website. Message has NOT been posted')
  238. del things_to_tweet[str(el)]
  239. counter = counter + 1
  240. if counter == 2:
  241. break
  242. #send email to report of problem
  243.  
  244.  
  245. def shuffle_posts_on_exit():
  246. time.sleep(random.uniform(1, 2))
  247. queue_button = browser.find_element_by_partial_link_text('ueue')
  248. queue_button.click()
  249. time.sleep(random.uniform(1, 2))
  250. queue_button = browser.find_element_by_partial_link_text('uffle')
  251. queue_button.click()
  252. time.sleep(1)
  253. #confirm shuffle
  254. button = browser.find_element_by_xpath('//*[@id="modal-buttons"]/a[2]')
  255. button.click()
  256. print('Shuffle has been done')
  257.  
  258. ###################################
  259.  
  260.  
  261. process_reddit_submission()
  262.  
  263. compare_to_file_and_write_if_not_present()
  264.  
  265. chromedriver = 'D:\\Program files\\chromedriver_win32\\chromedriver.exe'
  266. browser = webdriver.Chrome(chromedriver)
  267. browser.get('https://buffer.com/')
  268.  
  269. #Login to buffer
  270. verify_or_log_in()
  271.  
  272. #Verify how many posts in queue
  273. check_queue()
  274.  
  275. #Substract and send posts to complete 10 posts in queue
  276. post_message_to_website()
  277.  
  278. shuffle_posts_on_exit()
  279.  
  280. dump_into_text_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement