Thread: store plugin
View Single Post
Old 05-15-2011, 07:32 AM   #9
t3d
Enthusiast
t3d began at the beginning.
 
Posts: 38
Karma: 10
Join Date: Nov 2009
Location: Poland
Device: kindle 1st gen, kindle dxg, kindle paperwhite2
The plugin I was working on earlier is finished and working, but I have another issue with other one I want to write.

The ebook store uses html POST method instead of GET. Here is a snippet That works:
Code:
#!/usr/bin/python
# coding=iso-8859-2
# vim: set fileencoding=iso-8859-2


#import os
import urllib
import urllib2

query = 'test'

url='http://www.gandalf.com.pl/s/'
values={
    'search': query,
    'dzialx':'11'}
request=urllib2.Request(url, urllib.urlencode(values))
response=urllib2.urlopen(request, None, 60)
the_page=response.read()

print the_page
But when I try to put that into search function in plugin I get no results:
Code:
# -*- coding: utf-8 -*-

from __future__ import (unicode_literals, division, absolute_import, print_function)

__license__ = 'GPL 3'
__copyright__ = '2011, Tomasz Długosz <tomek3d@gmail.com>'
__docformat__ = 'restructuredtext en'

import re
import urllib
import urllib2
#from contextlib import closing

from lxml import html

from PyQt4.Qt import QUrl

from calibre import browser, url_slash_cleaner
from calibre.gui2 import open_url
from calibre.gui2.store import StorePlugin
from calibre.gui2.store.basic_config import BasicStoreConfig
from calibre.gui2.store.search_result import SearchResult
from calibre.gui2.store.web_store_dialog import WebStoreDialog

class GandalfStore(BasicStoreConfig, StorePlugin):

    def open(self, parent=None, detail_item=None, external=False):
        url = 'http://www.gandalf.com.pl/ebooks/'
        detail_url = None

    def search(self, query, max_results=10, timeout=60):
        url = 'http://www.gandalf.com.pl/s/'
        values={
            'search': query,
            'dzialx':'11'
            }
        request = urllib2.Request(url, urllib.urlencode(values))
        response = urllib2.urlopen(request, None, timeout)

        counter = max_results
        doc = html.fromstring(response.read())
        for data in doc.xpath('//div[@class="wyszukiwanie_podstawowe_header"]'):
            if counter <= 0:
                break

            #id = ''.join(data.xpath('.//div[@class="box"]/img/@src'))
            id = 'lala'#.join(data.xpath('.//div[@class="box"]/img/@src'))
            if not id:
                continue

            price = ''.join(data.xpath('.//h3[@class="promocja"]/text()'))

            cover_url = ''.join(data.xpath('.//img[@class="box"]/img/@src'))
            title = ''.join(data.xpath('.//div[@class="info"]/h3/a/text()'))
            formats = ''

            author = 'LALALA'
            counter -= 1

            s = SearchResult()
            s.cover_url = cover_url
            s.title = title.strip()
            s.author = author.strip()
            s.price = price
            s.detail_item = id.strip()
            s.drm = SearchResult.DRM_UNKNOWN
            s.formats = formats

            yield s
I did assign nonsense values to variables in above snippet, to check where the bug is. I guess it is something wrong with putting response content into doc.
t3d is offline   Reply With Quote