added C3vocUpdater plugin
[enigma2-plugins.git] / c3vocupdater / src / plugin.py
1 # -*- coding: utf-8 -*-
2 from Screens.Screen import Screen
3 from Components.Label import Label
4 from Components.ActionMap import ActionMap
5 from Plugins.Plugin import PluginDescriptor
6 from Screens.MessageBox import MessageBox
7 from enigma import eDVBDB
8 import requests
9 import json
10 import shutil
11
12 class C3vocScreen (Screen):
13
14         skin = """
15                 <screen position="130,150" size="460,150" title="c3voc Updater" >
16                         <widget name="myText" position="10,50" size="400,40" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;22"/>
17                         <widget name="myGreenBtn" position="10,110" size="110,40" backgroundColor="green" valign="center" halign="center" zPosition="2" foregroundColor="white" font="Regular;20"/>
18                         <widget name="myRedBtn" position="130,110" size="110,40" backgroundColor="red" valign="center" halign="center" zPosition="2" foregroundColor="white" font="Regular;20"/>
19                 </screen>"""
20
21         def  __init__ (self, session, args = 0 ):
22                 self.session = session
23                 Screen.__init__(self, session)
24
25                 self["myText"] = Label(_("Update the c3voc stream bouquet?"))
26                 self["myRedBtn"] = Label(_("Cancel"))
27                 self["myGreenBtn"] = Label(_("OK"))
28                 self["myActionsMap"] = ActionMap(["SetupActions", "ColorActions"],
29                         {
30                                 "ok": self.getinput,
31                                 "green": self.getinput,
32                                 "red": self.close,
33                                 "cancel": self.close,
34                         }, -1)
35
36         def getinput(self):
37                 try:
38                         req = requests.session()
39                         page = req.get("https://streaming.media.ccc.de/streams/v2.json", headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'})
40                         data = json.loads(page.content)
41
42                         with open("/tmp/c3voc", "a") as myfile:
43                                 myfile.write("#NAME c3voc (TV)\n")
44                                 myfile.close()
45
46                         for conference in data:
47                                 conference_name = conference["conference"]
48                                 rooms = self.get_rooms_for_group(conference["groups"], "Lecture Rooms")
49                                 if not rooms:
50                                         continue
51
52                                 for room in rooms:
53                                         schedule_name = room["schedulename"]
54                                         url = self.get_hls_url(room["streams"], "hd-native")
55                                         with open("/tmp/c3voc", "a") as myfile:
56                                                 myfile.write("#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s\n#DESCRIPTION %s, %s\n" % (url.replace(":", "%3a"), conference_name, schedule_name))
57                                                 myfile.close()
58
59                         if 'c3voc' not in open('/etc/enigma2/bouquets.tv').read():
60                                 with open("/etc/enigma2/bouquets.tv", "a") as myfile:
61                                         myfile.write("#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet.c3voc__tv_.tv\" ORDER BY bouquet")
62                                         myfile.close()
63
64                         shutil.move("/tmp/c3voc","/etc/enigma2/userbouquet.c3voc__tv_.tv")
65                         eDVBDB.getInstance().reloadBouquets()
66                         self.session.open(MessageBox, text = _("c3voc stream bouquet updated"), type = MessageBox.TYPE_INFO, timeout=4)
67                 except:
68                         pass
69
70                 self.close()
71
72         def get_rooms_for_group(self, groups, group_title):
73                 for group in groups:
74                         if group["group"] != group_title:
75                                 continue
76                         return group["rooms"]
77
78         def get_hls_url(self, streams, slug):
79                 for stream in streams:
80                         if stream["slug"] != slug:
81                                 continue
82                         return stream["urls"]["hls"]["url"]
83
84 def main(session, **kwargs):
85         session.open(C3vocScreen)
86
87 def Plugins(**kwargs):
88         return PluginDescriptor(name="C3vocUpdater", description="update the c3voc stream bouquet", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main)