# coding: utf-8

from collections import OrderedDict
from urlparse import urljoin
from urllib import urlencode
from scrapy.spider import Spider
from scrapy.selector import Selector
from scrapy.http import Request
from scrapy import log
from hashlib import md5
from ..items import Art, Page, Result


BASE_URL = 'http://www.toranoana.jp/'
QUERY_URL = 'http://www.toranoana.jp/cgi-bin/R2/allsearch.cgi'


class Tora(Spider):

    name = 'tora'

    def __init__(self, query, *args, **kargs):
        Spider.__init__(self, *args, **kargs)
        self.query = decode(query)

    @property
    def id(self):
        return md5(self.query.encode('utf-8')).hexdigest()

    @property
    def uri(self):
        return makeuri(self.query)

    @property
    def request(self):
        return Request(
            self.uri,
            cookies={'afg': '0'},
            headers={'Referer': QUERY_URL},
            callback=self.parse
        )

    def start_requests(self):
        try:
            return [self.request]
        except:
            log.msg('start requests failed', level=log.ERROR)
            return []

    def parse(self, response):
        def gen(trs):
            for tr in trs[2:-1:2]:
                yield Art(
                    title=tr.xpath('td[@class="c1"]/a/text()').extract()[0],
                    author=tr.xpath('td[@class="c2"]/a/text()').extract()[0],
                    company=tr.xpath('td[@class="c3"]/a/text()').extract()[0],
                    uri=urljoin(BASE_URL, tr.xpath('td[@class="c1"]/a/@href').extract()[0]),
                    status='reserve' if u'予' in tr.xpath('td[@class="c7"]/text()').extract() else 'other',
                )

        sel = Selector(response)

        try:
            trs = list(sel.xpath('//table[@class="FixFrame"]//tr'))
            return Page(uri=self.uri, total=total(sel), arts=list(gen(trs)))
        except:
            if empty(response):
                log.msg('empty result', level=log.INFO)
                return Page(uri=self.uri, total=0, arts=[])
            return Result(ok=False)


def total(sel):
    return int(sel.xpath('//table[@class="addrtbl"]//td[@class="DTW_td_l"]/span[2]/text()').re('\d+')[0])


def empty(response):
    return u'該当する商品が見つかりませんでした。' in response.body_as_unicode()


def decode(query):
    return query.decode('utf-8') if isinstance(query, str) else query


def makeuri(query, start=0):
    return QUERY_URL + '?' + urlencode(OrderedDict([
        ('item_kind', '0401'),
        ('bl_fg', '0'),
        ('search', query.encode('Shift_JIS')),
        ('ps', start + 1),
    ]))


def good(response):
    sel = Selector(response)
    try:
        total(sel)
        return True
    except:
        return empty(response)

@@ -1131,32 +1131,187 @@
 elf, response):%0A
+        if empty(response.body_as_unicode()):%0A            log.msg('empty result', level=log.INFO)%0A            return Page(uri=self.uri, total=0, arts=%5B%5D)%0A%0A
         def gen(
@@ -1312,24 +1312,24 @@
 f gen(trs):%0A
-
             
@@ -1864,17 +1864,16 @@
 sponse)%0A
-%0A
         
@@ -2042,44 +2042,8 @@
 pt:%0A
-            if empty(response):%0A    
     
@@ -2055,36 +2055,36 @@
    log.msg('
-empty result
+parse failed
 ', level=log
@@ -2088,72 +2088,13 @@
 log.
-INFO)%0A                return Page(uri=self.uri, total=0, arts=%5B%5D
+ERROR
 )%0A  
@@ -2263,24 +2263,23 @@
 f empty(
-response
+content
 ):%0A    r
@@ -2313,34 +2313,15 @@
  in 
-response.body_as_unicode()
+content
 %0A%0A%0Ad
@@ -2659,120 +2659,59 @@
     
-sel = Selector(response)%0A    try:%0A        total(sel)%0A        return True%0A    except:%0A        return empty(response
+return u'%E5%A4%A7%E5%A4%89%E6%B7%B7%E3%81%BF%E5%90%88%E3%81%A3%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99' not in response.body_as_unicode(
 )%0A

optimize error check