⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio.py

📁 Yahoo!search API. 用于搜索引擎接口
💻 PY
📖 第 1 页 / 共 2 页
字号:
"""DOM parser for Audio search resultsImplement a simple DOM parser for the Yahoo Search Web Servicesaudio search APIs."""__revision__ = "$Id: audio.py,v 1.4 2007/02/28 05:20:11 zwoop Exp $"__version__ = "$Revision: 1.4 $"__author__ = "Leif Hedstrom <leif@ogre.com>"__date__ = "Tue Feb 27 14:19:41 MST 2007"from yahoo.search import domfrom yahoo.search import parser## Custom DOM parser for some Audio classes. This will handle the# "id" attribute of artists and songs etc. properly#class _AudioParser(dom.DOMResultParser):    """_AudioParser - Custom DOM parser for some Audio classes    """    def _tags_to_dict(self, node, tags, parse_id=True):        """This specialized version will convert the "id" attribute of        the tag to an attribute.        """        res = super(_AudioParser, self)._tags_to_dict(node, tags)        if parse_id:            attr = node.attributes.getNamedItem('id')            if attr:                res['Id'] = str(attr.nodeValue)            else:                raise parser.XMLError("Result has no id attr")        return res## Album Search DOM parser#class AlbumSearch(_AudioParser):    """AlbumSearch - DOM parser for Album Search    Each result is a dictionary populated with the extracted data from the    XML results. The following keys are always available:        Title          - The title of the album.        Artist         - The performer of the album, and the unique ID        Publisher      - The publisher of the album.        ReleaseDate    - The date of the album's release.        Id             - Internal ID of the album, unique identifier.    The following attributes are optional, and might not be set:        Tracks         - Number of tracks on the album.        Thumbnail      - The URL of a thumbnail picture of the album cover.        RelatedAlbums  - Contains a list of related (similar) albums (IDs).    Thumbnail is in turn another dictionary, which will have the following    keys:        Url             - URL of the thumbnail.        Height          - Height of the thumbnail, in pixels (optional).        Width           - Width of the thumbnail, in pixels (optional).    The two attributes Artist and RelatedAlbums are both lists of    dictionaries, with the following two keys:        Name            - Textual "name" value.        Id              - Unique identifier (internal yahoo ID).    Example:        results = ws.parse_results()        for res in results:            print "%s - %s bytes" % (res.Artist.Name, res.Artist.Id)    """    def _init_res_fields(self):        """Initialize the valid result fields."""        super(AlbumSearch, self)._init_res_fields()        self._res_fields = [('Title', None, None),                            ('Publisher', None, None),                            ('ReleaseDate', None, None),                            ('Tracks', 0, int),                            ]    def _parse_result(self, result):        """Internal method to parse one Result node"""        res = super(AlbumSearch, self)._parse_result(result)        node = result.getElementsByTagName('Thumbnail')        if node:            res['Thumbnail'] = self._tags_to_dict(node[0], (('Url', None, None),                                                            ('Height', 0, int),                                                            ('Width', 0, int)),                                                  parse_id=False)        else:            res['Thumbnail'] = None        node = result.getElementsByTagName('Artist')        if node:            res['Artist'] = self._id_attribute_to_dict(node[0])        else:            res['Artist'] = None        node = result.getElementsByTagName('RelatedAlbums')        if node:            res['RelatedAlbums'] = self._parse_list_node(node[0], 'Album')        else:            res['RelatedAlbums'] = None        return res## Artist Search DOM parser#class ArtistSearch(_AudioParser):    """ArtistSearch - DOM parser for Artist Search    Each result is a dictionary populated with the extracted data from the    XML results. The following keys are always available:        Name           - The name of the artist.        Id             - Internal ID of the artist, unique identifier.    The following attributes are optional, and might not be set:        Thumbnail      - The URL of the thumbnail file.        RelatedArtists - Contains a list of related artists that fans of                         the artist in this Result might like.        PopularSongs   - Contains a list of popular songs by this artist.        YahooMusicPage - The URL to link to the artist's page on the                         Yahoo Music site. This can be empty!    Thumbnail is in turn another dictionary, which will have the following    keys:        Url             - URL of the thumbnail.        Height          - Height of the thumbnail, in pixels (optional).        Width           - Width of the thumbnail, in pixels (optional).   Both RelatedArtist and PopularSongs are lists of IDs, which can be   used as an identifier into subsequent Yahoo Audio search calls.    Example:        results = ws.parse_results()        for res in results:            print "%s - %s bytes" % (res.Name, res.YahooMusicPage)    """    def _init_res_fields(self):        """Initialize the valid result fields."""        super(ArtistSearch, self)._init_res_fields()        self._res_fields = [('Name', None, None),                            ('YahooMusicPage', "", None),                            ]    def _parse_result(self, result):        """Internal method to parse one Result node"""        res = super(ArtistSearch, self)._parse_result(result)        node = result.getElementsByTagName('Thumbnail')        if node:            res['Thumbnail'] = self._tags_to_dict(node[0], (('Url', None, None),                                                            ('Height', 0, int),                                                            ('Width', 0, int)),                                                  parse_id=False)        else:            res['Thumbnail'] = None        node = result.getElementsByTagName('RelatedArtists')        if node:            res['RelatedArtists'] = self._parse_list_node(node[0], 'Artist')        else:            res['RelatedArtists'] = None        node = result.getElementsByTagName('PopularSongs')        if node:            res['PopularSongs'] = self._parse_list_node(node[0], 'Song')        else:            res['PopularSongs'] = None        return res## Song Search DOM parser#class SongSearch(_AudioParser):    """SongSearch - DOM parser for Song Search    Each result is a dictionary populated with the extracted data from the    XML results. The following keys are always available:        Title          - The title of the song.        Id             - Internal ID of the song, unique identifier.        Album          - The album from which the song was taken, and ID.        Artist         - The performer of the album, and the unique ID.        Publisher      - The publisher of the album.        Length         - The length of the song in seconds.        ReleaseDate    - The date of the album's release.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -