My first plugin

Some help please...
Hi,

I wanted to write a Plugin so i gave it a shot. It's a plugin for the website [22tracks.com](http://22tracks.com). This is what i've got so far:


<br />
MUSIC_PREFIX = "/music/22tracks"<br />
NAME = L('Title')<br />
ART  = 'art-default.jpg'<br />
ICON = 'icon-default.png'<br />
<br />
####################################################################################################<br />
<br />
def Start():<br />
<br />
    Plugin.AddPrefixHandler(MUSIC_PREFIX, MusicMainMenu, NAME, ICON, ART)<br />
    Plugin.AddViewGroup("InfoList", viewMode="InfoList", mediaType="items")<br />
    Plugin.AddViewGroup("List", viewMode="List", mediaType="items")<br />
    MediaContainer.title1 = NAME<br />
    MediaContainer.viewGroup = "List"<br />
    MediaContainer.art = R(ART)<br />
    DirectoryItem.thumb = R(ICON)<br />
    VideoItem.thumb = R(ICON)<br />
    HTTP.CacheTime = CACHE_1HOUR<br />
    <br />
    <br />
def MusicMainMenu():<br />
<br />
    dir = MediaContainer(viewGroup="InfoList")<br />
    dir.Append(<br />
        Function(<br />
            DirectoryItem(<br />
                AmsMenu,<br />
                "Amsterdam",<br />
                subtitle="subtitle",<br />
                summary="Playlists selected by specialized DJ's from Amsterdam",<br />
                thumb=R(ICON),<br />
                art=R(ART)<br />
            )<br />
        )<br />
    )<br />
    return dir<br />
<br />
<br />
def AmsMenu(sender):<br />
    dir = MediaContainer(viewGroup="InfoList")<br />
    for item in HTML.ElementFromURL("http://22tracks.com/ams#pop", errors='ignore').xpath('//ul[@class="genre-nav"]/li'):<br />
        dir.Append(<br />
            Function(<br />
                DirectoryItem(<br />
                    CallbackExample,<br />
                    "1234",<br />
                    subtitle="subtitle",<br />
                    summary="clicking on me will call CallbackExample",<br />
                    thumb=R(ICON),<br />
                    art=R(ART)<br />
                )<br />
            )<br />
        )<br />
    return dir<br />
    <br />
    <br />
def CallbackExample(sender):<br />
<br />
    return MessageContainer(<br />
        "Not implemented",<br />
        "In real life, you'll make more than one callback,
and you'll do something useful.
sender.itemTitle=%s" % sender.itemTitle<br />
    )<br />




