Advertisement
Guest User

Test

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