jandrusk

Hacker Term of the Day

Apr 28th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Author: Justin R. Andrusk <jandrusk@gmail.com>
  5. """
  6.  
  7. import sgmllib
  8.  
  9. class MyParser(sgmllib.SGMLParser):
  10.     "A simple parser class."
  11.  
  12.     def parse(self, s):
  13.         "Parse the given string 's'."
  14.         self.feed(s)
  15.         self.close()
  16.  
  17.     def __init__(self, verbose=0):
  18.         "Initialise an object, passing 'verbose' to the superclass."
  19.  
  20.         sgmllib.SGMLParser.__init__(self, verbose)
  21.         self.hyperlinks = []
  22.  
  23.     def start_a(self, attributes):
  24.         "Process a hyperlink and its 'attributes'."
  25.  
  26.         for name, value in attributes:
  27.             if name == "href":
  28.                 self.hyperlinks.append(value)
  29.  
  30.     def get_hyperlinks(self):
  31.         "Return the list of hyperlinks."
  32.  
  33.         return self.hyperlinks
  34.  
  35. import urllib, sgmllib, random, string
  36. baseurl = 'http://catb.org/jargon/html/'
  37.  
  38. c = random.choice('abcdefghij')  # Choose a random element
  39. suburl1 = baseurl + c.upper()
  40. # print "suburl1 is " + suburl1
  41. # Get something to work with.
  42. f = urllib.urlopen(suburl1)
  43. s = f.read()
  44.  
  45. # Try and process the page.
  46. # The class should have been defined first, remember.
  47. myparser = MyParser()
  48. myparser.parse(s)
  49.  
  50. import re, os
  51. import tweepy
  52. CONSUMER_KEY = '***********'
  53. CONSUMER_SECRET = '*********'
  54. ACCESS_KEY = '****************'
  55. ACCESS_SECRET = '***************'
  56. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  57. auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
  58. api = tweepy.API(auth)
  59.  
  60. article = random.choice(myparser.get_hyperlinks())
  61.  
  62. if (re.search('[A-z]\.html', article)):
  63.     hackdict = suburl1 + '/' + article
  64.     term = article.split('.', 1)
  65.     api.update_status("Hacker Dictionary Term of the Day is " + str(term[0]) + ". #hackdict " + hackdict)
Add Comment
Please, Sign In to add comment