<commit_before>###
# Copyright (c) 2012, spline
# All rights reserved.
#
#
###

import os
import sqlite3
import urllib2
from BeautifulSoup import BeautifulSoup
import string

import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring

_ = PluginInternationalization('CFB')

@internationalizeDocstring
class CFB(callbacks.Plugin):
    """Add the help for "@plugin help CFB" here
    This should describe *how* to use this plugin."""
    threaded = True
    
    def _translateTeam(self, db, column, optteam):
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
            
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        query = "select %s from cfb where %s='%s'" % (db, column, optteam)
        cursor.execute(query)
        row = cursor.fetchone()

        cursor.close()            

        return (str(row[0]))
        
    def _validteams(self, conf=None):
        """Returns a list of valid teams for input verification."""
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
            
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        cursor.execute("select team from cfb where conf=?", (conf,))        
        teamlist = []
        
        for row in cursor.fetchall():
            teamlist.append(str(row[0]))

        cursor.close()
        
        return teamlist

    def _validconfs(self, optlong=None):
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
        
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
    
        if optlong:
            cursor.execute("select full from confs")        
        else:
            cursor.execute("select short from confs")        
    
        teamlist = []
        
        for row in cursor.fetchall():
            teamlist.append(str(row[0]))

        cursor.close()
        
        return teamlist
    
    def _translateConf(self, optconf):
        # conf is validated previously, so no error checking.
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
        
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        cursor.execute("select full from confs where short=?", (optconf,))
        row = cursor.fetchone()
        cursor.close()            

        return (str(row[0])) 
            
    def cfbconferences(self, irc, msg, args):
        """ .  """
        
        conferences = self._validconfs()
        irc.reply("Valid conferences are: %s" % (string.join([ircutils.bold(item) for item in conferences], " | ")))
        
    cfbconferences = wrap(cfbconferences)

    def cfbteams(self, irc, msg, args, optconf):
        """[conference]
        Display valid teams in a specific conference. 
        """
        
        if optconf not in self._validconfs():
            irc.reply("Invalid conf. Must be one of: %s" % self._validconfs())
            return
        
        fullconf = self._translateConf(optconf)
        
        teams = self._validteams(conf=fullconf)
                
        irc.reply("Valid teams are: %s" % (string.join([ircutils.bold(item) for item in teams], " | ")))
        
    cfbteams = wrap(cfbteams, [('somethingWithoutSpaces')])

Class = CFB


# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=250:
<commit_msg>Add in lookupteam and cfbschedules.<commit_after>###
# Copyright (c) 2012, spline
# All rights reserved.
#
#
###

import os
import sqlite3
import urllib2
from BeautifulSoup import BeautifulSoup
import string
import re

import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring

_ = PluginInternationalization('CFB')

@internationalizeDocstring
class CFB(callbacks.Plugin):
    """Add the help for "@plugin help CFB" here
    This should describe *how* to use this plugin."""
    threaded = True
    
    def _translateTeam(self, db, column, optteam):
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
            
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        query = "select %s from cfb where %s='%s'" % (db, column, optteam)
        cursor.execute(query)
        row = cursor.fetchone()

        cursor.close()            

        return (str(row[0]))
        
    def _validteams(self, conf=None):
        """Returns a list of valid teams for input verification."""
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
            
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        cursor.execute("select team from cfb where conf=?", (conf,))        
        teamlist = []
        
        for row in cursor.fetchall():
            teamlist.append(str(row[0]))

        cursor.close()
        
        return teamlist

    def _validconfs(self, optlong=None):
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
        
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
    
        if optlong:
            cursor.execute("select full from confs")        
        else:
            cursor.execute("select short from confs")        
    
        teamlist = []
        
        for row in cursor.fetchall():
            teamlist.append(str(row[0]))

        cursor.close()
        
        return teamlist
    
    def _translateConf(self, optconf):
        # conf is validated previously, so no error checking.
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
        
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        cursor.execute("select full from confs where short=?", (optconf,))
        row = cursor.fetchone()
        cursor.close()            

        return (str(row[0])) 
            
    def cfbconferences(self, irc, msg, args):
        """ .  """
        
        conferences = self._validconfs()
        irc.reply("Valid conferences are: %s" % (string.join([ircutils.bold(item) for item in conferences], " | ")))
        
    cfbconferences = wrap(cfbconferences)

    def cfbteams(self, irc, msg, args, optconf):
        """[conference]
        Display valid teams in a specific conference. 
        """
        
        if optconf not in self._validconfs():
            irc.reply("Invalid conf. Must be one of: %s" % self._validconfs())
            return
        
        fullconf = self._translateConf(optconf)
        
        teams = self._validteams(conf=fullconf)
                
        irc.reply("Valid teams are: %s" % (string.join([ircutils.bold(item) for item in teams], " | ")))
        
    cfbteams = wrap(cfbteams, [('somethingWithoutSpaces')])

    def _lookupTeam(self, optteam):
        db_filename = self.registryValue('dbLocation')
        
        if not os.path.exists(db_filename):
            self.log.error("ERROR: I could not find: %s" % db_filename)
            return
            
        conn = sqlite3.connect(db_filename)
        cursor = conn.cursor()
        query = "select tid from cfb where team LIKE ?"
        cursor.execute(query, (optteam,))
        row = cursor.fetchone()
        
        if row is None:
            conn.close()
            return "0"
        else:
            conn.close()
            return (str(row[0]))
    
    def cfbschedule(self, irc, msg, args, optteam):
        """[team]
        Display the schedule/results for team.
        """
        
        lookupteam = self._lookupTeam(optteam)
        
        if lookupteam == "0":
            irc.reply("I could not find a schedule for: %s" % optteam)
            return
        
        url = 'http://www.cbssports.com/collegefootball/teams/schedule/%s/' % lookupteam

        req = urllib2.Request(url)
        html = (urllib2.urlopen(req)).read()
        html = html.replace('&amp;','&').replace(';','')
    
        soup = BeautifulSoup(html)
        
        if soup.find('table', attrs={'class':'data stacked'}).find('tr', attrs={'class':'title'}).find('td'):
            title = soup.find('table', attrs={'class':'data stacked'}).find('tr', attrs={'class':'title'}).find('td')
        else:
            irc.reply("Something broke with schedules. Did formatting change?")
            return

        div = soup.find('div', attrs={'id':'layoutTeamsPage'}) # must use this div first since there is an identical table.
        table = div.find('table', attrs={'class':'data', 'width':'100%'})
        rows = table.findAll('tr', attrs={'class':re.compile('^row[1|2]')})

        append_list = []
        
        for row in rows:
            date = row.find('td')
            team = date.findNext('td').find('a')
            time = team.findNext('td')
            
            if team.text.startswith('@'): # underline home
                team = team.text
            else:
                team = ircutils.underline(team.text)
        
            if time.find('span'): # span has score time. empty otherwise.
                time = time.find('span').string
                append_list.append(date.text + " - " + ircutils.bold(team) + " (" + time + ")")
            else:
                time = time.string
                append_list.append(date.text + " - " + ircutils.bold(team))

        descstring = string.join([item for item in append_list], " | ")
        output = "{0} :: {1}".format(ircutils.bold(title.text), descstring)
        
        irc.reply(output)
        
    cfbschedule = wrap(cfbschedule, [('text')])

Class = CFB


# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=250:

{'fraction_non_alphanumeric': 0.11735537190082644, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 2.011891592920354, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, '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': '1390719', 'n_tokens_mistral': 3198, 'n_tokens_neox': 3113, 'n_words': 775}