<commit_before>#!/usr/bin/env python
"""
xkcd.py - XKCD Module
Copyright 2010, <NAME> (yanovich.net), and <NAME>
Copyright 2012, <NAME>
Licensed under the Eiffel Forum License 2.

More info:
* Jenni: https://github.com/myano/jenni/
* Phenny: http://inamidst.com/phenny/
"""

import random
from search import google_search
from url import find_title
import urllib2
from lxml import etree
import re

""".xkcd - Finds an xkcd comic strip. Takes one of 3 inputs:
If no input is provided it will return a random comic
If numeric input is provided it will return that comic
If non-numeric input is provided it will return the first google result for those keywords on the xkcd.com site"""
    
def xkcd(jenni, input):
    # get latest comic for rand function and numeric input
    body = urllib2.urlopen("http://xkcd.com/rss.xml").readlines()[1]
    parsed = etree.fromstring(body)
    newest = etree.tostring(parsed.findall("channel/item/link")[0])
    max_int = int(newest.split("/")[-3])

    # if no input is given (pre - lior's edits code)
    if not input.group(2): # get rand comic
        random.seed()
        website = "http://xkcd.com/%d/" % random.randint(0,max_int+1)
    else:
        query = input.group(2).strip()

        # numeric input! get that comic number if it exists
        if (query.isdigit()):
            if (int(query) > max_int):
                jenni.say("Sorry, comic #" + query + " hasn't been posted yet. The last comic was #%d" % max_int)
                return
            else: website = "http://xkcd.com/" + query
        
        # non-numeric input! code lifted from search.g
        else:
            if (query.lower() == "latest" or query.lower() == "newest"): # special commands
                website = "https://xkcd.com/"
            else: # just google
                try:
                    query = query.encode('utf-8')
                except:
                    pass
                website = google_search("site:xkcd.com "+ query)
                chkForum = re.match(re.compile(r'.*?([0-9].*?):.*'), find_title(website)) # regex for comic specific forum threads
                if (chkForum == True):
                    website = "http://xkcd.com/" + match.groups()[0].lstrip('0')
    if website: # format and say result
        website += ' [' + find_title(website)[6:] + ']'
        jenni.say(website)
    elif website is False: jenni.say("Problem getting data from Google.")
    else: jenni.say("No results found for '%s'." % query)
xkcd.commands = ['xkcd']
xkcd.priority = 'low'

if __name__ == '__main__':
    print __doc__.strip()<commit_msg>[xkcd] finally got everything working<commit_after>#!/usr/bin/env python
"""
xkcd.py - XKCD Module
Copyright 2010, <NAME> (yanovich.net), and <NAME>
Copyright 2012, <NAME>
Licensed under the Eiffel Forum License 2.

More info:
* Jenni: https://github.com/myano/jenni/
* Phenny: http://inamidst.com/phenny/
"""

import random
from search import google_search
from url import find_title
import urllib2
from lxml import etree
import re

""".xkcd - Finds an xkcd comic strip. Takes one of 3 inputs:
If no input is provided it will return a random comic
If numeric input is provided it will return that comic
If non-numeric input is provided it will return the first google result for those keywords on the xkcd.com site"""
    
def xkcd(jenni, input):
    # get latest comic for rand function and numeric input
    body = urllib2.urlopen("http://xkcd.com/rss.xml").readlines()[1]
    parsed = etree.fromstring(body)
    newest = etree.tostring(parsed.findall("channel/item/link")[0])
    max_int = int(newest.split("/")[-3])

    # if no input is given (pre - lior's edits code)
    if not input.group(2): # get rand comic
        random.seed()
        website = "http://xkcd.com/%d/" % random.randint(0,max_int+1)
    else:
        query = input.group(2).strip()

        # numeric input! get that comic number if it exists
        if (query.isdigit()):
            if (int(query) > max_int):
                jenni.say("Sorry, comic #" + query + " hasn't been posted yet. The last comic was #%d" % max_int)
                return
            else: website = "http://xkcd.com/" + query
        
        # non-numeric input! code lifted from search.g
        else:
            if (query.lower() == "latest" or query.lower() == "newest"): # special commands
                website = "https://xkcd.com/"
            else: # just google
                try:
                    query = query.encode('utf-8')
                except:
                    pass
                website = google_search("site:xkcd.com "+ query)
                chkForum = re.match(re.compile(r'.*?([0-9].*?):.*'), find_title(website)) # regex for comic specific forum threads
                if (chkForum):
                    website = "http://xkcd.com/" + chkForum.groups()[0].lstrip('0')
    if website: # format and say result
        website += ' [' + find_title(website)[6:] + ']'
        jenni.say(website)
    elif website is False: jenni.say("Problem getting data from Google.")
    else: jenni.say("No results found for '%s'." % query)
xkcd.commands = ['xkcd']
xkcd.priority = 'low'

if __name__ == '__main__':
    print __doc__.strip()
{'fraction_non_alphanumeric': 0.1319777479378477, 'fraction_numerical': 0.00959140610013428, 'mean_word_length': 2.4235062376887724, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2681454', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1618, 'n_words': 496}