Guest User

stahp

a guest
May 2nd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. #!
  2. __author__ = __author__ = [  'agentnola', 'chrispytoast123', 'jb567', 'electric-blue', 'rexrex600' ]
  3.  
  4. import gspread
  5. import json
  6. import praw
  7. import re
  8. from oauth2client.client import SignedJwtAssertionCredentials
  9. import concurrent.futures
  10. import getpass
  11. import time
  12.  
  13.  
  14. def checkURL():
  15.     print('Copy Voting Thread Link Below')
  16.     URL = str(input())
  17.     return URL
  18.  
  19.  
  20. def login():
  21.     user = str(input('Reddit Username:'))
  22.     try:
  23.         r.login(user,str(input('Reddit Password:')))
  24.     except praw.errors.InvalidUserPass:
  25.         print ("Incorrect Password")
  26.         login()
  27.  
  28.  
  29. sheetName = '10th Govt Voting Record'
  30. docName = 'MHoC Master Sheet'
  31. docKey = 'VoteCounter2-af942bc69325.json'
  32. totalMPs = 100
  33.  
  34.  
  35. json_key = json.load(open(docKey))
  36. scope = ['https://spreadsheets.google.com/feeds']
  37.  
  38.  
  39. credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
  40. r = praw.Reddit('MHOC-plebian house, vote counter v1')
  41. gc = gspread.authorize(credentials)
  42. sh = gc.open(docName)
  43. wks = sh.worksheet(sheetName)
  44.  
  45.  
  46. login()
  47. rThread = checkURL()
  48.  
  49.  
  50. strt = time.time()
  51.  
  52.  
  53. def findLastMP(wksColumn):
  54.     wksCellList = wks.col_values(wksColumn)
  55.     for wksCell in wksCellList:
  56.         if wksCell == "Speaker":
  57.             return wks.find(wksCell).row
  58.  
  59.  
  60. def getMPs():
  61.     col = getCol()
  62.     wksMPs = wks.col_values(3)[2:findLastMP(4)-1]
  63.     wksMPIsSitting = wks.col_values(col)[2:findLastMP(4)-1]
  64.     out = []
  65.     for i in range(len(wksMPs)):
  66.         if wksMPIsSitting[i] != 'N/A':
  67.             out.append(wksMPs[i])
  68.     return out
  69.  
  70.  
  71. def getVotes(url):
  72.     #getting the list of comments
  73.     rThread = r.get_submission(url)
  74.     rThread.replace_more_comments(limit=None, threshold=0)
  75.     rComments = praw.helpers.flatten_tree(rThread.comments)
  76.     #returning the list of comments
  77.     return rComments
  78.  
  79.  
  80. def getCol():
  81.     cells = wks.range('F3:BZ3')
  82.     for cell in cells:
  83.         if cell.value == '':
  84.             col = cell.col
  85.             break
  86.     return col
  87.  
  88.  
  89.  
  90. def getUpdateCells(MPs):
  91.     col = getCol()
  92.     updateList = wks.range(str(wks.get_addr_int(3, col)) + ":" + wks.get_addr_int(findLastMP(4)-1, col))
  93.     wksMPIsSitting = wks.col_values(col)[2:findLastMP(4)-1]
  94.     for i in updateList:
  95.         if wksMPIsSitting[updateList.index(i)] == 'N/A':
  96.             del wksMPIsSitting[updateList.index(i)]
  97.             updateList.remove(i)
  98.     return updateList
  99.  
  100.  
  101.  
  102. def sumVotes(vote, votes):
  103.     total = 0
  104.     for i in votes:
  105.         if i == vote:
  106.             total += 1
  107.     return total
  108.  
  109.  
  110.  
  111. def titleCol():
  112.     col = getCol()
  113.     title = str(rThread.title())
  114.     billNum = title.split("/")[-2]
  115.     billNum = billNum.split("_")[0]
  116.     print("You are counting " + billNum)
  117.     wks.update_cell(2, col, "=HYPERLINK(\"" + rThread + "\", \"" + billNum + "\")")
  118.  
  119.  
  120. titleCol()
  121.  
  122.  
  123. votes = getVotes(rThread)
  124. gMPs = getMPs()
  125. updateList = getUpdateCells(gMPs)
  126. duplicateVotes = []
  127. votesFinal = []
  128.  
  129.  
  130. def countVote(gMP):
  131.     voteCount = 0
  132.     for i in votes:
  133.         if str(i.author).lower() == gMP.lower():
  134.             voteCount += 1
  135.     if voteCount > 1:
  136.         return gMP, 'DNV', True
  137.     elif voteCount < 1:
  138.         return gMP, 'DNV', False
  139.     else:
  140.         for i in votes:
  141.             if str(i.author).lower() == gMP.lower():
  142.                 if 'aye' in str(i.body).lower():
  143.                     return gMP, 'Aye', False
  144.                 elif 'nay' in str(i.body).lower():
  145.                     return gMP, 'Nay', False
  146.                 elif 'abstain' in str(i.body).lower():
  147.                     return gMP, 'Abs', False
  148.  
  149.  
  150. print("The votes were as follows: ")
  151.  
  152.  
  153. with concurrent.futures.ThreadPoolExecutor() as executor:
  154.     for out in executor.map(countVote, gMPs):
  155.         #Handling output from counting function
  156.         MP, vote, isDupe = out
  157.         if isDupe == False:
  158.             print(MP + " : " + vote)
  159.             if not vote == 'DNV':
  160.                 votesFinal.append(vote)
  161.  
  162.         else:
  163.             print(MP + " voted more than once and recieved a DNV")
  164.             duplicateVotes.append(MP)
  165.         updateList[gMPs.index(MP)].value = vote
  166.  
  167.  
  168.  
  169. wks.update_cells(updateList)
  170.  
  171.  
  172. print("The Ayes to the right: " + str(sumVotes('Aye', votesFinal)))
  173. print("The Noes to the right: " + str(sumVotes('Nay', votesFinal)))
  174. print("Abstentions: " + str(sumVotes('Abs', votesFinal)))
  175. print(len(votesFinal), len(gMPs))
  176. turnout = str((len(votesFinal)/totalMPs)*100)
  177. print("Turnout: " + turnout + "%")
  178.  
  179.  
  180. end = time.time()
  181.  
  182.  
  183. print("The count took " + str(end-strt) + " seconds")
Add Comment
Please, Sign In to add comment