Created
August 12, 2013 04:22
-
-
Save riking/6208236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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