Advertisement
Guest User

addontry

a guest
Apr 18th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. # https://docs.python.org/2.7/
  2. import os
  3. import sys
  4. import urllib
  5. import urlparse
  6. # http://mirrors.kodi.tv/docs/python-docs/
  7. import xbmcaddon
  8. import xbmcgui
  9. import xbmcplugin
  10. # http://docs.python-requests.org/en/latest/
  11. import requests
  12. # http://www.crummy.com/software/BeautifulSoup/bs4/doc/
  13. from bs4 import BeautifulSoup
  14.  
  15. def build_url(query):
  16.     base_url = sys.argv[0]
  17.     return base_url + '?' + urllib.urlencode(query)
  18.    
  19. def get_page(url):
  20.     # download the source HTML for the page using requests
  21.     # and parse the page using BeautifulSoup
  22.     return BeautifulSoup(requests.get(url).text, 'html.parser')
  23.    
  24. def parse_page(page):
  25.     songs = {}
  26.     index = 1
  27.     # the sample below is specific for the page we are scraping
  28.     # you will need to view the source of the page(s) you are
  29.     # planning to scrape to find the content you want to display
  30.     # this will return all the <a> elements on the page:
  31.     # <a href="some_url">some_text</a>
  32.     for item in page.find_all('a'):
  33.         # the item contains a link to an album cover
  34.         if item['href'].find('.jpg') > 1:
  35.             # format the url for the album cover to include the site url and url encode any spaces
  36.             album_cover = '{0}{1}'.format(mixes, item['href'].replace(' ', '%20'))
  37.         # the item contains a link to a song containing '.mp3'
  38.         if item['href'].find('.mp3') > 1:
  39.             # update dictionary with the album cover url, song filename, and song url
  40.             songs.update({index: {'album_cover': album_cover, 'title': item['href'], 'url': '{0}{1}'.format(mixes, item['href'])}})
  41.             index += 1
  42.     return songs
  43.    
  44. def build_song_list(songs):
  45.     song_list = []
  46.     # iterate over the contents of the dictionary songs to build the list
  47.     for song in songs:
  48.         # create a list item using the song filename for the label
  49.         li = xbmcgui.ListItem(label=songs[song]['title'], thumbnailImage=songs[song]['album_cover'])
  50.         # set the fanart to the albumc cover
  51.         li.setProperty('fanart_image', songs[song]['album_cover'])
  52.         # set the list item to playable
  53.         li.setProperty('IsPlayable', 'true')
  54.         # build the plugin url for Kodi
  55.         # Example: plugin://plugin.audio.example/?url=http%3A%2F%2Fwww.theaudiodb.com%2Ftestfiles%2F01-pablo_perez-your_ad_here.mp3&mode=stream&title=01-pablo_perez-your_ad_here.mp3
  56.         url = build_url({'mode': 'stream', 'url': songs[song]['url'], 'title': songs[song]['title']})
  57.         # add the current list item to a list
  58.         song_list.append((url, li, False))
  59.     # add list to Kodi per Martijn
  60.     # http://forum.kodi.tv/showthread.php?tid=209948&pid=2094170#pid2094170
  61.     xbmcplugin.addDirectoryItems(addon_handle, song_list, len(song_list))
  62.     # set the content of the directory
  63.     xbmcplugin.setContent(addon_handle, 'songs')
  64.     xbmcplugin.endOfDirectory(addon_handle)
  65.    
  66. def play_song(url):
  67.     # set the path of the song to a list item
  68.     play_item = xbmcgui.ListItem(path=url)
  69.     # the list item is ready to be played by Kodi
  70.     xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
  71.    
  72. def main():
  73.     args = urlparse.parse_qs(sys.argv[2][1:])
  74.     mode = args.get('mode', None)
  75.    
  76.     # initial launch of add-on
  77.     if mode is None:
  78.         # get the HTML for http://www.theaudiodb.com/testfiles/
  79.         page = get_page(mixes)
  80.         # get the content needed from the page
  81.         content = parse_page(page)
  82.         # display the list of songs in Kodi
  83.         build_song_list(content)
  84.     # a song from the list has been selected
  85.     elif mode[0] == 'stream':
  86.         # pass the url of the song to play_song
  87.         play_song(args['url'][0])
  88.    
  89. if __name__ == '__main__':
  90.     mixes = 'https://www.truehouse.net/mixes/'
  91.     archiv = 'https://www.truehouse.net/archiv/'
  92.     addon_handle = int(sys.argv[1])
  93.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement