<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A tv show module
"""
import sys
import re
import urllib2
import time
import urllib

progname = 'tv'

monthName = {
    "Jan" : '01',
    "Feb" : '02',
    "Mar" : '03',
    "Apr" : '04',
    "May" : '05',
    "Jun" : '06',
    "Jul" : '07',
    "Aug" : '08',
    "Sep" : '09',
    "Oct" : '10',
    "Nov" : '11',
    "Dec" : '12'
}

def handleGenres(str):
    genres = {}
    genres = str.split(" | ")
    genres = ", ".join(genres)
    return genres

def handleData(str):
    data = {}

    for line in str.split("\n"):
        result = re.search("([^@]+)@(.*)", line)

        if result:
            key = result.group(1)
            value = result.group(2)
            data[key] = value

    return data


def handleDate(str):
    if str != "":
        result = re.search("([^/]+)/([^/]+)/([^/]+)", str)

        if result:
            month = result.group(1) or None
            day = result.group(2) or None
            year = result.group(3) or None

            if month and day and year:
                return "%s.%s.%s" % (day, monthName[month], year[-2:])

        result = re.search("([^/]+)/([^/]+)", str)

        if result:
            month = result.group(1)
            year = result.group(2)

            if month and year:
                return "%s.%s.%s" % ("??", monthName[month], year[-2:])

            return str
    else:
        return "?"

def handleEpisode(str):
    result = re.search("([^^]+)\^([^^]+)\^([^^]+)", str)

    if result:
        num = result.group(1)
        name = result.group(2)
        date = result.group(3)

        return "%s %s" % (num, handleDate(date))

def getETA(timestamp):
    diff = int(timestamp) - int(time.time())

    minutes, seconds = divmod(diff, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    weeks, days = divmod(days, 7)

    output = ""

    if weeks:
        output = str(weeks) + "w "
    if days:
        output = output + str(days) + "d "
    if hours:
        output = output + str(hours) + "h "
    if minutes:
        output = output + str(minutes) + "m "
    if seconds:
        output = output + str(seconds) + "s "

    if output:
        return output

    return "In the future!"

def out(data):
    output = data.get("Show Name")

    if data.get("Started") != None and data.get("Ended") != None:
        output = output + " (%s till %s)" % (handleDate(data["Started"]), handleDate(data["Ended"]))
    if data.get("Status"):
        output = output + " " + data["Status"]
    if data.get("Genres"):
        output = output + " // " + handleGenres(data["Genres"])
    if data.get("Latest Episode"):
        output = output + " | Latest: " + handleEpisode(data["Latest Episode"])
    #if data["Next Episode"]:
    if data.get("Next Episode"):
        output = output + " | Next: " + handleEpisode(data["Next Episode"])
        if data.get("RFC3339"):
            output = output + " (ETA: " + getETA(data.get('GMT+0 NODST')) + ")"
    if data["Show URL"]:
        output = output + " | " + data.get("Show URL")
    return output

def main(args):
    """The program entry point."""

    if len(args) <= 0:
        # Roll a six-sided dice
        print 'Usage:'
        print '       !tv game of thrones\n'
        print 'Returns information about game of thrones'
        return

    cmd = " ".join(args[0:])

    data = None

    url = "http://services.tvrage.com/tools/quickinfo.php?show=" + cmd
    url = urllib.urlencode(url)

    try:
        open = urllib2.urlopen(url)
    except urllib2.URLError as e:
        if hasattr(e, "reason"):
            print "We failed to reach a server. Reason: " + e.reason
            return
        elif hasattr(e, "code"):
            print "The server could not fulfill the request. Error code: " + e.code
            return
    else:
        data = open.read()
        open.close()

        if "No Show Results" in data:
            print "No such show!"
            return
        else:
            output = out(handleData(data))
            if output:
                print output
                return
            return


if __name__ == '__main__':
    main(sys.argv[1:])
<commit_msg>changed to use quote_plus<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A tv show module
"""
import sys
import re
import urllib2
import time
import urllib

