~wattazoum/xbmc/mangas-tv-plugin

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import md5
import pickle
import os
import sys
import re
import urllib
import shutil
import xbmc
import xbmcgui
import xbmcaddon
import xbmcplugin
from BeautifulSoup import BeautifulSoup
from string import join
from resources.lib.utils import isCacheValid, debug, unescapeHTMLchar, is_number,\
	 Browser, info, _get_keyboard
from pickle import HIGHEST_PROTOCOL

__addonid__ = 'plugin.video.mangastv'
__settings__ = xbmcaddon.Addon(id=__addonid__)
__site_url__ = "http://mangas-tv.com/"
__version__ = "2.1"

lang = __settings__.getLocalizedString

CATEGORIE_LIST_MODE = 'categorielist'
ANIME_LIST_MODE = 'animelist'
MOST_VIEWED_MODE = 'mostviewed'
PROMOTED_LIST_MODE = 'promoted'
PLAY_VIDEO_MODE = 'playVideo'
SHOW_EPISODES_MODE = 'showEpisodes' 
LOGIN_TO_UNLOCK_MODE = 'loginToUnlock'
SEARCH_MODE = 'search'

# Empty cache dir when we change the version
profile_dir = xbmc.translatePath(__settings__.getAddonInfo('profile'))
COOKIEFILE = os.path.join(profile_dir, 'cookie-mangas-tv.lwp')
cache_dir = os.path.join(profile_dir, "cache")
versionfile = os.path.join(cache_dir, __version__)
if not os.path.exists(cache_dir):
	os.makedirs(cache_dir)
elif not os.path.exists(versionfile) :
	shutil.rmtree(cache_dir)
	os.makedirs(cache_dir)
	f = open(versionfile, 'w')
	f.close()

class UpdateArgs:

	def __init__(self, args):
		for key, value in args.iteritems():
			if value == 'None':
				args[key] = None
			else:
				args[key] = urllib.unquote_plus(args[key])
		self.__dict__.update(args)

