-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.py
executable file
·187 lines (140 loc) · 6.39 KB
/
addon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sys
import urllib
import urlparse
import xbmcgui
import xbmcaddon
import xbmcplugin
import netflix
import pickle
import os
import subprocess
import shutil
base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
args = urlparse.parse_qs(sys.argv[2][1:])
addon = xbmcaddon.Addon()
addonId = addon.getAddonInfo('id')
username = addon.getSetting("username")
password = addon.getSetting("password")
scriptFileSysPath = xbmc.translatePath('special://home/addons/'+addonId)
mode = args.get('mode', None)
PICKLE_FILE = os.path.join(scriptFileSysPath, "netflix.pickle")
PROFILES_DICT_KEY = "profiles"
CHROME_USER_DATA_PATH = xbmc.translatePath('special://home/addons/%s/ChromeProfile' % addonId)
xbmcplugin.setContent(addon_handle, 'movies')
def build_url(query):
return base_url + '?' + urllib.urlencode(query)
def init():
if os.access(PICKLE_FILE, os.R_OK):
xbmc.log("Loading data from pickle")
netflixDict = pickle.load(open(PICKLE_FILE, "rb"))
else:
xbmc.log("Getting fresh data")
netflixDict = {}
with netflix.Netflix(username, password, profilename=None, datadir=CHROME_USER_DATA_PATH) as flix:
netflixDict[PROFILES_DICT_KEY] = flix.get_profiles()
# Save the netflix data for use later
pickle.dump(netflixDict, open(PICKLE_FILE, "wb"))
for name in netflixDict[PROFILES_DICT_KEY]:
xbmc.log("adding: " + name)
url = build_url({'mode': 'profile', 'profilename': name})
li = xbmcgui.ListItem(name, iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
xbmcplugin.endOfDirectory(addon_handle)
def profile_selection():
profilename = args['profilename'][0]
if os.access(PICKLE_FILE, os.R_OK):
netflixDict = pickle.load(open(PICKLE_FILE, "rb"))
if (not netflixDict.has_key(profilename)) or \
(len(netflixDict[profilename].items()) == 0):
xbmc.log("Getting fresh data")
with netflix.Netflix(username, password, profilename, datadir=CHROME_USER_DATA_PATH) as flix:
netflixDict[profilename] = flix.get_categories()
try:
os.remove(PICKLE_FILE)
except:
pass
# Save the netflix data for use later
pickle.dump(netflixDict, open(PICKLE_FILE, "wb"))
for category, _ in netflixDict[profilename].items():
url = build_url({'mode': 'category', 'profilename': profilename, 'category': category})
li = xbmcgui.ListItem(category, iconImage='DefaultFolder.png')
# Build context menu item
scriptPath = xbmc.translatePath('special://home/addons/'+addonId+'/contextMenu.py')
menuUrl = urllib.urlencode(({'mode': 'refresh_category', 'profilename' : profilename, 'category' : category}))
scriptCmd = 'XBMC.RunAddon(%s, %s)' % (addonId, menuUrl)
commands = [("Refresh", scriptCmd, )]
li.addContextMenuItems( commands )
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
xbmcplugin.endOfDirectory(addon_handle)
def category_selection():
profilename = args['profilename'][0]
category = args['category'][0]
if os.access(PICKLE_FILE, os.R_OK):
netflixDict = pickle.load(open(PICKLE_FILE, "rb"))
if netflixDict.has_key(profilename):
for title, flixUrl in netflixDict[profilename][category].items():
url = build_url({'mode': 'play', 'profilename' : profilename, 'url': flixUrl})
li = xbmcgui.ListItem(title, iconImage='DefaultVideo.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
xbmcplugin.endOfDirectory(addon_handle)
def play():
url = args['url'][0]
profilename = args['profilename'][0]
scriptPath = xbmc.translatePath('special://home/addons/'+addonId+'/player.py')
xbmc.executebuiltin('XBMC.RunScript(%s, %s, %s, %s, %s, %s)' % (scriptPath, url, username, password, profilename, CHROME_USER_DATA_PATH))
xbmcplugin.endOfDirectory(addon_handle)
def delete_cache():
'''
Delete the cached list of videos under each profile
'''
xbmc.log("delete_cache called")
if os.access(PICKLE_FILE, os.R_OK):
xbmc.log("Loading data from pickle")
netflixDict = pickle.load(open(PICKLE_FILE, "rb"))
for profilename in netflixDict[PROFILES_DICT_KEY]:
if netflixDict.has_key(profilename):
netflixDict.pop(profilename)
os.remove(PICKLE_FILE)
# Save the netflix data for use later
pickle.dump(netflixDict, open(PICKLE_FILE, "wb"))
xbmcplugin.endOfDirectory(addon_handle)
def reset_addon():
try:
if os.path.exists(PICKLE_FILE):
os.remove(PICKLE_FILE)
if os.path.exists(CHROME_USER_DATA_PATH):
shutil.rmtree(CHROME_USER_DATA_PATH)
except:
pass
xbmcplugin.endOfDirectory(addon_handle)
def refresh_category():
profilename = args['profilename'][0]
category = args['category'][0]
if os.access(PICKLE_FILE, os.R_OK):
netflixDict = pickle.load(open(PICKLE_FILE, "rb"))
if netflixDict.has_key(profilename) and netflixDict[profilename].has_key(category):
with netflix.Netflix(username, password, profilename, datadir=CHROME_USER_DATA_PATH) as flix:
netflixDict[profilename][category] = flix.get_categories(specific_category=category)
os.remove(PICKLE_FILE)
# Save the netflix data for use later
pickle.dump(netflixDict, open(PICKLE_FILE, "wb"))
profile_selection()
while (username == "" or password == ""):
addon.openSettings()
if mode is None:
init()
elif mode[0] == 'profile':
profile_selection()
elif mode[0] == 'category':
category_selection()
elif mode[0] == 'play':
play()
elif mode[0] == 'deleteCache':
delete_cache()
elif mode[0] == 'resetAddon':
reset_addon()
elif mode[0] == 'refresh_category':
refresh_category()