«
Previous
|
Next
»
Revision 429e8d81
Added by dsorber over 13 years ago
- ID 429e8d81790fdddcf3aa608fa4e4b20b7a4939dd
- Child 08b1cb51
| bashquote/bashquote | ||
|---|---|---|
|
#!/usr/bin/env python
|
||
|
|
||
|
import cPickle
|
||
|
import os
|
||
|
import random
|
||
|
import sys
|
||
|
|
||
|
BQ_DIR = '/Users/dsorber/.bashquote'
|
||
|
BQ_CACHE = '/Users/dsorber/.bashquote/bq_cache'
|
||
|
|
||
|
def main():
|
||
|
|
||
|
# Create the cache file, or open it if it already exists
|
||
|
if not os.path.exists(BQ_CACHE):
|
||
|
print 'bashquote cache does not exist!'
|
||
|
sys.exit()
|
||
|
else :
|
||
|
cache = open(BQ_CACHE, 'r')
|
||
|
quotes = cPickle.load(cache)
|
||
|
cache.close()
|
||
|
|
||
|
# Choose a random quote from the list
|
||
|
num_quotes = len(quotes)
|
||
|
if num_quotes == 1:
|
||
|
# Repopulate the list manually if there is only one quote remaining
|
||
|
print 'Low on quotes, grabbing some more...',
|
||
|
os.system('bashquote_populate')
|
||
|
print 'Done'
|
||
|
index = random.randint(0, num_quotes - 1)
|
||
|
quote = quotes.pop(index)
|
||
|
|
||
|
# Print it out
|
||
|
print 'bashquote #%s ------ %s' % (quote[0], quote[1])
|
||
|
print quote[2]
|
||
|
|
||
|
# Re-store the remaining quotes and exit
|
||
|
cache = open(BQ_CACHE, 'w')
|
||
|
cPickle.dump(quotes, cache, -1)
|
||
|
cache.close()
|
||
|
sys.exit()
|
||
|
|
||
|
if __name__=="__main__":
|
||
|
main()
|
||
| bashquote/bashquote_populate | ||
|---|---|---|
|
#!/usr/bin/env python
|
||
|
|
||
|
import cPickle
|
||
|
import os
|
||
|
import re
|
||
|
import sys
|
||
|
import urllib
|
||
|
|
||
|
BQ_DIR = '/Users/dsorber/.bashquote'
|
||
|
BQ_CACHE = '/Users/dsorber/.bashquote/bq_cache'
|
||
|
|
||
|
def main():
|
||
|
|
||
|
# Create a list to hold the quotes
|
||
|
quotes = []
|
||
|
|
||
|
# Create the bashquote directory if it does not exist
|
||
|
if not os.path.exists(BQ_DIR):
|
||
|
print 'Creating bashquote directory...',
|
||
|
os.mkdir(BQ_DIR)
|
||
|
print 'Done'
|
||
|
|
||
|
# Create the cache file, or open it if it already exists
|
||
|
if not os.path.exists(BQ_CACHE):
|
||
|
print 'Creating bashquote cache...',
|
||
|
cache = open(BQ_CACHE, 'w')
|
||
|
cache.close()
|
||
|
print 'Done'
|
||
|
else:
|
||
|
cache = open(BQ_CACHE, 'r')
|
||
|
quotes = cPickle.load(cache)
|
||
|
cache.close()
|
||
|
|
||
|
|
||
|
# Only grab fresh quotes if there are less than 50 in the cache
|
||
|
if not len(quotes) < 50:
|
||
|
print 'plenty left'
|
||
|
sys.exit()
|
||
|
|
||
|
# Goto to the random >0 bash quotes page
|
||
|
listpage = urllib.urlopen("http://www.bash.org/?random1")
|
||
|
listpagetext = listpage.read()
|
||
|
|
||
|
quote_regex = '<p class="quote".+<b>#(\d+)</b>.+</a>\((\d+)\)<a .+<p class="qt">([\s\S]+?)</p>'
|
||
|
|
||
|
quotes = []
|
||
|
for quote_match in re.finditer(quote_regex, listpagetext):
|
||
|
quotes.append((quote_match.group(1),
|
||
|
quote_match.group(2),
|
||
|
unhtmlify(quote_match.group(3))))
|
||
|
|
||
|
# Store the list of quotes in the cache file, using the highest protocol
|
||
|
cache = open(BQ_CACHE, 'w')
|
||
|
cPickle.dump(quotes, cache, -1)
|
||
|
cache.close()
|
||
|
sys.exit()
|
||
|
|
||
|
|
||
|
def unhtmlify(quote):
|
||
|
# Filter out that HTML bullshit
|
||
|
return quote.replace("<", "<").replace("\'<", "<")\
|
||
|
.replace(">", ">").replace(""", "\"")\
|
||
|
.replace(" ", " ").replace("\\'", "'")\
|
||
|
.replace("\\x92", "'").replace("<br />\\r\\n', '", "\n")\
|
||
|
.replace("<br />\\r\\n\", '", "\n").replace("&", "&")\
|
||
|
.replace("<br />\\r\\n', \"", "\n")\
|
||
|
.replace("<br />\\r\\n\", \"", "\n").replace("<br />", "")
|
||
|
|
||
|
|
||
|
if __name__=="__main__":
|
||
|
main()
|
||
Adding some old code starting with bashquote.