class UI:
	
	modeToContentDict = {CATEGORIE_LIST_MODE: 'movies',
						MOST_VIEWED_MODE : 'tvshows',
						PROMOTED_LIST_MODE: 'tvshows',
						ANIME_LIST_MODE: 'tvshows',
						SHOW_EPISODES_MODE: 'episodes',
						PLAY_VIDEO_MODE: 'episodes',
						LOGIN_TO_UNLOCK_MODE: 'files',
						SEARCH_MODE: 'tvshows' }
	
	def __init__(self):
		self.main = Main(checkMode=False)
		self.browser = Browser(COOKIEFILE)
		self.pagelimit = '100'
		if __settings__.getSetting('page_limit') is not None and __settings__.getSetting('page_limit') != '':
				self.pagelimit = __settings__.getSetting('page_limit')
		self.cache_delay = 24
		if __settings__.getSetting('cache_delay')  is not None and __settings__.getSetting('cache_delay') != '':
			self.cache_delay = int(__settings__.getSetting('cache_delay'))
		if self.main.args.mode is not None:
			xbmcplugin.setContent(int(sys.argv[1]), self.modeToContentDict[self.main.args.mode])
		else:
			xbmcplugin.setContent(int(sys.argv[1]), 'files')
		
	def welcomePage(self):
		self.addItem({'Title': lang(30030), 'mode':CATEGORIE_LIST_MODE, 'url': __site_url__ + "index.php?module=listeMangas"})
		self.addItem({'Title': lang(30031), 'mode': MOST_VIEWED_MODE, 'url': __site_url__ + 'index.php?module=listeMangas&voir=vus&d=1&l=' + str(self.pagelimit)})
		self.addItem({'Title': lang(30032), 'mode': PROMOTED_LIST_MODE, 'url': __site_url__ + 'index.php?module=listeMangas&voir=prom&d=1&l=' + str(self.pagelimit)})
		self.addItem({'Title': lang(30033), 'mode': ANIME_LIST_MODE, 'url': __site_url__ + 'index.php?module=listeMangas&voir=tous&d=1'})
		self.addItem({'Title': lang(30045), 'mode': SEARCH_MODE, 'url': __site_url__ + "index.php?module=listeMangas"})
		self.endofdirectory()

	def __getFromCache(self, main, processFct, addSingleItemFct, dontAddToHierarchy = False):
		listItems = []
		cachefilepath = os.path.join(cache_dir, md5.new(main.args.url).hexdigest())
		if isCacheValid(cachefilepath, max_age=3600 * self.cache_delay):
			cachefile = open(cachefilepath, 'rb')
			listItems = pickle.load(cachefile)
			total = len(listItems)
			for item in listItems:
				addSingleItemFct(item, total)	
		else:
			dialog = xbmcgui.DialogProgress()
			dialog.create("Mangas-TV.com", lang(30034), lang(30035))
			dialog.update(50)
			htmlSource = self.browser.downloadHTML(main.args.url)
			soup = BeautifulSoup(htmlSource)
			dialog.update(100)
			dialog.close()
			processFct(soup, listItems)
			cachefile = open(cachefilepath, 'wb')
			pickle.dump(listItems, cachefile, HIGHEST_PROTOCOL)
			
		self.endofdirectory(sortMethod='title', dontAddToHierarchy=dontAddToHierarchy)

	def showCategories(self, main):
		def __parseCategory(soup, listItems):
			listCategories = soup.find('div', attrs={'id':'vignetteCategoriesTrie'}).findAll('div', attrs={'class':'vignetteCategorie'})
			total = len(listCategories)
			for cat in listCategories:
				thumb = __site_url__ + cat.find('img')['src']
				url = __site_url__ + cat.find('a')['href'] + '&d=1&l=' + str(self.pagelimit)
				title = join(cat.findAll(text=True), '').strip()
				debug("thumb = %s , href = %s, title = %s" % (thumb, url, title))
				item = {'Title': title, 'mode' : ANIME_LIST_MODE, 'url': url, 'category': title, 'Thumb' : thumb}
				infoLabels = { "name": title, "plot": "", "genre": title, "rating" : 0}
				itemTuple = (item, infoLabels)
				listItems.append(itemTuple)
				__addSingleItem(itemTuple, total)
		
		def __addSingleItem(itemTuple, total):
			item, extra = itemTuple
			self.addItem(info=item, extrainfo=extra , total_items=total)
		
		self.__getFromCache(main, __parseCategory, __addSingleItem)
			
	def search(self, main):
		vq = _get_keyboard( heading=lang(30046) )
		
		if ( not vq ): return False, 0
		# we need to set the title to our query
		info("search query = " + vq)
		dialog = xbmcgui.DialogProgress()
		dialog.create("Mangas-TV.com", lang(30034), lang(30035))
		dialog.update(50)

		data = urllib.urlencode({"formAction" : "textRecherche", 'textRecherche' : vq, 'resultLicence': 1, 'submit':'Valider'})
		response = self.browser.opener.open(main.args.url, data)
		htmlsource = unicode(response.read(), 'utf-8', 'replace')
		response.close()
		soup = BeautifulSoup(htmlsource)
		dialog.update(100)
		dialog.close()
		listItems = []
		self.__parseAnimeSeries(soup, listItems);
		self.endofdirectory(sortMethod='title', dontAddToHierarchy=False)
		
		
	def __parseAnimeSeries(self, soup, listItems):
		listArticleManga = soup.findAll('div', attrs={'class':'articleManga'})
		total = len(listArticleManga)
		for articleManga in listArticleManga:
			title = articleManga.find("div", attrs={"class": "nomMangaFr"}).find("a").find(text=True).strip()
			url = __site_url__ + articleManga.find("div", attrs={"class": "nomMangaFr"}).find("a")['href']
			image = __site_url__ + articleManga.find('img', attrs={'class': 'articleMangaImage'})['src']
			desc = articleManga.find('p', attrs={'class': 'articleMangaDescription'}).find(text=True).strip()
			detailManga = articleManga.find("div", attrs={"class": "optionDetailManga"})
			stats = detailManga.findAll('b')
			
			# get genre
			genre = lang(30039)
			genreTable = articleManga.find("td", attrs={"class": "articleMangaContenu"}).find('table')
			if genreTable is not None:
				genreList = genreTable.findAll('td')
				
				genre = ""
				prevGVotes = 0
				for genreTd in genreList:
					genreCandidate = join(genreTd.find("br").findNextSiblings(text=True), '').strip()
					genreCandidate = genreCandidate.replace("\n votes", "")
					gVotes = int(genreTd.find("b").string)	
					if prevGVotes > 0 :
						genre = genre + ', ' + genreCandidate
					else :
						genre = genreCandidate
					prevGVotes = gVotes
			
			nbEpisodes = None
			name = title
			m = re.search("Episodes disponible\(s\): (([0-9]*?)/([0-9a-zA-Z_]*))", str(detailManga))
			if m :
				name = title + " (%s episodes)" % m.group(1)
				nbEpisodes = m.group(2)
				
			infoLabels = { "name": title, "plot": desc, "genre": genre}				
			if nbEpisodes:
				infoLabels['nbepisodes'] = nbEpisodes
			
			rating, votes, views = stats[1].find(text=True), stats[2].find(text=True), stats[3].find(text=True)
			if rating is not None and is_number(rating):
				infoLabels['rating'] = float(rating)
			else : 
				infoLabels['rating'] = 0.0
			if votes is not None and is_number(rating):
				infoLabels['votes'] = long(votes)
			else:
				infoLabels['votes'] = 0
			if views is not None and is_number(rating):
				infoLabels['playcount'] = long(views)
			else:
				infoLabels['playcount'] = 0
			info = {'Title': name, 'mode' : SHOW_EPISODES_MODE, 'url': url , 'Thumb' : image}
			debug('info = %s, infoLabel = %s' % (info, infoLabels))
			item = (info, infoLabels)
			listItems.append(item)
			self.__addAnimeSeriesSingleItem(item, total)
				
	def __addAnimeSeriesSingleItem(self, itemTuple, total):
		item, extra = itemTuple
		self.addItem(info=item, extrainfo=extra, total_items=total)	

	def showAnimeSeries(self, main):
		
		self.__getFromCache(main, self.__parseAnimeSeries, self.__addAnimeSeriesSingleItem)

	def showEpisodes(self, main, dontAddToHierarchy=False):
		def __showEpisodes(soup, listItems):
			ficheManga = soup.find('div', attrs={'id' : 'ficheMangaPublic'})
			episodes = ficheManga.find('div', attrs={'id' : 'episodes'})
			if episodes is None:
				xbmcplugin.setContent(int(sys.argv[1]), 'files')
				infoRestriction = ficheManga.find('center')
				if infoRestriction is not None:
					img = __site_url__ + infoRestriction.find('img')['src']
					info = {'Title': lang(30036), 'mode' : LOGIN_TO_UNLOCK_MODE, 'url' : self.main.args.url , 'Thumb' : img, 'no-url' : True, 'icon2' : self.main.args.icon }
					if self.main.args.__dict__.has_key('icon2'):
						info['icon2'] = self.main.args.icon2 
				else:
					img = __site_url__ + "fichiers/licence.gif"
					info = {'Title': lang(30037), 'mode' : SHOW_EPISODES_MODE, 'url' : self.main.args.url , 'Thumb' : img, 'no-url' : True }
				listItems.append(info)
				__addSingleItem(info, 1)
			else:
				episodesList = soup.find('div', attrs={'id' : 'episodes'}).findAll('a')
				total = len(episodesList)
				for episode in episodesList:
					title = episode.find('a', text=True)
					if title is None:
						continue
					title = title.strip()
					debug('checking %s' % title)
					link = __site_url__ + episode['href']
					debug(link)
					info = {'Title': title, 'mode' : PLAY_VIDEO_MODE, 'url': link, 'Thumb': self.main.args.icon }
					if self.main.args.__dict__.has_key('icon2'):
						info['Thumb'] = self.main.args.icon2
					listItems.append(info)
					__addSingleItem(info, total)
		
		def __addSingleItem(item, total):
			if item.has_key('no-url'):
				self.addItem(info=item, total_items=total)
			else:
				self.addItem(info=item, isFolder=False, total_items=total)	
		
		self.__getFromCache(main, __showEpisodes, __addSingleItem, dontAddToHierarchy)

	def loginToUnlock(self, main, refererMethod, *extraArgs):
		
		def __openLoginDialog(line1, line2="", line3=""):
			self.resp = xbmcgui.Dialog().yesno(line1, line2, line3)
			if self.resp:
				__settings__.openSettings()
			return not self.resp
				
		if __settings__.getSetting('username') == '' or __settings__.getSetting('password') == '':
			__openLoginDialog(lang(30040), lang(30041), lang(30042))
			removeCookie = False
		elif __settings__.getSetting('username') != __settings__.getSetting('prev_username') or __settings__.getSetting('password') != __settings__.getSetting('prev_password'):
			removeCookie = True
		else:
			removeCookie = False
		
		status = self.browser.login(__settings__, __site_url__ + 'index.php?module=connection', removeCookie)
		if not status:
			stopAskingMe = False
			while not status:
				stopAskingMe = __openLoginDialog(lang(30043), lang(30044))
				if stopAskingMe:
					break
				status = self.browser.login(__settings__, __site_url__ + 'index.php?module=connection', removeCookie)
			
		cachefilepath = os.path.join(cache_dir, md5.new(main.args.url).hexdigest())
		if os.path.exists(cachefilepath):
			os.remove(cachefilepath)
		if extraArgs:
			refererMethod(main, extraArgs[0])
		else:
			refererMethod(main)

	def playVideo(self, main, stop = False):
		vidHtml = self.browser.downloadHTML(main.args.url)
		soup = BeautifulSoup(vidHtml)
		lecteurFlash = soup.find('div', attrs={'id':'lecteurFlash'})
		if lecteurFlash:
			jsWithVidLink = lecteurFlash.find('script', attrs={'type':'text/javascript'}).find(text=True)
			m = re.search('url: \'(.+?)\',', jsWithVidLink)
			if m is None:
				dialog = xbmcgui.Dialog()
				dialog.ok("Mangas-TV.com", lang(30038))
				xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=False)
			else:
				link = m.group(1)
				title = soup.find('div', attrs={'id':'lecteur'}).h2.string
				debug("title = %s, link = %s" % (title, link))
				item = xbmcgui.ListItem(label=title, label2=title, path=link, iconImage=main.args.icon, thumbnailImage=main.args.icon)
				xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
		elif stop:
			pass
		else:
			self.loginToUnlock(main, self.playVideo, True)
			
	def addItem(self, info, extrainfo=None, isFolder=True, total_items=0):
		#Defaults in dict. Use 'None' instead of None so it is compatible for quote_plus in parseArgs
		info.setdefault('url', 'None')
		info.setdefault('Thumb', 'None')
		info.setdefault('id', 'None')
		info.setdefault('category', 'None')
		info.setdefault('referer', 'None')
		info.setdefault('Icon', info['Thumb'])
		#create params for xbmcplugin module
		u = sys.argv[0] + \
			'?url=' + urllib.quote_plus(info['url']) + \
			'&mode=' + urllib.quote_plus(info['mode']) + \
			'&id=' + urllib.quote_plus(info['id']) + \
			'&category=' + urllib.quote_plus(info['category']) + \
			'&referer=' + urllib.quote_plus(info['referer']) + \
			'&icon=' + urllib.quote_plus(info['Thumb'])
		if info.has_key('icon2'):
			u = u + "&icon2=" + urllib.quote_plus(info['icon2'])
		#create list item
		li = xbmcgui.ListItem(label=unescapeHTMLchar(info['Title']), iconImage=info['Thumb'], thumbnailImage=info['Thumb'])
		
		if extrainfo != None:
			extra = { "Title":unescapeHTMLchar(extrainfo['name']), "Plot":unescapeHTMLchar(extrainfo['plot']), "Genre":unescapeHTMLchar(extrainfo['genre']), 'Rating':extrainfo['rating']}
			if extrainfo.has_key('nbepisodes'):
				extra['Episode'] = int(extrainfo['nbepisodes'])

			li.setInfo(type="Video", infoLabels=extra)
		
		if not isFolder:
			li.setProperty("IsPlayable", "true")#let xbmc know this can be played, unlike a folder.
		else:
			li.setProperty("IsPlayable", "false")
		debug("url = %s" % u)
		#add item to list
		xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=li, isFolder=isFolder, totalItems=total_items)

	def mostViewed(self, main):
		self.showAnimeSeries(main)
		
	def promoted(self, main):
		self.showAnimeSeries(main)
	
	def endofdirectory(self, sortMethod='none', dontAddToHierarchy = False):
		# set sortmethod to something xbmc can use
		if sortMethod == 'title':
			xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_LABEL)
			xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_RATING)
			xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_GENRE)
		elif sortMethod == 'none':
			xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_NONE)

		xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), updateListing=dontAddToHierarchy, cacheToDisc=True)

