Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaronixon committed Sep 28, 2015
0 parents commit d8ef79d
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.pyo
.DS_Store
28 changes: 28 additions & 0 deletions README.md
@@ -0,0 +1,28 @@
# AutoFavourites
Everyone knows how is boring when channels change and you get a "UNKNOWN" on your Favourites. AutoFavourites Plugin can generate favourites based on a configuration file and channel names.

Installation
------------
opkg install enigma2-plugin-extensions-autofavourites.ipk

Usage
------------
Create a file /etc/autoFavourites.cfg with content as example:
```
Documentaries=Animal Planet|Discovery Channel|H2|National Geographic|The History Channel|Nat Geo
Movies and Series=AXN|Canal Brasil|Cinemax|^Fox$|^Fox HD$|FX|^HBO|I-SAT|^Max|MegaPix|Paramount|Prime Box|Sony|Space|Studio
```

Syntax is:
(Favourite Name)=(Regular expression to find channels by name)

Some examples for regular expressions:
```
^startwith
endwith$
^exact$
contains
regexp1|regexp2|regexp3
```

You can test your regular expressions and learn a little bit here http://pythex.org/.
11 changes: 11 additions & 0 deletions control
@@ -0,0 +1,11 @@
Package: enigma2-plugin-extensions-autofavourites
Version: 0.1.0
Description: Generate favourites based on a config file
Section: base
Priority: optional
Maintainer: lazaronixon
License: GNU General Public License
Architecture: mips32el
OE: enigma2-plugins
Homepage: http://github.com/lazaronixon/autofavourites
Source: http://github.com/lazaronixon/autofavourites
Empty file.
@@ -0,0 +1,138 @@
#!/usr/bin/python

import os
import sys
import glob
import re
import unicodedata
import urllib

#---------------------------------------------
lamedb = '/etc/enigma2/lamedb'
outdir = '/etc/enigma2'
rules = '/etc/autoFavourites.cfg'
#---------------------------------------------

def removeoldfiles():
userbouquets = glob.glob(outdir + '/userbouquet.*')
for userbouquet in userbouquets:
os.remove(userbouquet)

bouquetindexes = glob.glob(outdir + '/bouquets.*')
for bouquetindex in bouquetindexes:
os.remove(bouquetindex)

def reload():
f = urllib.urlopen("http://127.0.0.1/web/servicelistreload?mode=2")
s = f.read()
f.close()

def mkfavfilename(s):
s = unicode(s.replace(' ', '').lower(), 'UTF-8')
s = ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')
return 'userbouquet.' + s + '.tv'

def formatchannel(channel):
channel['channeltype'] = re.sub("^0+","",channel['channeltype']).upper()
channel['channelcode'] = re.sub("^0+","",channel['channelcode']).upper()
channel['code2'] = re.sub("^0+","",channel['code2']).upper()
channel['tpcode'] = re.sub("^0+","",channel['tpcode']).upper()

if (channel['channeltype'] == '25'):
channel['channeltype'] = '19'

return channel

def genfavindex():
favindexfile = open(outdir + '/bouquets.tv', 'w')
favindexfile.write('#NAME User - bouquets (TV)\n')

filerules = open(rules)
for rule in filerules:
favname, channellist = rule.split("=")
favfilename = mkfavfilename(favname)
favindexfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet\n' % favfilename)
favindexfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet\n')
favindexfile.close()

radioindexfile = open(outdir + '/bouquets.radio', 'w')
radioindexfile.write('#NAME User - bouquets (RADIO)\n')
radioindexfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.favourites.radio" ORDER BY bouquet\n')
radioindexfile.close()

def genfav():
filerules = open(rules)
for rule in filerules:
rule = rule.rstrip()
favname, channellist = rule.split("=")
favfilename = mkfavfilename(favname)
favfile = open(outdir + '/' + favfilename, 'w')
favfile.write("#NAME " + favname + "\n")

channels = []
prevline = None
servicesarea = False
regex = re.compile(channellist, re.IGNORECASE)
filelamedb = open(lamedb)
for line in filelamedb:
line = line.rstrip()

if (line == 'services'):
servicesarea = True

if (servicesarea and line == 'end'):
servicesarea = False

if (not servicesarea or line.startswith('p:')):
continue

if regex.search(line):
servicesplit = prevline.split(':')
channel = {
'channelname': line,
'channelcode': servicesplit[0],
'tpcode': servicesplit[1],
'code2': servicesplit[2],
'code3': servicesplit[3],
'channeltype': servicesplit[4]
}

channel = formatchannel(channel)
channels.append(channel)
prevline = line

filelamedb.close()

channels = sorted(channels, key=lambda channel: channel['channelname'])
for channel in channels:
favfile.write("#SERVICE 1:0:%(channeltype)s:%(channelcode)s:%(code2)s:1:%(tpcode)s:0:0:0:" % channel + "\n")

favfile.close()

def gendefaultfav():
favtvallfile = open(outdir + '/userbouquet.favourites.tv', 'w')
favtvallfile.write("#NAME Favourites (TV)\n")
favtvallfile.close()

favradioallfile = open(outdir + '/userbouquet.favourites.radio', 'w')
favradioallfile.write("#NAME Favourites (Radio)\n")
favradioallfile.close()

def log(msg):
statusmsg = msg
sys.stdout.write(msg)
sys.stdout.flush()

def main():
log('Removing old files...\n')
removeoldfiles()
log('Generating favorites Index...\n')
genfavindex()
log('Generating default favorites...\n')
gendefaultfav()
log('Generating favorites...\n')
genfav()
log('Reloading...\n')
reload()

main()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions usr/lib/enigma2/python/Plugins/Extensions/AutoFavourites/plugin.py
@@ -0,0 +1,65 @@
#################################################################################
#
# Plugin for Enigma2
# version:
VERSION = "0.1.0"
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#################################################################################

from Plugins.Plugin import PluginDescriptor
from Screens.Console import Console
from Screens.ChoiceBox import ChoiceBox

APP_NAME = "AutoFavourites"
AUTO_FAVOURITES = "python /usr/lib/enigma2/python/Plugins/Extensions/AutoFavourites/autoFavourites.py"

def main(session,**kwargs):
parts = [
(_("Automatic scan"), 0, session),
(_("Add to bouquet"), 1, session),
(_("Exit"), 2, session)
]

text = APP_NAME + " " + VERSION
session.openWithCallback(menuDone, ChoiceBox, title = text, list = parts)

def menuDone(option):
if option is None:
return
(description, choice, session) = option

if choice == 0:
fastScan(session)
elif choice == 1:
genFav(session)
elif choice == 2:
session.close

def fastScan(session):
from Screens.ScanSetup import *
session.open(ScanSimple)

def genFav(session):
text = APP_NAME
cmd = AUTO_FAVOURITES
session.openWithCallback(consoleClosed,Console,text,[cmd])

def consoleClosed(answer=None):
return

def Plugins(**kwargs):
return PluginDescriptor(
name="AutoFavourites",
description="Generate favourites based on a config file",
where = PluginDescriptor.WHERE_PLUGINMENU,
icon="plugin.png",
fnc=main)

0 comments on commit d8ef79d

Please sign in to comment.