progname = 'tv'

monthName = {
    "Jan" : '01',
    "Feb" : '02',
    "Mar" : '03',
    "Apr" : '04',
    "May" : '05',
    "Jun" : '06',
    "Jul" : '07',
    "Aug" : '08',
    "Sep" : '09',
    "Oct" : '10',
    "Nov" : '11',
    "Dec" : '12'
}

def handleGenres(str):
    genres = {}
    genres = str.split(" | ")
    genres = ", ".join(genres)
    return genres

def handleData(str):
    data = {}

    for line in str.split("\n"):
        result = re.search("([^@]+)@(.*)", line)

        if result:
            key = result.group(1)
            value = result.group(2)
            data[key] = value

    return data


def handleDate(str):
    if str != "":
        result = re.search("([^/]+)/([^/]+)/([^/]+)", str)

        if result:
            month = result.group(1) or None
            day = result.group(2) or None
            year = result.group(3) or None

            if month and day and year:
                return "%s.%s.%s" % (day, monthName[month], year[-2:])

        result = re.search("([^/]+)/([^/]+)", str)

        if result:
            month = result.group(1)
            year = result.group(2)

            if month and year:
                return "%s.%s.%s" % ("??", monthName[month], year[-2:])

            return str
    else:
        return "?"

def handleEpisode(str):
    result = re.search("([^^]+)\^([^^]+)\^([^^]+)", str)

    if result:
        num = result.group(1)
        name = result.group(2)
        date = result.group(3)

        return "%s %s" % (num, handleDate(date))

def getETA(timestamp):
    diff = int(timestamp) - int(time.time())

    minutes, seconds = divmod(diff, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    weeks, days = divmod(days, 7)

    output = ""

    if weeks:
        output = str(weeks) + "w "
    if days:
        output = output + str(days) + "d "
    if hours:
        output = output + str(hours) + "h "
    if minutes:
        output = output + str(minutes) + "m "
    if seconds:
        output = output + str(seconds) + "s "

    if output:
        return output

    return "In the future!"

def out(data):
    output = data.get("Show Name")

    if data.get("Started") != None and data.get("Ended") != None:
        output = output + " (%s till %s)" % (handleDate(data["Started"]), handleDate(data["Ended"]))
    if data.get("Status"):
        output = output + " " + data["Status"]
    if data.get("Genres"):
        output = output + " // " + handleGenres(data["Genres"])
    if data.get("Latest Episode"):
        output = output + " | Latest: " + handleEpisode(data["Latest Episode"])
    #if data["Next Episode"]:
    if data.get("Next Episode"):
        output = output + " | Next: " + handleEpisode(data["Next Episode"])
        if data.get("RFC3339"):
            output = output + " (ETA: " + getETA(data.get('GMT+0 NODST')) + ")"
    if data["Show URL"]:
        output = output + " | " + data.get("Show URL")
    return output

def main(args):
    """The program entry point."""

    if len(args) <= 0:
        # Roll a six-sided dice
        print 'Usage:'
        print '       !tv game of thrones\n'
        print 'Returns information about game of thrones'
        return

    cmd = " ".join(args[0:])

    data = None

    url = "http://services.tvrage.com/tools/quickinfo.php?show=" + cmd
    url = urllib.quote_plus(url)

    try:
        open = urllib2.urlopen(url)
    except urllib2.URLError as e:
        if hasattr(e, "reason"):
            print "We failed to reach a server. Reason: " + e.reason
            return
        elif hasattr(e, "code"):
            print "The server could not fulfill the request. Error code: " + e.code
            return
    else:
        data = open.read()
        open.close()

        if "No Show Results" in data:
            print "No such show!"
            return
        else:
            output = out(handleData(data))
            if output:
                print output
                return
            return


if __name__ == '__main__':
    main(sys.argv[1:])

{'fraction_non_alphanumeric': 0.16862048549437536, 'fraction_numerical': 0.013025458851391355, 'mean_word_length': 1.9084022038567494, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2426361', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2764, 'n_words': 737}