Now the problem is when i go to the website 22tracks.com and take a look at the source, there are no
  • 's in the
      .
      When I use firebug to view HTML there I see all the
    • 's I need to find for generating the dynamic AmsMenu list.

      So is xpath just not possible for this website?

      Thanks!
  • You may need to find another way or a different coats expression. It sounds like the page is loading dynamically using JavaScript or something. The plugin framework only has access to the page source not the dynamically loaded portion. Using an add-on like HTTP-Fox you can watch the traffic a the page loads. Sometimes it will expose a hidden API or other URL that you can use to get the data you want.

    Thanks, that helped me a lot!



    Code i’ve got so far:


    
    <br />
    MUSIC_PREFIX = "/music/22tracks"<br />
    NAME = L('Title')<br />
    ART  = 'art-default.jpg'<br />
    ICON = 'icon-default.jpg'<br />
    <br />
    WEBSITE = 'http://22tracks.com'<br />
    SLUG = '/ams'<br />
    GENRE = '#disco'<br />
    <br />
    ####################################################################################################<br />
    <br />
    def Start():<br />
    <br />
        Plugin.AddPrefixHandler(MUSIC_PREFIX, MusicMainMenu, NAME, ICON, ART)<br />
        Plugin.AddViewGroup("InfoList", viewMode="InfoList", mediaType="items")<br />
        Plugin.AddViewGroup("List", viewMode="List", mediaType="items")<br />
        MediaContainer.title1 = NAME<br />
        MediaContainer.viewGroup = "List"<br />
        MediaContainer.art = R(ART)<br />
        DirectoryItem.thumb = R(ICON)<br />
        VideoItem.thumb = R(ICON)<br />
        HTTP.CacheTime = CACHE_1HOUR<br />
        <br />
        <br />
    def MusicMainMenu():<br />
        dir = MediaContainer(viewGroup="InfoList")<br />
        <br />
        city = 'Amsterdam'<br />
        dir.Append(<br />
            Function(<br />
                DirectoryItem(<br />
                    GenreMenu,<br />
                    city,<br />
                    summary="Playlists selected by specialized DJ's from %s" % city,<br />
                    thumb=R(ICON),<br />
                    art=R(ART)<br />
                ),<br />
                city_id = 1,<br />
                city_slug = "ams"<br />
            )<br />
        )<br />
        <br />
        city = 'Brussels'<br />
        dir.Append(<br />
            Function(<br />
                DirectoryItem(<br />
                    GenreMenu,<br />
                    city,<br />
                    summary="Playlists selected by specialized DJ's from %s" % city,<br />
                    thumb=R(ICON),<br />
                    art=R(ART)<br />
                ),<br />
                city_id = 2,<br />
                city_slug = "bru"<br />
            )<br />
        )<br />
        <br />
        city = 'London'<br />
        dir.Append(<br />
            Function(<br />
                DirectoryItem(<br />
                    GenreMenu,<br />
                    city,<br />
                    summary="Playlists selected by specialized DJ's from %s" % city,<br />
                    thumb=R(ICON),<br />
                    art=R(ART)<br />
                ),<br />
                city_id = 3,<br />
                city_slug = "ldn"<br />
            )<br />
        )<br />
        <br />
        return dir<br />
    <br />
    <br />
    def GenreMenu(sender, city_id, city_slug):<br />
        genre_api = "%s/api/genres/%s" % (WEBSITE, city_id)<br />
        genres = JSON.ObjectFromURL(genre_api)<br />
        <br />
        dir = MediaContainer(viewGroup="InfoList")<br />
        for genre in genres:<br />
            dir.Append(<br />
                Function(<br />
                    DirectoryItem(<br />
                        TrackMenu,<br />
                        genre['title'],<br />
                        summary=genre['description_plain'],<br />
                        thumb="%s/%s" % (WEBSITE, genre['photo_path']),<br />
                        art=R(ART)<br />
                    ),<br />
                    city_id = city_id,<br />
                    city_slug = city_slug,<br />
                    genre_id = genre['id'],<br />
                    genre_slug = genre['slug']<br />
                )<br />
            )<br />
        return dir<br />
    <br />
    <br />
    def TrackMenu(sender, city_id, city_slug, genre_id, genre_slug):<br />
        track_api = "%s/api/tracks/%s" % (WEBSITE, genre_id)<br />
        tracks = JSON.ObjectFromURL(track_api)<br />
        <br />
        dir = MediaContainer(viewGroup="InfoList")<br />
        for track in tracks:<br />
            track_url = genre_api = "%s/%s#%s/%s" % (WEBSITE, city_slug, genre_slug, track['id'])<br />
            dir.Append(<br />
                Function(<br />
                    DirectoryItem(<br />
                        TracksMenu,<br />
                        "%s - %s" % (track['artist'], track['title']),<br />
                        summary=track['bio'],<br />
                        thumb=R(ICON),<br />
                        art=R(ART)<br />
                    ),<br />
                    track_url = track_url<br />
                )<br />
            )<br />
        return dir<br />
            <br />
            <br />
    def TracksMenu(sender, track_url):<br />
        return MessageContainer(<br />
            "Not implemented",<br />
            track_url<br />
        )<br />
    
    
    


    Can someone point me in the right direction on how to actually play the audio?
    I attached the .bundle...

    22tracks.bundle.zip (216 KB)


    I didn't do any audio plugins using the 2.0 version of the plugin framework, but it's fairly straightforward using v2.1. Start by upgrading you plugin code guided by the docs at dev.plexapp.com/docs.
    The new Freakonomics plugin is a fairly simple example of a v2.1 audio plugin. The hidden mechanics of how the audio track is handed off to Plex are in the URL Service which is located in the Services.bundle/Contents/Service Sets/com.plexapp.plugins.freakonomics which is located in the Plugins folder.

    This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.