class Main:

	args = None

	def __init__(self, checkMode=True):
		print sys.argv
		self.parseArgs()
		if checkMode:
			self.checkMode()

	def parseArgs(self):
		if (sys.argv[2]):
			argsDict = {}
			listParams = sys.argv[2][1:].split('&')
			debug('Parameter list: %s ' % str(listParams))
			for paramPair in listParams:
				key, value = paramPair.split('=')
				argsDict[str(key)] = str(value)
			self.args = UpdateArgs(argsDict)
		else:
			self.args = UpdateArgs({'mode': 'None', 'url': 'None', 'name' : 'None'})

	def checkMode(self):
		mode = self.args.mode
		if mode is None:
			UI().welcomePage()
		elif mode == CATEGORIE_LIST_MODE:
			UI().showCategories(self)
		elif mode == MOST_VIEWED_MODE:
			UI().mostViewed(self)
		elif mode == ANIME_LIST_MODE:
			UI().showAnimeSeries(self)
		elif mode == PROMOTED_LIST_MODE:
			UI().promoted(self)
		elif mode == SEARCH_MODE:
			UI().search(self)
		elif mode == SHOW_EPISODES_MODE:
			UI().showEpisodes(self)
		elif mode == PLAY_VIDEO_MODE:
			UI().playVideo(self)
		elif mode == LOGIN_TO_UNLOCK_MODE:
			ui = UI()
			ui.loginToUnlock(self, ui.showEpisodes, True)