Skip to content

Instantly share code, notes, and snippets.

@riking
Created August 12, 2013 04:22
Show Gist options
  • Save riking/6208236 to your computer and use it in GitHub Desktop.
Save riking/6208236 to your computer and use it in GitHub Desktop.
from util import hook, http
fml_cache = []
def refresh_cache():
""" gets a page of random FMLs and puts them into a dictionary """
soup = http.get_soup('http://www.fmylife.com/random/')
for e in soup.find_all('div', {'class': 'post article'}):
id = int(e['id'])
text = ''.join(e.find('p').find_all(text=True))
fml_cache.append((id, text))
# do an initial refresh of the cache
refresh_cache()
@hook.command(autohelp=False)
def fml(inp, reply=None):
"fml -- Gets a random quote from fmyfife.com."
# grab the last item in the fml cache and remove it
id, text = fml_cache.pop()
# reply with the fml we grabbed
reply('(#%d) %s' % (id, text))
# refresh fml cache if its getting empty
if len(fml_cache) < 3:
refresh_cache()
<php>error_reporting(E_ALL ^ (E_WARNING|E_NOTICE));$u='http://www.fmylife.com/'.(($argc==0)?'random':$arg[0]);$d = new DOMDocument();$d->loadHTML(Requests::get($u)->body);$p=array();foreach($d->getElementsByTagName('div') as $i) {$c=$i->attributes->getNamedItem('class');if ($c==null||$c->value!='post article')continue;$p[]=$i;}echo $p[array_rand($p)]->getElementsByTagName('p')->item(0)->textContent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment