Package lastfm :: Package mixins :: Module taggable

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.mixins.taggable

     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.base import LastfmBase
     8  from lastfm.safelist import SafeList
     9  from lastfm.decorators import cached_property, authenticate
    10
    11 -class Taggable(object):
    12 -    def init(self, api):
    13          self._api = api
    14
    15      @cached_property
    16      @authenticate
    17 -    def tags(self):
    18          from lastfm.tag import Tag
    19          params = self._default_params({'method': '%s.getTags' % self.__class__.__name__.lower()})
    20          data = self._api._fetch_data(params, sign = True, session = True, no_cache = True).find('tags')
    21          return SafeList([
    22                         Tag(
    23                             self._api,
    24                             name = t.findtext('name'),
    25                             url = t.findtext('url')
    26                             )
    27                         for t in data.findall('tag')
    28                         ],
    29                         self.add_tags, self.remove_tag)
    30
    31      @authenticate
    32 -    def add_tags(self, tags):
    33          from lastfm.tag import Tag
    34          while(len(tags) > 10):
    35                          section = tags[0:9]
    36                          tags = tags[9:]
    37                          self.add_tags(section)
    38
    39          if len(tags) == 0: return
    40
    41          tagnames = []
    42          for tag in tags:
    43              if isinstance(tag, Tag):
    44                  tagnames.append(tag.name)
    45              elif isinstance(tag, str):
    46                  tagnames.append(tag)
    47
    48          params = self._default_params({
    49              'method': '%s.addTags' % self.__class__.__name__.lower(),
    50              'tags': ",".join(tagnames)
    51              })
    52          self._api._post_data(params)
    53          self._tags = None
    54
    55      @authenticate
    56 -    def remove_tag(self, tag):
    57          from lastfm.tag import Tag
    58          if isinstance(tag, Tag):
    59              tag = tag.name
    60
    61          params = self._default_params({
    62              'method': '%s.removeTag' % self.__class__.__name__.lower(),
    63              'tag': tag
    64              })
    65          self._api._post_data(params)
    66          self._tags = None
    67
    68 -    def _default_params(self, extra_params = {}):
    69          return extra_params
    70