Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import sys
  2. import urllib
  3. import urlparse
  4. import xbmcgui
  5. import xbmcplugin
  6. import xbmcaddon
  7. import requests
  8. import pyxbmct
  9.  
  10. base_url = sys.argv[0]
  11. addon_handle = int(sys.argv[1])
  12. args = urlparse.parse_qs(sys.argv[2][1:])
  13.  
  14. xbmcplugin.setContent(addon_handle, 'movies')
  15.  
  16. my_addon = xbmcaddon.Addon()
  17.  
  18. def build_url(query):
  19.     return base_url + '?' + urllib.urlencode(query)
  20.  
  21. mode = args.get('mode', None)
  22.  
  23. # begin to write api logic
  24.  
  25. username = my_addon.getSetting('username')
  26. password = my_addon.getSetting('password')
  27.  
  28. api_base = '***********'
  29.  
  30. s = requests.Session()
  31.  
  32. # get auth token
  33.  
  34. token = s.get(api_base + '/token', headers = {
  35.     'Email': username,
  36.     'Password': password
  37. }).headers['Token']
  38.  
  39. s.headers['Authorization'] = 'Token ' + token
  40.  
  41. # get mount (Dropbox...)
  42.  
  43. mounts = s.get(api_base + '/api/v2/mounts').json()['mounts']
  44.  
  45. mount = [x for x in mounts if x['name'] == 'Digi Cloud'][0]
  46.  
  47. print(mount['name'] + mount['id'])
  48.  
  49. if mode is None:
  50.     files = s.get(api_base + '/api/v2/mounts/' + mount['id'] + '/files/list', params = {'path': '/'}).json()['files']
  51.     for file in files:
  52.         if file['type'] == 'dir':
  53.             url = build_url({'mode': 'folder', 'foldername': '/' + file['name']})
  54.             li = xbmcgui.ListItem('/' + file['name'], iconImage='DefaultFolder.png')
  55.             xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
  56.                                 listitem=li, isFolder=True)
  57.         elif '.mp3' in file['name'] or '.mkv' in file['name']:
  58.             url = s.get(api_base + '/api/v2/mounts/' + mount['id'] + '/files/download', params = {'path': '/'+file['name']}).json()['link']
  59.             li = xbmcgui.ListItem(file['name'], iconImage='media.png')
  60.             xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
  61.  
  62.     xbmcplugin.endOfDirectory(addon_handle)
  63. elif mode[0] == 'folder':
  64.     foldername = args['foldername'][0]
  65.     files = s.get(api_base + '/api/v2/mounts/' + mount['id'] + '/files/list', params = {'path': foldername + '/'}).json()['files']
  66.     for file in files:
  67.         if file['type'] == 'dir':
  68.             url = build_url({'mode': 'folder', 'foldername': foldername + '/' + file['name']})
  69.             li = xbmcgui.ListItem(foldername + '/' + file['name'], iconImage='DefaultFolder.png')
  70.             xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
  71.                                 listitem=li, isFolder=True)
  72.         elif '.mp3' in file['name'] or '.mkv' in file['name']:
  73.             url = s.get(api_base + '/api/v2/mounts/' + mount['id'] + '/files/download', params = {'path': foldername + '/' + file['name']}).json()['link']
  74.             li = xbmcgui.ListItem(file['name'], iconImage='media.png')
  75.             xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
  76.  
  77.     xbmcplugin.endOfDirectory(addon_handle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement