Advertisement
Guest User

detroitpistons2

a guest
Nov 2nd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #PistonsBotS v1.0.0 by /u/TeroTheTerror and revised by /u/defeatedmac
  2. import datetime
  3. import time
  4. import urllib2
  5. import HTMLParser
  6. import praw
  7. from bs4 import BeautifulSoup
  8.  
  9. class Hockey_Bot_S(object):
  10.  
  11. def __init__(self):
  12. #Define divisions in a list
  13. divisions = ['Eastern', 'Western']
  14. username=('DefMacsPistonsBot')
  15. password=('Chauncey1')
  16. r.login(username,password)
  17. subreddit=r.get_subreddit('DetroitPistons') #Change the subreddit if necessary
  18. alreadydone=set()
  19. self.userAgent = ('Standings_Bot_S/v1.0.0 by Defeatedmac based off work by TeroTheTerror')
  20.  
  21. while (True):
  22. print ('\nConference:')
  23. y = 1
  24. #Number conference and print in list
  25. for div in divisions:
  26. print (str(y)+': '+div)
  27. y += 1
  28.  
  29. self.division = 1
  30.  
  31. self.fix = (raw_input('\nWould you like the table to be formatted for icons? \n(y/n): ').lower())
  32.  
  33.  
  34. def scrape(self):
  35. w = urllib2.urlopen('http://www.espn.com.au/nba/standings')
  36. soup = BeautifulSoup(w.read())
  37.  
  38. standings_list = []
  39. final_list1 = []
  40. final_list2 = []
  41. #Find the division table
  42. if self.division == 1:
  43. for table in soup.findAll('table', summary="This shit works brah."):
  44. rawdata = table
  45. else:
  46. print ('An error has occured, please contact TeroTheTerror')
  47.  
  48. #Make list of cells within a list of rows
  49. rawdata_list = [tr.findAll('td') for tr in rawdata.findAll('tr')]
  50. #Text only version of rawdata_list
  51. for row in rawdata_list:
  52. standings_list.append([cell.text for cell in row])
  53. #Get rid of problematic characters
  54. for lst in standings_list:
  55. final_list1.append([val.replace(u'\xa0', u' ') for val in lst])
  56.  
  57. for lst in final_list1:
  58. final_list2.append([val.replace(u'\xe9', u'e') for val in lst])
  59.  
  60. w.close()
  61. return final_list2
  62.  
  63. def generate_tables(self, lst):
  64. #Time/Date stamp
  65. updated = datetime.datetime.now().strftime('%b %d, %Y at %I:%M%p')
  66. if self.division == 1:
  67. #Compiling the table
  68. standings = "Last Updated: " + updated + "\n"
  69. standings += "\n|Rank|Team|W|L|%|GB|"
  70. standings += "\n|:--:|:--:|:--:|:--:|:--:|:--:|:--:|"
  71. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[1][0], lst[1][1], lst[1][2], lst[1][3], lst[1][4], lst[1][5], lst[1][6])
  72. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[2][0], lst[2][1], lst[2][2], lst[2][3], lst[2][4], lst[2][5], lst[2][6])
  73. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[3][0], lst[3][1], lst[3][2], lst[3][3], lst[3][4], lst[3][5], lst[3][6])
  74. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[4][0], lst[4][1], lst[4][2], lst[4][3], lst[4][4], lst[4][5], lst[4][6])
  75. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[5][0], lst[5][1], lst[5][2], lst[5][3], lst[5][4], lst[5][5], lst[5][6])
  76. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[6][0], lst[6][1], lst[6][2], lst[6][3], lst[6][4], lst[6][5], lst[6][6])
  77. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[7][0], lst[7][1], lst[7][2], lst[7][3], lst[7][4], lst[7][5], lst[7][6])
  78. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[8][0], lst[8][1], lst[8][2], lst[8][3], lst[8][4], lst[8][5], lst[8][6])
  79. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[9][0], lst[9][1], lst[9][2], lst[9][3], lst[9][4], lst[9][5], lst[9][6])
  80. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[10][0], lst[10][1], lst[10][2], lst[10][3], lst[10][4], lst[10][5], lst[10][6])
  81. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[11][0], lst[11][1], lst[11][2], lst[11][3], lst[11][4], lst[11][5], lst[11][6])
  82. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[12][0], lst[12][1], lst[12][2], lst[12][3], lst[12][4], lst[12][5], lst[12][6])
  83. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[13][0], lst[13][1], lst[13][2], lst[13][3], lst[13][4], lst[13][5], lst[13][6])
  84. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[14][0], lst[14][1], lst[14][2], lst[14][3], lst[14][4], lst[14][5], lst[14][6])
  85. standings += "\n|{0}|{1}|{2}|{3}|{4}|{5}|{6}|".format(lst[15][0], lst[15][1], lst[15][2], lst[15][3], lst[15][4], lst[15][5], lst[15][6])
  86.  
  87.  
  88.  
  89. return standings
  90.  
  91. def create_sidebar(self):
  92. #To fix character glitch when grabbing the sidebar
  93. h = HTMLParser.HTMLParser()
  94. #Initialize PRAW and login
  95. r = praw.Reddit(user_agent='HocketBotS v1.1 by TeroTheTerror')
  96. r.login(self.username,self.password)
  97. #Grab the sidebar template from the wiki
  98. sidebar = r.get_subreddit(self.subreddit).get_wiki_page('edit_sidebar').content_md
  99. #Create list from sidebar by splitting at ***
  100. sidebar_list = sidebar.split('***')
  101. #Sidebar with updated tables - +lucky_guess+sidebar_list[6]
  102. sidebar = (sidebar_list[0]+standings_a+sidebar_list[2])
  103. #Fix characters in sidebar
  104. sidebar = h.unescape(sidebar)
  105.  
  106. return sidebar
  107.  
  108. def update_reddit(self):
  109. #Initialize PRAW and login
  110. r = praw.Reddit(user_agent='HocketBotS v1.1 by TeroTheTerror')
  111. r.login(self.username,self.password)
  112. #Grab the current settings
  113. settings = r.get_subreddit(self.subreddit).get_settings()
  114. #Update the sidebar
  115. settings['description'] = sidebar
  116. settings = r.get_subreddit(self.subreddit).update_settings(description=settings['description'])
  117.  
  118. hbs = Hockey_Bot_S()
  119.  
  120. while(True):
  121. print ('Scraping Standings...')
  122. final_list = hbs.scrape()
  123. print ('Generating Table...')
  124. standings_b = hbs.generate_tables(final_list)
  125. print ('Fixing Table...')
  126. standings_a = hbs.fix_standings(standings_b)
  127. print ('Grabbing Sidebar Template...')
  128. sidebar = hbs.create_sidebar()
  129. print ('Updating Sidebar...')
  130. hbs.update_reddit()
  131. print ('Sidebar Updated: '+datetime.datetime.now().strftime('%b %d, %Y at %I:%M%p'))
  132. print ('Sleeping... \n')
  133. time.sleep(600)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement