summaryrefslogtreecommitdiff
path: root/podcast/src/plugin.py
blob: cd42050a34386aee3dc7454c240e7de29fec1635 (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
## Podcast
## by AliAbdul
##
from Components.ActionMap import ActionMap
from Components.config import config, ConfigSelection, ConfigSubsection, ConfigText, ConfigYesNo, getConfigListEntry
from Components.ConfigList import ConfigListScreen
from Components.FileList import FileList
from Components.Label import Label
from Components.Language import language
from Components.MenuList import MenuList
from Components.PluginComponent import plugins
from Components.ProgressBar import ProgressBar
from enigma import eServiceReference, eTimer
from os import environ, remove
from Plugins.Plugin import PluginDescriptor
from Screens.InfoBar import MoviePlayer
from Screens.MessageBox import MessageBox
from Screens.Screen import Screen
from Tools.BoundFunction import boundFunction
from Tools.Directories import fileExists, resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
from Tools.Downloader import downloadWithProgress
from twisted.web.client import getPage
from xml.etree.cElementTree import parse
import gettext, re

###################################################

def localeInit():
	lang = language.getLanguage()
	environ["LANGUAGE"] = lang[:2]
	gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))
	gettext.textdomain("enigma2")
	gettext.bindtextdomain("Podcast", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/Podcast/locale/"))

def _(txt):
	t = gettext.dgettext("Podcast", txt)
	if t == txt:
		t = gettext.gettext(txt)
	return t

localeInit()
language.addCallback(localeInit)

###################################################

class ChangedMoviePlayer(MoviePlayer):
	def __init__(self, session, service):
		MoviePlayer.__init__(self, session, service)
		self.skinName = "MoviePlayer"

	def leavePlayer(self):
		self.session.openWithCallback(self.leavePlayerConfirmed, MessageBox, _("Stop playing this movie?"))

	def leavePlayerConfirmed(self, answer):
		if answer:
			self.close()

	def doEofInternal(self, playing):
		pass

	def getPluginList(self):
		list = []
		for p in plugins.getPlugins(where=PluginDescriptor.WHERE_EXTENSIONSMENU):
			if p.name != _("Podcast"):
				list.append(((boundFunction(self.getPluginName, p.name), boundFunction(self.runPlugin, p), lambda: True), None))
		return list

	def showMovies(self):
		pass

###################################################

config.plugins.Podcast = ConfigSubsection()
config.plugins.Podcast.buffer = ConfigYesNo(default=True)
config.plugins.Podcast.bufferDevice = ConfigText(default="/media/hdd/", fixed_size=False)
config.plugins.Podcast.keepStored = ConfigSelection(choices={"delete": _("delete"), "keep": _("keep on device"), "ask": _("ask me")}, default="delete")

###################################################

def encodeUrl(url):
	url = url.replace("&", "&")
	url = url.replace("&lt;", "<")
	url = url.replace("&gt;", ">")
	url = url.replace("&#39;", "'")
	url = url.replace("&quot;", '"')
	url = url.replace("&#42;", "*")
	url = url.replace("&#124;", "|")
	url = url.replace("&#039;", "'")
	url = url.replace("&#187;", ">>")
	return url

###################################################

class BufferThread():
	def __init__(self):
		self.progress = 0
		self.downloading = False
		self.error = ""
		self.download = None

	def startDownloading(self, url, file):
		self.progress = 0
		self.downloading = True
		self.error = ""
		self.download = downloadWithProgress(url, file)
		self.download.addProgress(self.httpProgress)
		self.download.start().addCallback(self.httpFinished).addErrback(self.httpFailed)

	def httpProgress(self, recvbytes, totalbytes):
		self.progress = int(100 * recvbytes / float(totalbytes))

	def httpFinished(self, string=""):
		self.downloading = False
		if string is not None:
			self.error = str(string)
		else:
			self.error = ""

	def httpFailed(self, failure_instance=None, error_message=""):
		self.downloading = False
		if error_message == "" and failure_instance is not None:
			error_message = failure_instance.getErrorMessage()
			self.error = str(error_message)

	def stop(self):
		self.progress = 0
		self.downloading = False
		self.error = ""
		self.download.stop()

bufferThread = BufferThread()

###################################################

class PodcastBuffer(Screen):
	skin = """
		<screen position="center,center" size="520,80" title="%s" >
			<widget name="info" position="5,5" size="510,40" font="Regular;18" halign="center" valign="center" />
			<widget name="progress" position="100,50" size="320,14" pixmap="skin_default/progress_big.png" borderWidth="2" borderColor="#cccccc" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url, file):
		self.session = session
		Screen.__init__(self, session)
		
		self.url = url
		self.file = file
		
		self.infoTimer = eTimer()
		self.infoTimer.timeout.get().append(self.updateInfo)
		
		self["info"] = Label(_("Downloading movie: %s") % self.file)
		self["progress"] = ProgressBar()
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.okClicked, "cancel": self.exit}, -1)
		
		self.onLayoutFinish.append(self.downloadMovie)

	def downloadMovie(self):
		bufferThread.startDownloading(self.url, self.file)
		self.infoTimer.start(300, False)

	def updateInfo(self):
		if bufferThread.error != "":
			self["info"].setText(bufferThread.error)
			self.infoTimer.stop()
		else:
			progress = int(bufferThread.progress)
			self["progress"].setValue(progress)
			if progress == 100:
				self.infoTimer.stop()
				self.close(True)

	def okClicked(self):
		if int(bufferThread.progress) > 0:
			self.infoTimer.stop()
			self.close(True)

	def exit(self):
		bufferThread.download.stop()
		self.close(None)

###################################################

class PodcastMovies(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="5,5" size="410,250" scrollbarMode="showOnDemand" />
			<eLabel position="5,260" size="420,2" backgroundColor="#ffffff" />
			<widget name="info" position="5,265" size="420,90" font="Regular;18" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self.url = url
		self.list = []
		self.movies = []
		self.working = True
		
		self["list"] = MenuList([])
		self["list"].onSelectionChanged.append(self.showInfo)
		self["info"] = Label()
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			if len(self.list) > 0:
				idx = self["list"].getSelectionIndex()
				(url, length, type) = self.splitExtraInfo(self.movies[idx][1])
				if config.plugins.Podcast.buffer.value:
					file = url
					while file.__contains__("/"):
						idx = file.index("/")
						file = file[idx+1:]
					self.file = "%s%s" % (config.plugins.Podcast.bufferDevice.value, file)
					self.session.openWithCallback(self.bufferCallback, PodcastBuffer, url, self.file)
				else:
					ref = eServiceReference(4097, 0, url)
					self.session.open(ChangedMoviePlayer, ref)

	def bufferCallback(self, callback):
		if callback is not None:
			ref = eServiceReference(4097, 0, self.file)
			self.session.openWithCallback(self.delete, ChangedMoviePlayer, ref)

	def delete(self, callback=None):
		if bufferThread.downloading: #still downloading?
			bufferThread.stop()
		if config.plugins.Podcast.keepStored.value == "delete":
			remove(self.file)
		elif config.plugins.Podcast.keepStored.value == "ask":
			self.session.openWithCallback(self.deleteCallback, MessageBox, _("Delete this movie?"))

	def deleteCallback(self, callback):
		if callback:
			remove(self.file)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage(self.url).addCallback(self.showMovies).addErrback(self.error)

	def showMovies(self, page):
		if page.__contains__("<itunes"):
			reonecat = re.compile(r'<item>(.+?)</item>', re.DOTALL)
			for item in reonecat.findall(page):
				reonecat2 = re.compile(r'<title>(.+?)</title>.+?<description>(.+?)</description>.+?<enclosure(.+?)/>.+?', re.DOTALL)
				for title, description, extra in reonecat2.findall(item):
					if title.startswith("<![CDATA["):
						title = title[9:]
					if title.endswith("]]>"):
						title = title[:-3]
					if description.__contains__("<![CDATA["):
						idx = description.index("<![CDATA[")
						description = description[idx+10:]
					if description.endswith("]]>"):
						description = description[:-3]
					self.list.append(encodeUrl(title))
					self.movies.append([description, extra])
		else:
			reonecat = re.compile(r'<title>(.+?)</title>.+?<description>(.+?)</description>.+?<enclosure(.+?)/>.+?', re.DOTALL)
			for title, description, extra in reonecat.findall(page):
				if title.startswith("<![CDATA["):
					title = title[9:]
				if title.endswith("]]>"):
					title = title[:-3]
				if description.__contains__("<![CDATA["):
					idx = description.index("<![CDATA[")
					description = description[idx+10:]
				if description.endswith("]]>"):
					description = description[:-3]
				self.list.append(encodeUrl(title))
				self.movies.append([description, extra])
		self["list"].setList(self.list)
		self.showInfo()
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting movies"))
		self.working = False

	def showInfo(self):
		if len(self.list) > 0:
			idx = self["list"].getSelectionIndex()
			description = self.movies[idx][0]
			(url, length, type) = self.splitExtraInfo(self.movies[idx][1])
			self["info"].setText("%s: %s   %s: %s\n%s" % (_("Length"), length, _("Type"), type, encodeUrl(description)))

	def splitExtraInfo(self, info):
		if info.__contains__('url="'):
			idx = info.index('url="')
			url = info[idx+5:]
			idx = url.index('"')
			url = url[:idx]
		else:
			url = "N/A"
		
		if info.__contains__('length="'):
			idx = info.index('length="')
			length = info[idx+8:]
			idx = length.index('"')
			length = length[:idx]
			length = str((int(length) / 1024) / 1024) + " MB"
		else:
			length = "N/A"
		
		if info.__contains__('type="'):
			idx = info.index('type="')
			type = info[idx+6:]
			idx = type.index('"')
			type = type[:idx]
		else:
			type = "N/A"
		
		return (url, length, type)

###################################################

class PodcastPodcasts(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, provider):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.close}, -1)
		
		self.urls = []
		list = []
		for podcast in provider.findall("podcast"):
			name = podcast.get("name") or None
			name = name.encode("UTF-8") or name
			url = podcast.get("url") or None
			if name and url:
				list.append(name)
				self.urls.append(url)
		self["list"] = MenuList(list)

	def ok(self):
		if len(self.urls) > 0:
			cur = self.urls[self["list"].getSelectedIndex()]
			self.session.open(PodcastMovies, cur)

###################################################

class PodcastProvider(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, language):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.close}, -1)
		
		self.providers = []
		list = []
		for provider in language.findall("provider"):
			name = provider.get("name") or None
			name = name.encode("UTF-8") or name
			if name:
				list.append(name)
				self.providers.append(provider)
		self["list"] = MenuList(list)

	def ok(self):
		if len(self.providers) > 0:
			cur = self.providers[self["list"].getSelectedIndex()]
			self.session.open(PodcastPodcasts, cur)

###################################################

class PodcastXML(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.close}, -1)
		
		self.languages = []
		list = []
		file = "/etc/podcast/podcasts.xml"
		if fileExists(file):
			xml = parse(file).getroot()
			for language in xml.findall("language"):
				name = language.get("name") or None
				name = name.encode("UTF-8") or name
				if name:
					list.append(name)
					self.languages.append(language)
		self["list"] = MenuList(list)

	def ok(self):
		if len(self.languages) > 0:
			cur = self.languages[self["list"].getSelectedIndex()]
			self.session.open(PodcastProvider, cur)

###################################################

class PodcastComGenre2(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self.url = url
		self.urls = []
		self.working = True
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadGenres)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				self.working = True
				cur = self.urls[self["list"].getSelectedIndex()]
				getPage(cur).addCallback(self.getRssUrl).addErrback(self.error2)

	def getRssUrl(self, page):
		idx = page.index('">rss feed</a><br>')
		page = page[:idx]
		while page.__contains__("http://"):
			idx = page.index("http://")
			page = page[idx+1:]
		self.working = False
		self.session.open(PodcastMovies, "h%s"%page)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadGenres(self):
		getPage(self.url).addCallback(self.getGenres).addErrback(self.error)

	def getGenres(self, page):
		list = []
		reonecat = re.compile(r'height="19"><a href="(.+?)">(.+?)</a>', re.DOTALL)
		for url, title in reonecat.findall(page):
			list.append(encodeUrl(title))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting genres"))
		self.working = False

	def error2(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting rss feed"))
		self.working = False

###################################################

class PodcastComGenre(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self.url = url
		self.urls = []
		self.working = True
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadSite)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.session.open(PodcastComGenre2, cur)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadSite(self):
		getPage(self.url).addCallback(self.getUrl).addErrback(self.error)

	def getUrl(self, page):
		reonecat = re.compile(r'Get this podcast channel on your mobile phone:</strong><br><a href="(.+?)"', re.DOTALL)
		list = reonecat.findall(page)
		if len(list) > 0:
			getPage(list[0]).addCallback(self.getGenres).addErrback(self.error)
		else:
			self.error("Error getting movies-url")

	def getGenres(self, page):
		list = []
		reonecat = re.compile(r'height="17"><a title="(.+?)" href="(.+?)">(.+?)</a>', re.DOTALL)
		for title2, url, title in reonecat.findall(page):
			list.append(encodeUrl(title))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting genres"))
		self.working = False

###################################################

class PodcastCom(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session):
		self.session = session
		Screen.__init__(self, session)
		
		self.working = True
		self.urls = []
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.session.open(PodcastComGenre, cur)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage("http://podcast.com/home.php?subpage=_pages/channels_home.php").addCallback(self.showGenres).addErrback(self.error)

	def showGenres(self, page):
		list = []
		reonecat = re.compile(r'<li><a href="(.+?)" title="(.+?)">(.+?)</a></li>', re.DOTALL)
		for url, title2, title in reonecat.findall(page):
			if not title.startswith("<"):
				list.append(encodeUrl(title))
				self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting genres"))
		self.working = False

###################################################

class LocationSelection(Screen):
	skin = """
	<screen position="center,center" size="560,300" title="%s">
		<ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
		<widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
		<widget name="filelist" position="10,45" size="550,255" scrollbarMode="showOnDemand" />
	</screen>""" % _("Podcast")

	def __init__(self, session, dir="/"):
		Screen.__init__(self, session)
		
		self["key_green"] = Label(_("Select"))
		
		try: self["filelist"] = FileList(dir, showDirectories=True, showFiles=False)
		except: self["filelist"] = FileList("/", showDirectories, showFiles)
		
		self["actions"] = ActionMap(["ColorActions", "OkCancelActions"],
			{
				"ok": self.okClicked,
				"cancel": self.exit,
				"green": self.select
			}, -1)
		
		self.onLayoutFinish.append(self.updateDirectoryName)
		
	def okClicked(self):
		if self["filelist"].canDescent():
			self["filelist"].descent()
			self["filelist"].instance.moveSelectionTo(0)
			self.updateDirectoryName()

	def exit(self):
		self.close(None)

	def select(self):
		dir = self["filelist"].getCurrentDirectory()
		if dir is not None:
			self.close(dir)
		else:
			self.close(None)

	def updateDirectoryName(self):
		try:
			dir = self["filelist"].getCurrentDirectory()
			self.instance.setTitle(dir)
		except:
			self.instance.setTitle("?")

###################################################

class PodcastConfig(ConfigListScreen, Screen):
	skin = """
	<screen position="center,center" size="560,180" title="%s">
		<ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
		<ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
		<widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
		<widget name="config" position="10,45" size="550,125" scrollbarMode="showOnDemand" />
	</screen>""" % _("Podcast")

	def __init__(self, session):
		Screen.__init__(self, session)
		
		self["key_green"] = Label(_("Save"))
		
		ConfigListScreen.__init__(self, [])
			
		
		self["actions"] = ActionMap(["OkCancelActions", "ColorActions"], {"green": self.save, "cancel": self.exit}, -1)
		
		self.onLayoutFinish.append(self.createConfig)

	def createConfig(self):
		self.deviceEntry = ConfigSelection(choices=[config.plugins.Podcast.bufferDevice.value], default=config.plugins.Podcast.bufferDevice.value)
		self["config"].list = [
			getConfigListEntry(_("Buffer:"), config.plugins.Podcast.buffer),
			getConfigListEntry(_("Buffer device:"), self.deviceEntry),
			getConfigListEntry(_("Buffer file handling:"), config.plugins.Podcast.keepStored)]

	def keyLeft(self):
		ConfigListScreen.keyLeft(self)
		self.handleKeysLeftAndRight()

	def keyRight(self):
		ConfigListScreen.keyRight(self)
		self.handleKeysLeftAndRight()

	def handleKeysLeftAndRight(self):
		sel = self["config"].getCurrent()[1]
		if sel == self.deviceEntry:
			self.session.openWithCallback(self.locationSelected, LocationSelection, config.plugins.Podcast.bufferDevice.value)

	def locationSelected(self, dir):
		if dir is not None and dir != "?":
			config.plugins.Podcast.bufferDevice.value = dir
			config.plugins.Podcast.bufferDevice.save()
			self.createConfig()

	def save(self):
		for x in self["config"].list:
			x[1].save()
		self.close()

	def exit(self):
		for x in self["config"].list:
			x[1].cancel()
		self.close()

###################################################

class PodcastDeEpisodes(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self.working = True
		self.url = url
		self.urls = []
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			self.instance.setTitle(_("Podcast"))
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.working = True
				getPage(cur).addCallback(self.playPodcast).addErrback(self.error2)

	def playPodcast(self, url):
		if url.__contains__('" id="control_download"'):
			self.working = False
			idx = url.index('" id="control_download"')
			url = url[:idx]
			while url.__contains__("http://"):
				idx = url.index("http://")
				url = url[idx+1:]
			url = "h%s"%url
			
			if config.plugins.Podcast.buffer.value:
				file = url
				while file.__contains__("/"):
					idx = file.index("/")
					file = file[idx+1:]
				self.file = "%s%s" % (config.plugins.Podcast.bufferDevice.value, file)
				self.session.openWithCallback(self.bufferCallback, PodcastBuffer, url, self.file)
			else:
				ref = eServiceReference(4097, 0, url)
				self.session.open(ChangedMoviePlayer, ref)
		else:
			self.error2()

	def bufferCallback(self, callback):
		if callback is not None:
			ref = eServiceReference(4097, 0, self.file)
			self.session.openWithCallback(self.delete, ChangedMoviePlayer, ref)

	def delete(self, callback=None):
		if bufferThread.downloading: #still downloading?
			bufferThread.stop()
		if config.plugins.Podcast.keepStored.value == "delete":
			remove(self.file)
		elif config.plugins.Podcast.keepStored.value == "ask":
			self.session.openWithCallback(self.deleteCallback, MessageBox, _("Delete this movie?"))

	def deleteCallback(self, callback):
		if callback:
			remove(self.file)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage(self.url).addCallback(self.showEpisodes).addErrback(self.error)

	def showEpisodes(self, page):
		list = []
		idx = page.index('<h3>')
		page = page[idx:]
		idx = page.index('</div></div>')
		page = page[:idx]
		reonecat = re.compile(r'<a href="(.+?)" title="(.+?)">', re.DOTALL)
		for url, title in reonecat.findall(page):
			if title.startswith("Episode: "):
				title = title[9:]
			list.append(encodeUrl(title))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting episodes"))
		self.working = False

	def error2(self, error=""):
		print "[Podcast] Error: Error getting podcast url"
		self.instance.setTitle(_("Error getting podcast url"))
		self.working = False

###################################################

class PodcastDePodcasts(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self.working = True
		self.url = url
		self.urls = []
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.session.open(PodcastDeEpisodes, cur)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage(self.url).addCallback(self.showPodcasts).addErrback(self.error)

	def showPodcasts(self, page):
		list = []
		idx = page.index('<h4>Podcasts</h4>')
		page = page[idx:]
		idx = page.index('</div>')
		page = page[:idx]
		reonecat = re.compile(r'alt="(.+?)" class="(.+?)<a href="(.+?)" title="(.+?)">(.+?)<span class="(.+?)"></span>', re.DOTALL)
		for title, x, url, x2, x3, type in reonecat.findall(page):
			if type.__contains__("content_type_1_icon"):
				text = _(" (Audio)")
			else:
				text = _(" (Video)")
			list.append(encodeUrl(title+text))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting podcasts"))
		self.working = False

###################################################

class PodcastDeCategories(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session, url):
		self.session = session
		Screen.__init__(self, session)
		
		self.working = True
		self.url = url
		self.urls = []
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.session.open(PodcastDePodcasts, cur)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage(self.url).addCallback(self.showCategories).addErrback(self.error)

	def showCategories(self, page):
		list = []
		idx = page.index('<h3>')
		page = page[idx:]
		idx = page.index('</div>')
		page = page[:idx]
		reonecat = re.compile(r'<a href="(.+?)" title="(.+?)">', re.DOTALL)
		for url, title in reonecat.findall(page):
			list.append(encodeUrl(title))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting categories"))
		self.working = False

###################################################

class PodcastDe(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session):
		self.session = session
		Screen.__init__(self, session)
		
		self.working = True
		self.urls = []
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.exit}, -1)
		
		self["list"] = MenuList([])
		
		self.onLayoutFinish.append(self.downloadMovies)

	def ok(self):
		if self.working == False:
			if len(self.urls) > 0:
				cur = self.urls[self["list"].getSelectedIndex()]
				self.session.open(PodcastDeCategories, cur)

	def exit(self):
		if self.working == False:
			self.close()

	def downloadMovies(self):
		getPage("http://m.podcast.de/kategorien").addCallback(self.showCategories).addErrback(self.error)

	def showCategories(self, page):
		list = []
		idx = page.index('<h3>')
		page = page[idx:]
		idx = page.index('</div>')
		page = page[:idx]
		reonecat = re.compile(r'<a href="(.+?)" title="(.+?)">', re.DOTALL)
		for url, title in reonecat.findall(page):
			list.append(encodeUrl(title))
			self.urls.append(url)
		self["list"].setList(list)
		self.working = False

	def error(self, error=""):
		print "[Podcast] Error:", error
		self.instance.setTitle(_("Error getting categories"))
		self.working = False

###################################################

class Podcast(Screen):
	skin = """
		<screen position="center,center" size="420,360" title="%s" >
			<widget name="list" position="0,0" size="420,350" scrollbarMode="showOnDemand" />
		</screen>""" % _("Podcast")

	def __init__(self, session):
		self.session = session
		Screen.__init__(self, session)
		
		self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.ok, "cancel": self.close}, -1)
		
		self["list"] = MenuList([
			_("podcast.de"),
			_("podcast.com"),
			_("from xml"),
			_("configuration")])

	def ok(self):
		cur = self["list"].getCurrent()
		if cur == _("podcast.de"):
			self.session.open(PodcastDe)
		elif cur == _("podcast.com"):
			self.session.open(PodcastCom)
		elif cur == _("from xml"):
			self.session.open(PodcastXML)
		else:
			self.session.open(PodcastConfig)

###################################################

def main(session, **kwargs):
	session.open(Podcast)

def Plugins(**kwargs):
	return PluginDescriptor(name=_("Podcast"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)