#!/usr/bin/env python
"""
Author: Justin R. Andrusk <jandrusk@gmail.com>
"""
import sgmllib
class MyParser(sgmllib.SGMLParser):
"A simple parser class."
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
"Initialise an object, passing 'verbose' to the superclass."
sgmllib.SGMLParser.__init__(self, verbose)
self.hyperlinks = []
def start_a(self, attributes):
"Process a hyperlink and its 'attributes'."
for name, value in attributes:
if name == "href":
self.hyperlinks.append(value)
def get_hyperlinks(self):
"Return the list of hyperlinks."
return self.hyperlinks
import urllib, sgmllib, random, string
baseurl = 'http://catb.org/jargon/html/'
c = random.choice('abcdefghij') # Choose a random element
suburl1 = baseurl + c.upper()
# print "suburl1 is " + suburl1
# Get something to work with.
f = urllib.urlopen(suburl1)
s = f.read()
# Try and process the page.
# The class should have been defined first, remember.
myparser = MyParser()
myparser.parse(s)
import re, os
import tweepy
CONSUMER_KEY = '***********'
CONSUMER_SECRET = '*********'
ACCESS_KEY = '****************'
ACCESS_SECRET = '***************'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
article = random.choice(myparser.get_hyperlinks())
if (re.search('[A-z]\.html', article)):
hackdict = suburl1 + '/' + article
term = article.split('.', 1)
api.update_status("Hacker Dictionary Term of the Day is " + str(term[0]) + ". #hackdict " + hackdict)