This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

top100sub.py

By: daniel110 on May 4th, 2012  |  syntax: Python  |  size: 1.37 KB  |  views: 443  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #tob100sub.py
  2.  
  3. """
  4. tob100sub is a program that gets a list of top 100 subreddit on reddit.com and
  5. opens the a random subreddit from the list on a new tab on your default browser.
  6. """
  7.  
  8. # python imports
  9. import random
  10. import urllib2
  11. import webbrowser
  12. import random
  13. import re
  14. from BeautifulSoup import BeautifulSoup
  15.  
  16.  
  17. site_name = 'http://www.redditlist.com/'
  18.  
  19. def get_content():
  20.     req = urllib2.Request(site_name)
  21.     site_data = urllib2.urlopen(req)
  22.     content = BeautifulSoup(site_data)
  23.     return content
  24.  
  25. def get_subreddits(site_content):
  26.     subreddits = []
  27.     table_reddits = site_content.findAll('tr', {'class': re.compile("halluc (?:bgcol)?")})
  28.     for i in range(100):
  29.         name = str(table_reddits[i].find('td', {'class': 'redditname'}).a.string)
  30.         subreddits.append(name)
  31.     return subreddits
  32.  
  33. def get_random(subreddits):
  34.     randvalue = random.randrange(100)
  35.     reddit_name =  subreddits[randvalue].lower()
  36.     return reddit_name
  37.  
  38. def open_page(reddit_name):
  39.     baseSite = "http://www.reddit.com/r/"
  40.     redditSite = baseSite + reddit_name
  41.     print "Opening: %s" % (redditSite)
  42.     webbrowser.open_new_tab(redditSite)
  43.  
  44. def main():
  45.     site_content = get_content()
  46.     subreddits = get_subreddits(site_content)
  47.     reddit_name = get_random(subreddits)
  48.     open_page(reddit_name)
  49.    
  50.  
  51. if __name__ == '__main__':
  52.     main()
clone this paste RAW Paste Data