Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1.  
  2. import praw
  3. from datetime import datetime
  4. import time
  5. import gspread
  6. from oauth2client.service_account import ServiceAccountCredentials
  7.  
  8. # tracks what row in the sheet we're writing data to
  9. cursor = 3
  10.  
  11. # will be used to store # of instances of keyword
  12. count_new, count_hot, count_top, count_rising = 0, 0, 0, 0
  13. count_new2, count_hot2, count_top2, count_rising2 = 0, 0, 0, 0
  14. count_new3, count_hot3, count_top3, count_rising3 = 0, 0, 0, 0
  15.  
  16.  
  17. # Puts all the words from politics posts into a single list
  18. def list_builder(keyword0, keyword1):
  19.  
  20. # this is shitty and bad practice but oh well
  21. global cursor
  22.  
  23. # will be used to store # of instances of keyword
  24. global count_new, count_hot, count_top, count_rising
  25. global count_new2, count_hot2, count_top2, count_rising2
  26. global count_new3, count_hot3, count_top3, count_rising3
  27.  
  28. # use creds to create a client to interact with the Google Drive API
  29. scope = ['https://www.googleapis.com/auth/drive']
  30. creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
  31. client = gspread.authorize(creds)
  32.  
  33. # Find a workbook by name and open the first sheet
  34. sheet = client.open("Reddit Data with Twitter").sheet1
  35.  
  36. # getting our client data
  37. reddit = praw.Reddit(client_id='no',client_secret='no',
  38. user_agent='PythonCrawler',username='no', password='no')
  39.  
  40. # creating subreddit objects to pull from
  41. politics = reddit.subreddit('Politics')
  42. news = reddit.subreddit('News')
  43. world_news = reddit.subreddit('Worldnews')
  44.  
  45. # pulling /r/politics from client
  46. politics_new = politics.new(limit=100)
  47. politics_hot = politics.hot(limit=100)
  48. politics_top = politics.top(limit=100)
  49. politics_rising = politics.rising(limit=100)
  50.  
  51. # pulling /r/news from client
  52. news_new = news.new(limit=100)
  53. news_hot = news.hot(limit=100)
  54. news_top = news.top(limit=100)
  55. news_rising = news.rising(limit=100)
  56.  
  57. # pulling /r/worldnews from client
  58. world_news_new = world_news.new(limit=100)
  59. world_news_hot = world_news.hot(limit=100)
  60. world_news_top = world_news.top(limit=100)
  61. world_news_rising = world_news.rising(limit=100)
  62.  
  63. # updating all of the counts
  64. count_new = list_processor(politics_new, count_new, keyword0, keyword1)
  65. count_hot = list_processor(politics_hot, count_hot, keyword0, keyword1)
  66. count_top = list_processor(politics_top, count_top, keyword0, keyword1)
  67. count_rising = list_processor(politics_rising, count_rising, keyword0, keyword1)
  68.  
  69. # updating timestamp
  70. sheet.update_cell(cursor, 1, str(datetime.now()))
  71.  
  72. # updating new
  73. sheet.update_cell(cursor, 2, count_new)
  74.  
  75. # updating hot
  76. sheet.update_cell(cursor, 3, count_hot)
  77.  
  78. # updating top
  79. sheet.update_cell(cursor, 4, count_top)
  80.  
  81. # updating rising
  82. sheet.update_cell(cursor, 5, count_rising)
  83.  
  84. # updating all of the counts
  85. count_new = list_processor(news_new, count_new2, keyword0, keyword1)
  86. count_hot = list_processor(news_hot, count_hot2, keyword0, keyword1)
  87. count_top = list_processor(news_top, count_top2, keyword0, keyword1)
  88. count_rising = list_processor(news_rising, count_rising2, keyword0, keyword1)
  89.  
  90. # updating timestamp
  91. sheet.update_cell(cursor, 7, str(datetime.now()))
  92.  
  93. # updating new
  94. sheet.update_cell(cursor, 8, count_new2)
  95.  
  96. # updating hot
  97. sheet.update_cell(cursor, 9, count_hot2)
  98.  
  99. # updating top
  100. sheet.update_cell(cursor, 10, count_top2)
  101.  
  102. # updating rising
  103. sheet.update_cell(cursor, 11, count_rising2)
  104.  
  105. # updating all of the counts for /r/politics
  106. count_new = list_processor(world_news_new, count_new3, keyword0, keyword1)
  107. count_hot = list_processor(world_news_hot, count_hot3, keyword0, keyword1)
  108. count_top = list_processor(world_news_top, count_top3, keyword0, keyword1)
  109. count_rising = list_processor(world_news_rising, count_rising3, keyword0, keyword1)
  110.  
  111. # updating timestamp
  112. sheet.update_cell(cursor, 13, str(datetime.now()))
  113.  
  114. # updating /r/politics new
  115. sheet.update_cell(cursor, 14, count_new3)
  116.  
  117. # updating /r/politics hot
  118. sheet.update_cell(cursor, 15, count_hot3)
  119.  
  120. # updating /r/politics top
  121. sheet.update_cell(cursor, 16, count_top3)
  122.  
  123. # updating /r/politics rising
  124. sheet.update_cell(cursor, 17, count_rising3)
  125.  
  126. cursor += 1
  127. count_new, count_hot, count_top, count_rising = 0, 0, 0, 0
  128. count_new2, count_hot2, count_top2, count_rising2 = 0, 0, 0, 0
  129. count_new3, count_hot3, count_top3, count_rising3 = 0, 0, 0, 0
  130.  
  131. # searches through each submission title looking for keywords
  132. def list_processor(subreddit_category, count_category, keyword0, keyword1):
  133.  
  134. for submission in subreddit_category:
  135.  
  136. current_title = submission.title.split()
  137.  
  138. count_category += current_title.count(keyword0) + current_title.count(keyword1)
  139.  
  140. return count_category
  141.  
  142.  
  143. start_time = time.time()
  144.  
  145. while True:
  146. list_builder("Trump", "Trump's")
  147. time.sleep(10.0 - ((time.time() - start_time) % 10.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement