Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #PistonsBotS v1.0.0 by /u/TeroTheTerror and revised by /u/defeatedmac
- import datetime
- import time
- import urllib2
- import HTMLParser
- import praw
- from bs4 import BeautifulSoup
- class Hockey_Bot_S(object):
- def __init__(self):
- #Define divisions in a list
- divisions = ['Eastern', 'Western']
- username=('DefMacsPistonsBot')
- password=('Chauncey1')
- r.login(username,password)
- subreddit=r.get_subreddit('DetroitPistons') #Change the subreddit if necessary
- alreadydone=set()
- self.userAgent = ('Standings_Bot_S/v1.0.0 by Defeatedmac based off work by TeroTheTerror')
- while (True):
- print ('\nConference:')
- y = 1
- #Number conference and print in list
- for div in divisions:
- print (str(y)+': '+div)
- y += 1
- self.division = 1
- self.fix = (raw_input('\nWould you like the table to be formatted for icons? \n(y/n): ').lower())
- def scrape(self):
- w = urllib2.urlopen('http://www.espn.com.au/nba/standings')
- soup = BeautifulSoup(w.read())
- standings_list = []
- final_list1 = []
- final_list2 = []
- #Find the division table
- if self.division == 1:
- for table in soup.findAll('table', summary="This shit works brah."):
- rawdata = table
- else:
- print ('An error has occured, please contact TeroTheTerror')
- #Make list of cells within a list of rows
- rawdata_list = [tr.findAll('td') for tr in rawdata.findAll('tr')]
- #Text only version of rawdata_list
- for row in rawdata_list:
- standings_list.append([cell.text for cell in row])
- #Get rid of problematic characters
- for lst in standings_list:
- final_list1.append([val.replace(u'\xa0', u' ') for val in lst])
- for lst in final_list1:
- final_list2.append([val.replace(u'\xe9', u'e') for val in lst])
- w.close()
- return final_list2
- def generate_tables(self, lst):
- #Time/Date stamp
- updated = datetime.datetime.now().strftime('%b %d, %Y at %I:%M%p')
- if self.division == 1:
- #Compiling the table
- standings = "Last Updated: " + updated + "\n"
- standings += "\n|Rank|Team|W|L|%|GB|"
- standings += "\n|:--:|:--:|:--:|:--:|:--:|:--:|:--:|"
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- 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])
- return standings
- def create_sidebar(self):
- #To fix character glitch when grabbing the sidebar
- h = HTMLParser.HTMLParser()
- #Initialize PRAW and login
- r = praw.Reddit(user_agent='HocketBotS v1.1 by TeroTheTerror')
- r.login(self.username,self.password)
- #Grab the sidebar template from the wiki
- sidebar = r.get_subreddit(self.subreddit).get_wiki_page('edit_sidebar').content_md
- #Create list from sidebar by splitting at ***
- sidebar_list = sidebar.split('***')
- #Sidebar with updated tables - +lucky_guess+sidebar_list[6]
- sidebar = (sidebar_list[0]+standings_a+sidebar_list[2])
- #Fix characters in sidebar
- sidebar = h.unescape(sidebar)
- return sidebar
- def update_reddit(self):
- #Initialize PRAW and login
- r = praw.Reddit(user_agent='HocketBotS v1.1 by TeroTheTerror')
- r.login(self.username,self.password)
- #Grab the current settings
- settings = r.get_subreddit(self.subreddit).get_settings()
- #Update the sidebar
- settings['description'] = sidebar
- settings = r.get_subreddit(self.subreddit).update_settings(description=settings['description'])
- hbs = Hockey_Bot_S()
- while(True):
- print ('Scraping Standings...')
- final_list = hbs.scrape()
- print ('Generating Table...')
- standings_b = hbs.generate_tables(final_list)
- print ('Fixing Table...')
- standings_a = hbs.fix_standings(standings_b)
- print ('Grabbing Sidebar Template...')
- sidebar = hbs.create_sidebar()
- print ('Updating Sidebar...')
- hbs.update_reddit()
- print ('Sidebar Updated: '+datetime.datetime.now().strftime('%b %d, %Y at %I:%M%p'))
- print ('Sleeping... \n')
- time.sleep(600)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement