Package lastfm :: Package mixins :: Module searchable

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.mixins.searchable

     1  #!/usr/bin/env python
     2
     3  __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
     4  __version__ = "0.2"
     5  __license__ = "GNU Lesser General Public License"
     6
     7  from lastfm.lazylist import lazylist
     8
     9 -class Searchable(object):
    10      @classmethod
    11 -    def search(cls,
    12                 api,
    13                 search_item,
    14                 limit = None,
    15                 **kwds):
    16          from lastfm.api import Api
    17          cls_name = cls.__name__.lower()
    18          params = {
    19                    'method': '%s.search'%cls_name,
    20                    cls_name: search_item
    21                    }
    22          for kwd in kwds:
    23              if kwds[kwd] is not None:
    24                  params[kwd] = kwds[kwd]
    25
    26          if limit:
    27              params.update({'limit': limit})
    28
    29          @lazylist
    30          def gen(lst):
    31              data = api._fetch_data(params).find('results')
    32              total_pages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
    33                              int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
    34
    35              @lazylist
    36              def gen2(lst, data):
    37                  for a in data.findall('%smatches/%s'%(cls_name, cls_name)):
    38                      yield cls._search_yield_func(api, a)
    39
    40              for a in gen2(data):
    41                  yield a
    42
    43              for page in xrange(2, total_pages+1):
    44                  params.update({'page': page})
    45                  data = api._fetch_data(params).find('results')
    46                  for a in gen2(data):
    47                      yield a
    48          return gen()
    49
    50      @staticmethod
    51 -    def _search_yield_func(api, search_term):
    52          pass
    53