# Path: narwhal/core/torrent/models.py
# class Torrent(Model):
#     class Meta:
#         ordering = ('-added',)
#         app_label = 'torrent'
#         verbose_name =_('torrent')
#         verbose_name_plural =_('torrents')
# 
#     category = ForeignKey(Category, related_name='torrents')
#     title = CharField(_('Title'), max_length=80)
#     slug = SlugField(_('Slug'))
#     user = ForeignKey('auth.User', blank=True, null=True, verbose_name=_('Author'))
#     image = ImageField(_('Image'), upload_to='img/torrents', blank=True, null=True)
#     description = TextField(_('Description'))
#     added = DateTimeField(_('Added'), auto_now_add=True, editable=False)
#     
#     torrent = FileField(upload_to='torrent/')
#     data = JSONField(editable=False, default=lambda: {})
#     info_hash = CharField(_('Info hash'), unique=True, max_length=40, db_index=True, editable=False)
#     seeders = PositiveIntegerField(editable=False, default=0)
#     leechers = PositiveIntegerField(editable=False, default=0)
#     downloaded = PositiveIntegerField(editable=False, default=0)
#     comments_enabled = BooleanField(_('comments enabled'), default=True)
#     
#     tags = TaggableManager()
#     
#     def single_file(self):
#         print self.data
#         return 'length' in self.data['info'].keys()
#     single_file.boolean = True
# 
#     def files(self):
#         if self.single_file():
#             return [ self.data['info']['name'] ]
#         else:
#             return [ ( "/".join(f['path']), f['length']) for f in self.data['info']['files'] ]
# 
#     def num_files(self):
#         if self.single_file():
#             return 1
#         else:
#             return len(self.data['info']['files'])
# 
#     def size(self):
#         if 'length' in self.data['info'].keys():
#             return self.data['info']['length']
#         elif 'files' in self.data['info'].keys():
#             return reduce(lambda a,b:a+b, (length for file, length in self.data['info']['files'].items()) )
# 
#     def __unicode__(self):
#         return smart_unicode(self.title)
# 
#     def validate(self):
#         fkeys = self.data['info'].keys()
#         if not 'length' in fkeys and  not 'files' in fkeys:
#             raise ValidationError(_('Torrent File is not valid: does not contain any files.'))
#         #extra validation performed by subclasses
# 
#     def save(self, **kwargs):
#         #from markdown import Markdown
#         #md = Markdown(extensions=['footnotes'], safe_mode=True)
#         #self.html = md.convert(self.text)
#         if not self.slug:
#             self.slug = slugify(self.title)[:self._meta.get_field_by_name('slug')[0].max_length]
#         super(Torrent, self).save(**kwargs)


import time, sys, traceback
from optparse import make_option
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower
from threading import Thread, Event
from djboss.commands import *
from narwhal.core.torrent.models import Torrent
                from django.utils.text import smart_split


    def say_public(self, text):
        "Print TEXT into public channel, for all to see."
        for line in text.split("\n"):
            self.queue.send(line, self.channel)

    def say_private(self, nick, text):
        "Send private message of TEXT to NICK."
        for line in text.split("\n"):
            self.queue.send(line, nick)

    def reply(self, e, text):
        "Send TEXT to public channel or as private msg, in reply to event E."
        if e.eventtype() == "pubmsg":
            self.say_public("%s: %s" % (nm_to_n(e.source()), text))
        else:
            self.say_private(nm_to_n(e.source()), text)

    def do_command(self, e, c, cmd, from_nick):
        try:
            if cmd == 'echo':
                self.reply(e, "Wow, there is an echo here")
            
            if cmd.startswith('count'):
                ans =""
                words = cmd.split(" ")
                while True:
                    mcmd = words.pop(0)
                    model = words.pop(0)
                    if model=='torrents':

# Path: narwhal/core/torrent/models.py
# class Torrent(Model):
#     class Meta:
#         ordering = ('-added',)
#         app_label = 'torrent'
#         verbose_name =_('torrent')
#         verbose_name_plural =_('torrents')
# 
#     category = ForeignKey(Category, related_name='torrents')
#     title = CharField(_('Title'), max_length=80)
#     slug = SlugField(_('Slug'))
#     user = ForeignKey('auth.User', blank=True, null=True, verbose_name=_('Author'))
#     image = ImageField(_('Image'), upload_to='img/torrents', blank=True, null=True)
#     description = TextField(_('Description'))
#     added = DateTimeField(_('Added'), auto_now_add=True, editable=False)
#     
#     torrent = FileField(upload_to='torrent/')
#     data = JSONField(editable=False, default=lambda: {})
#     info_hash = CharField(_('Info hash'), unique=True, max_length=40, db_index=True, editable=False)
#     seeders = PositiveIntegerField(editable=False, default=0)
#     leechers = PositiveIntegerField(editable=False, default=0)
#     downloaded = PositiveIntegerField(editable=False, default=0)
#     comments_enabled = BooleanField(_('comments enabled'), default=True)
#     
#     tags = TaggableManager()
#     
#     def single_file(self):
#         print self.data
#         return 'length' in self.data['info'].keys()
#     single_file.boolean = True
# 
#     def files(self):
#         if self.single_file():
#             return [ self.data['info']['name'] ]
#         else:
#             return [ ( "/".join(f['path']), f['length']) for f in self.data['info']['files'] ]
# 
#     def num_files(self):
#         if self.single_file():
#             return 1
#         else:
#             return len(self.data['info']['files'])
# 
#     def size(self):
#         if 'length' in self.data['info'].keys():
#             return self.data['info']['length']
#         elif 'files' in self.data['info'].keys():
#             return reduce(lambda a,b:a+b, (length for file, length in self.data['info']['files'].items()) )
# 
#     def __unicode__(self):
#         return smart_unicode(self.title)
# 
#     def validate(self):
#         fkeys = self.data['info'].keys()
#         if not 'length' in fkeys and  not 'files' in fkeys:
#             raise ValidationError(_('Torrent File is not valid: does not contain any files.'))
#         #extra validation performed by subclasses
# 
#     def save(self, **kwargs):
#         #from markdown import Markdown
#         #md = Markdown(extensions=['footnotes'], safe_mode=True)
#         #self.html = md.convert(self.text)
#         if not self.slug:
#             self.slug = slugify(self.title)[:self._meta.get_field_by_name('slug')[0].max_length]
#         super(Torrent, self).save(**kwargs)


# Path: narwhal/plugins/irc/bot/commands.py
import time, sys, traceback
from optparse import make_option
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower
from threading import Thread, Event
from djboss.commands import *
from narwhal.core.torrent.models import Torrent
                from django.utils.text import smart_split


    def say_public(self, text):
        "Print TEXT into public channel, for all to see."
        for line in text.split("\n"):
            self.queue.send(line, self.channel)

    def say_private(self, nick, text):
        "Send private message of TEXT to NICK."
        for line in text.split("\n"):
            self.queue.send(line, nick)

    def reply(self, e, text):
        "Send TEXT to public channel or as private msg, in reply to event E."
        if e.eventtype() == "pubmsg":
            self.say_public("%s: %s" % (nm_to_n(e.source()), text))
        else:
            self.say_private(nm_to_n(e.source()), text)

    def do_command(self, e, c, cmd, from_nick):
        try:
            if cmd == 'echo':
                self.reply(e, "Wow, there is an echo here")
            
            if cmd.startswith('count'):
                ans =""
                words = cmd.split(" ")
                while True:
                    mcmd = words.pop(0)
                    model = words.pop(0)
                    if model=='torrents':

                        qs = Torrent.objects.all()
