daniel110

top100sub.py

May 4th, 2012
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  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()
Add Comment
Please, Sign In to add comment