
#\c           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z y d d l j j Z Wn# e k
 rd d l j j Z n Xy d d l m Z Wn e k
 rCe Z n Xd d l m Z d d l m  Z  g  Z! d Z" d	 Z# d
 Z$ d Z% d Z& d Z' d Z( e j) j* e' d  Z+ d Z, e- Z. d Z/ d e0 f d     YZ1 d e j2 f d     YZ3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 d   Z: d   Z; d   Z< d   Z= d   Z> d    Z? d! f  d"     YZ@ d# f  d$     YZA d% f  d&     YZB d' f  d(     YZC e d)  ZD eE d* k re& rd+ Z( d, Z' d- Z, e j) j* e' d  Z+ n  e jF eD    n eG Z. d S(.   s   
e2m3u2bouquet.e2m3u2bouquet -- Enigma2 IPTV m3u to bouquet parser

@author:     Dave Sully, Doug Mackay
@copyright:  2017 All rights reserved.
@license:    GNU GENERAL PUBLIC LICENSE version 3
@deffield    updated: Updated
iN(   t   Image(   t   OrderedDict(   t   eDVBDB(   t   ArgumentParser(   t   RawDescriptionHelpFormatters   0.8.2s
   2017-06-04s
   2018-12-26i    s   /etc/enigma2/s   /etc/epgimport/s   e2m3u2bouquet/s   /usr/share/enigma2/picon/s   #SERVICE 1:832:d:0:0:0:0:0:0:0:t   CLIErrorc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s:   Generic exception to raise and log different fatal errors.c         C   s*   t  t  j t |    d | |  _ d  S(   Ns   E: %s(   t   superR   t   __init__t   typet   msg(   t   selfR	   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   :   s    c         C   s   |  j  S(   N(   R	   (   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   __str__>   s    c         C   s   |  j  S(   N(   R	   (   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   __unicode__A   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   8   s   		t   AppUrlOpenerc           B   s   e  Z d  Z d Z RS(   s!   Set user agent for downloads
    sf   Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36(   R   R   R   t   version(    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   E   s   c           C   s0   d GHd j  t  GHt t j j    GHd GHd  S(   Ns!   
********************************s"   Starting Enigma2 IPTV bouquets v{}s!   ********************************
(   t   formatt   __version__t   strt   datetimet   now(    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   display_welcomeK   s    c           C   s1   d GHd GHd GHd GHd GHd GHd GHd GHd	 GHd  S(
   Ns!   
********************************s    Enigma2 IPTV bouquets created ! s    ********************************s   
To enable EPG datas"   Please open EPG-Importer plugin.. s-   Select sources and enable the new IPTV sources>   (will be listed as under 'IPTV Bouquet Maker - E2m3u2bouquet')sE   Save the selected sources, press yellow button to start manual importsG   You can then set EPG-Importer to automatically import the EPG every day(    (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   display_end_msgQ   s    c          C   sC   y t  j t  Wn+ t k
 r> }  |  j t j k r?   q? n Xd S(   s-   create config folder if it doesn't exist
    N(   t   ost   makedirst   CFGPATHt   OSErrort   errnot   EEXIST(   t   e(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   make_config_folder\   s
    c          C   s  d GHyd GHxm t  j t  D]\ }  d |  k rN t  j t  j j t |    q d |  k r t  j t  j j t |    q q Wd GHt  j j t  r xE t  j t  D]1 }  d |  k r t  j t  j j t |    q q Wn  d GHt  j t  j j t d  t  j j t d   t	 t  j j t d  d	  } t	 t  j j t d   } x* | D]" } d
 | k rO| j
 |  qOqOW| j   | j   Wn t k
 r} d GH  n Xd GHd S(   s;   Clean up routine to remove any previously made changes
    s   ----Running uninstall----s   Removing old IPTV bouquets...s   userbouquet.suls_iptv_s   bouquets.tv.baks    Removing IPTV custom channels...t
   suls_iptv_s*   Removing IPTV bouquets from bouquets.tv...s   bouquets.tvs   w+s   .suls_iptv_s   Unable to uninstalls   ----Uninstall complete----N(   R   t   listdirt
   ENIGMAPATHt   removet   patht   joint   isdirt   EPGIMPORTPATHt   renamet   opent   writet   closet	   Exception(   t   fnamet   tvfilet   bakfilet   lineR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   uninstallerf   s4    #&.
c         C   s6   |  | k r2 | |  j  d t  r. | |  d S|  S|  S(   s4   Return the title override if set else the title
    t   nameOverride(   t   gett   False(   t   catt   category_options(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   get_category_title   s    &c         C   s"   |  j  d t  r |  d S|  d S(   s4   Return the title override if set else the title
    R3   s   stream-name(   R4   R5   (   t   channel(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   get_service_title   s    c           C   sB   t  s> d GHt r) t j   j   d GHq> t j d  d GHn  d  S(   Ns   
----Reloading bouquets----s   bouquets reloaded...sK   wget -qO - http://127.0.0.1/web/servicelistreload?mode=2 > /dev/null 2>&1 &(   t   TESTRUNR   t   getInstancet   reloadBouquetsR   t   system(    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   reload_bouquets   s    c         C   s   |  j  d d  S(   s   Can't have -- in xml commentss   --s   - - (   t   replace(   t   string(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   xml_safe_comment   s    c         C   s@   |  j  d d  j  d d  j  d d  j  d d  j  d	 d
  S(   Nt   &s   &amp;s   "s   &quot;t   's   &apos;t   <s   &lt;t   >s   &gt;(   R@   (   RA   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt
   xml_escape   s
    c         C   sF   |  j  d d  j    j  d d  j  d d  j  d d  j  d	 d
  S(   Ns   &quot;t   "s   &apos;RD   s   &lt;RE   s   &gt;RF   s   &amp;RC   (   R@   (   RA   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   xml_unescape   s
    c         C   s   |  j  d d  j  d d  } t |  t k rB | j d  } n  t j d t | d   j d d  } t j d	 d
 | j    } | S(   s&   Convert filename to safe filename
    t    t   _t   /s   utf-8t   NFKDt   utf_8t   ASCIIt   ignores   [^a-z0-9-_]t    (	   R@   R   t   unicodet   encodet   unicodedatat	   normalizet   ret   subt   lower(   t   filenamet   name(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   get_safe_filename   s    'c         C   s  t  d |  d t  } | j d  } | j d d d d d d	 d
 d | j d d d d d d	 d
 d | j d  } | j d d d d d d	 d
 d | j d d d d d d	 d
 d | j d d d d d d	 d
 d | j d d d d d d  d
 d! | j d" d# d d$ d d	 d% t d
 d& | j d' d( d d) d d	 d% t d
 d* | j d+ d, d d- d d  d
 d. | j d/ d0 d d1 d d  d
 d2 | j d3 d4 d d5 d d  d
 d6 | j d7 d8 d d9 d d	 d
 d: | j d; d< d d= d d  d
 d> | j d? d@ d dA d d	 d
 dB | j dC dD d dE d d  d
 dF | j dG dH d dI d d  d
 dJ | j dK dL d dM d d  d
 dN | j dO dP d dQ dQ | | S(R   Nt   descriptiont   formatter_classs   URL Based Setups   -ms   --m3uurlt   destt   m3uurlt   actiont   storet   helps(   URL to download m3u data from (required)s   -es   --epgurlt   epgurls&   URL source for XML TV epg data sourcess   Provider Based Setups   -ns   --providernamet   providernames2   Host IPTV provider name (e.g. FAB/EPIC) (required)s   -us
   --usernamet   usernames   Your IPTV username (required)s   -ps
   --passwordt   passwords   Your IPTV password (required)s   -is   --iptvtypest	   iptvtypest
   store_truesQ   Treat all stream references as IPTV stream type. (required for some enigma boxes)s   -sttvs   --streamtype_tvt   sttvR   sC   Stream type for TV (e.g. 1, 4097, 5001 or 5002) overrides iptvtypess   -stvods   --streamtype_vodt   stvodsA   Stream type for VOD (e.g. 4097, 5001 or 5002) overrides iptvtypess   -Ms
   --multivodt   multivods6   Create multiple VOD bouquets rather single VOD bouquets   -as   --allbouquett
   allbouquets   Create all channels bouquets   -Ps   --piconst   piconssE   Automatically download of Picons, this option will slow the executions   -qs
   --iconpatht   iconpathsR   Option path to store picons, if not supplied defaults to /usr/share/enigma2/picon/s   -xss   --xcludesreft
   xcludesrefs5   Disable service ref overriding from override.xml files   -bs   --bouqueturlt
   bouqueturlsD   URL to download providers bouquet - to map custom service referencess   -bds   --bouquetdownloadt   bouquetdownloadsO   Download providers bouquet (use default url) - to map custom service referencess   -bts   --bouquettopt
   bouquettops   Place IPTV bouquets at tops   -Us   --uninstallt	   uninstalls)   Uninstall all changes made by this scripts   -Vs	   --versionR   (   R   R   t   add_argument_groupt   add_argumentt   int(   t   program_licenset   program_version_messaget   parsert   urlgroupt   providergroup(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   get_parser_args   sN    !!t   Statusc           B   s   e  Z e Z d  Z RS(   RQ   (   R   R   R5   t
   is_runningt   message(    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR}      s   t   ProviderConfigc           B   s   e  Z d    Z RS(   c         C   s   d |  _  t |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ t |  _	 d |  _
 d |  _ t |  _ t |  _ t |  _ d |  _ t |  _ d |  _ t |  _ t |  _ d |  _ d  S(   NRQ   i    (   RZ   R5   t   enabledt   settings_levelt   m3u_urlt   epg_urlRe   Rf   t   provider_update_urlt
   iptv_typest   streamtype_tvt   streamtype_vodt	   multi_vodt   all_bouquetRm   t	   icon_patht   sref_overridet   bouquet_urlt   bouquet_downloadt   bouquet_topt   last_provider_update(   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR      s(    																			(   R   R   R   (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR      s   t   Providerc           B   s"  e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! RS(    c         C   sO   d |  _  d  |  _ d  |  _ g  |  _ i  |  _ t   |  _ d  |  _ | |  _	 d  S(   NRQ   (
   t   _panel_bouquet_filet   Nonet   _panel_bouquett	   _m3u_filet   _category_ordert   _category_optionsR   t   _dictchannelst   _xmltv_sources_listt   config(   R
   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR     s    						c   	      C   st  | rp| j  d  s' d j |  } n  |  j |  } t j j |  j j |  } t t j j	 t
 j
 | d   } | spt r d GHd j |  GHn& t s t j j d  t j j   n  yj t j |  } | j   } | j   | j d k rt j | |  n t rd GHn  |  j |  d  SWn5 t k
 r\} t rKd	 | f GHn  |  j |  d  SX|  j |  qpn  d  S(
   Nt   https	   http://{}t   *s$   Picon file doesn't exist downloadings   PiconURL: {}t   .t   images'   Download Picon - not an image, skippings   Download picon urlopen error(   t
   startswithR   t   _get_picon_nameR   R%   R&   R   R   t   filtert   isfilet   globt   DEBUGt   IMPORTEDt   syst   stdoutR+   t   flusht   urllibt   urlopent   infoR,   t   maintypet   urlretrievet   _picon_create_emptyR-   t   _picon_post_processing(	   R
   t   logo_urlt   titlet	   piconnamet   picon_file_patht   existingpicont   responseR   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _download_picon_file  s:    "
c         C   s   t  | d d  j   d S(   sI   
        create an empty picon so that we don't retry this picon
        s   .Nonet   aN(   R*   R,   (   R
   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   :  s    c         C   se  d } y t  j |  } Wn8 t k
 rS } t rB d | | f GHn  |  j |  d SX| d k	 r| d k	 rt rz d GHn  y& t j |  j d j	 | d   Wn5 t k
 r } t r d | f GHn  |  j |  d SXy t
 j |  Wqat k
 r} t rd | f GHn  d SXnJ y  t
 j | d j	 | |   Wn' t k
 r`} t rad	 | f GHqan Xd S(
   sM   Check type of image received and convert to png
        if necessary
        RQ   s/   Picon post processing - not an image or no fileNt   pngs   Converting Picon to pngs   {}.{}s/   Picon post processing - unable to convert images5   Picon post processing - unable to remove non png files.   Picon post processing - unable to rename file (   t   imghdrt   whatR-   R   R   R   R    R*   t   saveR   R   R$   R)   (   R
   R   t   extR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   @  s:    & c         C   s   | } t  |  t k r* | j d  } n  t j d t | d   j d d  } t j d d | j d d	  j d
 d  j d d  j    } | S(   s9   Convert the service name to a Picon Service Name
        s   utf-8RM   RN   RO   RP   s   [\W]RQ   RC   t   andt   +t   plusR   t   star(	   R   RR   RS   RT   RU   RV   RW   R@   RX   (   R
   t   service_nameRZ   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   g  s    'c         C   s[  i  |  _  t j j |  j  rWt |  j d  	} x | D] } d | k r: | j   j d  } t |  d k r1| d j	 d  } | d k r.| d t | d  k r.| d | d } d j
 | d | d	 | d
 | d | d | d | d | d | d f	  } | d k r+| |  j  | <q+q.q1q: q: WWd QXt sWt j |  j  qWn  d S(   s>   Check providers bouquet for custom service references
        t   rs   #SERVICEt   :i   i
   RL   ii   i   i   i   i   i   i   i   i	   s   0:1:0:0:0:0:0:0:0N(   R   R   R%   R   R   R*   t   stript   splitt   lent   rfindR&   R   R$   (   R
   t   fR1   t   servicet   post   keyt   value(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _parse_panel_bouquett  s     	&)%#c         C   s  t  j  | d  } t j j | j  \ } } t j d | j  } | j j d  s | j j d  s | r | r | d j d  r |  j j	 r d n d | d	 <|  j j
 rt |  j j
  | d	 <qn? d
 j | d  | d <|  j j s d n t |  j j  | d	 <d S(   s0   Set the stream types and VOD categories
        s
   stream-urls   \.[^/]+\.m3u8$s   .tss   .m3u8s   group-titlet   VODt   4097t   1s   stream-typeu   VOD - {}N(   t   urlparseR   R%   t   splitextRV   t   searcht   endswithR   R   R   R   R   R   R   (   R
   t   service_dictt   parsed_stream_urlt   rootR   t   is_m3u8_vod(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _set_streamtypes_vodcats  s    $c         C   s  g  } |  j    } | r|  j d  d j t j  GHyt j d |  } x\| j d  D]K} i  } | j j	 d  } t
 |  t k	 r | j d  } n  | j j	 d d  } t
 |  t k	 r | j d  } n  | | d <| j j	 d	 d
  j   rt | j j	 d	 d
   n d | d	 <| j j	 d t  d k | d <| j |  | j j	 d t  d k } | rt | d <| |  j k rg  |  j | <qn  | |  j | <q[ W|  j d  t j GHWqt k
 r}	 d }
 |
 GHt r|
  qqXn  | S(   sM   Check for bouquets within mapping override file and applies if found
        s$   ----Parsing custom bouquet order----s   
t   files   .//categoryRZ   s   utf-8R3   RQ   t   idStartt   0i    R   t   truet   customCategorys   custom bouquet order applied...s   Corrupt override.xml file(   t   _get_mapping_filet   _update_statusR   R}   R   t   ETt   ElementTreet   findallt   attribR4   R   RR   t   decodet   isdigitRv   t   Truet   appendR5   R   R   R-   R   (   R
   t   category_ordert   mapping_filet   treet   nodet
   dictoptiont   categoryt   cat_title_overridet   is_custom_categoryR   R	   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _parse_map_bouquet_xml  sB    
@
c            s   j    } | r j d  d j t j  GHyt j d |  } d } x j D]} | j d  sU g  } g  } x | j	 d j |   D] } | j
 j d   | j
 j d    d$ } y0 t     f d	    j   D d$  } Wn t k
 rn X| d$ k	 r  j | j  j   j |   q q Wx&  j | D] }	 | j |	 d
  qHWx | j	 d j |   D]v } | j
 j d    d k rd t |   | j    j | j i  d
 6 | d 7} n  | j   q|W| j |  t d   | D  j   } d   t |  D   j | j d  f d    qU qU W j d  t j GH| j d  }
 x|
 D]} | j
 j d   | j
 j d    | j
 j d   d$ } d$ }  ry0 t    f d    j  D d$  } Wqt k
 rqXn   rC| d$ k	 rC j j   } n  j j    } | d$ k	 r d k rx,| D]!}	 |	 d
  k rt| j
 j d  d k rt |	 d <n  | j
 j d d  |	 d <| j
 j d d  |	 d <| j
 j d |	 d  |	 d <| j
 j d d$  rI j j rI| j
 j d |	 d  |	 d <t |	 d <n  | j
 j d |	 d  |	 d <| j
 j d   d! k } | rd |	 d <n  PqtqtWqqW j d"  t j GHWqt k
 r} d# } | GHt r|  qqXn  d$ S(%   sK   Check for channels within mapping override file and apply if found
        s7   ----Parsing custom channel order, please be patient----s   
{}R   i    R   u"   .//channel[@categoryOverride="{}"]RZ   R   c         3   s5   |  ]+ } | d   k r  j    j |  Vq d S(   s   stream-nameN(   R   t   index(   t   .0t   item(   R   t	   node_nameR
   (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    s   stream-nameu   .//channel[@category="{}"]t   placeholdert   placeholder_i   c         s   s   |  ] } | t  f Vq d  S(   N(   R   (   R   t   x(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    c         S   s   i  |  ] \ } } | |  q S(    (    (   R   R   R9   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys
   <dictcomp>  s   	 R   c            s     |  d S(   Ns   stream-name(    (   R   (   t   channel_order_dict(    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   <lambda>  s    s   custom channel order applied...R9   t   categoryOverridec         3   s5   |  ]+ } | d   k r  j    j |  Vq d S(   s   stream-nameN(   R   R   (   R   R   (   t   category_overrideRZ   R
   (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    R   t   falseR3   RQ   s   tvg-idt
   serviceReft   serviceRefOverridet	   streamUrls
   stream-urlt   clearStreamUrlR   s   custom overrides applied...s   Corrupt override.xml fileN(   R   R   R   R}   R   R   R   R   R   R   R   R4   R   t   nextt   KeyErrorR   t   popR   t   extendR   t   keyst	   enumeratet   sortt   iterR5   R   R   R   R-   R   (   R
   R   R   t   iR6   t   sortedchannelst   listchannelsR   t   channel_indexR   t   channel_nodest   override_channelt   channels_listt   clear_stream_urlR   R	   (    (   R   R   R   RZ   R   R
   sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _parse_map_channels_xml  s    #+'#!c         C   s   d  } t j j t t |  j j  d  t j j t j   t |  j j  d  g } x* | D]" } t j j	 |  r] | } Pq] q] W| S(   Ns   -sort-override.xml(
   R   R   R%   R&   R   R[   R   RZ   t   getcwdR   (   R
   R   t   search_pathR%   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   :  s    ".c         C   s   | d j  d  sw | j d j | d t j | d  t |  j d    | j d j t |  j d    n | j d j t   d	 S(
   s$   Add service to bouquet file
        s   stream-nameR   s   #SERVICE {}:{}:{}
R   s
   stream-urls   utf-8s   #DESCRIPTION {}
s   {}
N(   R   R+   R   R   t   quoteR:   RS   t   PLACEHOLDER_SERVICE(   R
   R   R9   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _save_bouquet_entryD  s    (c         C   s   d j  | |  S(   Ns\   #SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "userbouquet.suls_iptv_{}_{}.tv" ORDER BY bouquet
(   R   (   R
   t   cat_filenamet   provider_filename(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _get_bouquet_index_nameO  s    c         C   s   |  j    } | r t t j j t d  d   } | j d  |  j j r x | D] } | j |  qS WxZ | D] } | j |  qq Wn< x | D] } | j |  q Wx | D] } | j |  q WWd QXn  d S(   s)   Add to the main bouquets.tv file
        s   bouquets.tvt   ws   #NAME Bouquets (TV)
N(	   t   _get_current_bouquet_indexesR*   R   R%   R&   R#   R+   R   R   (   R
   t   iptv_bouquetst   current_bouquet_indexesR   t   bouquet(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _save_bouquet_index_entriesS  s    !c         C   s   g  } t  t j j t d  d  ^ } xT | D]L } | j d  rI q. q. d j t |  j j	   | k r. | j
 |  q. q. WWd QX| S(   s9   Get all the bouquet indexes except this provider
        s   bouquets.tvR   s   #NAMEs   .suls_iptv_{}N(   R*   R   R%   R&   R#   R   R   R[   R   RZ   R   (   R
   t   current_bouquets_indexesR   R1   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR  g  s    !!c      	   C   sW  |  j  d  d j t j  GHg  } t d   |  j D  } d j |  j j  } t |  } t |  j j  } t	 j
 j t d j | |   } t r d j |  GHn  t | d  l} | j d j |  j j j d	  | j d	    x* t d
  D] } | j d j t   q Wd }	 x |  j D] }
 |
 |  j k r(|
 | k rt |
 |  j  } | j d  | j d j | j d	    xT |  j |
 D]E } | j d  s| d j d  r|  j | |  n  |	 d 7}	 qWx7 |	 d
 d k	 r| j d j t   |	 d 7}	 qWqq(q(WWd QX| j |  j | |   |  j  d  t j GH| S(   s0   Create the Enigma2 all channels bouquet
        s%   ----Creating all channels bouquet----s   
{}c         s   s$   |  ] } | j  d   r | Vq d S(   s   VOD -N(   R   (   R   R6   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>}  s    s   {} All Channelss   userbouquet.suls_iptv_{}_{}.tvs   Creating: {}s   w+s   #NAME {} - {}
s   utf-8id   s   {}
i   s   #SERVICE 1:64:0:0:0:0:0:0:0:0:
s   #DESCRIPTION {}
R   s   stream-nameR   i    Ns    all channels bouquet created ...(   R   R   R}   R   t   listR   R   RZ   R[   R   R%   R&   R#   R   R*   R+   RS   t   xrangeR  R   R8   R   R4   R   R  R   R  (   R
   t   bouquet_indexest   vod_categoriest   bouquet_nameR  R  t   bouquet_filepathR   R
  t   channel_numR6   t	   cat_titleR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _create_all_channels_bouquetu  sB    1"c   	   	   C   sw  d } | r$ d j  |  j j |  n	 |  j j } t j j t d j  t |  j j    } t j j t d j  t |    } t t j j t |  d   } | j	 d  | j	 d j  |   | j	 d j  d	 | |   | j	 d
 j  d | t
 |    x+ | D]# } | j	 d j  d | |   q	W| j	 d j  d	 |   | j	 d j  |   | j	 d  Wd QXd S(   s(   Create epg-importer source file
        s     s   {} - {}s   suls_iptv_{}_channels.xmls   suls_iptv_{}.sources.xmls   w+s
   <sources>
sA   {}<sourcecat sourcecatname="IPTV Bouquet Maker - E2m3u2bouquet">
s6   {}<source type="gen_xmltv" nocheck="1" channels="{}">
i   s    {}<description>{}</description>
i   s   {}<url><![CDATA[{}]]></url>
s   {}</source>
s   {}</sourcecat>
s   </sources>
N(   R   R   RZ   R   R%   R&   R(   R[   R*   R+   RG   (	   R
   t   sourcest   groupt   indentt   source_namet   channels_filenamet   source_filenameR   t   source(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _create_epgimport_source  s     **!#!c         C   s*   t  j |  j j | j d   j   d  S(   s<   Generate 32 bit category id to help make service refs uniques   utf-8i   (   t   hashlibt   md5R   RZ   RS   t	   hexdigest(   R
   R6   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _get_category_id  s    c         C   s   |  j  d  k	 S(   N(   R   R   (   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _has_m3u_file  s    c         C   s   d j  |  j j |  t _ d  S(   Ns   {}: {}(   R   R   RZ   R}   R   (   R
   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR     s    c   
      C   sF  t  } t  } t j   } t j j | d j |  j j   } |  j	 d  d j t
 j  GHd |  j j f GHy2 t j   } t j |  j j | d | t } Wn t k
 r n X| sy t j |  j j |  Wqt k
 r} d t |  f GHd | f GHqXn  t j j |  rBy t | d   } | j   j   } Wd	 QX| ri | j d
  d d 6| j d
  d d 6| j d
  d d 6}	 |	 j d  r|	 j d  r|	 d |  j _ |	 d |  j _ |	 j d |  j j  |  j _ t t j    |  j _ t } qn  Wn t k
 r(} d GHn Xt  sBt j! |  qBn  | S(   s&   Download provider update file from urls   provider-{}-update.txts)   ----Downloading providers update file----s   
{}s   provider update url = t   contexts.   [e2m3u2b] process_provider_update error. Type:s)   [e2m3u2b] process_provider_update error: R   Nt   ,i    RZ   i   t   m3ui   t   epgsM   [e2m3u2b] _process_provider_update error unable to read providers update file("   R5   t   tempfilet
   gettempdirR   R%   R&   R   R   RZ   R   R}   R   R   t   sslt   _create_unverified_contextR   R   R   R-   R   R   R*   t   readlineR   R   R4   R   R   Rv   t   timeR   t
   IndexErrorR   R$   (
   R
   t
   downloadedt   updatedR%   RY   R8  R   R   R1   t   provider_tmp(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   _process_provider_update  sN    $
	c         C   s  t  t _ |  j j d  k r* d |  j _ n  |  j j d  k sH t d k rW t |  j _ n  |  j j	 d  k rx d |  j _	 n  |  j j
 d  k s |  j j d  k r |  j   n  |  j j j d t j |  j j
   j d t j |  j j   |  j _ |  j j j d t j |  j j
   j d t j |  j j   |  j _ |  j j r|  j j r|  j j j d t j |  j j
   j d t j |  j j   |  j _ n  |  j j r'|  j j r'|  j j j d  } | d k r'|  j j d | d	 !d
 j t j |  j j
  t j |  j j   |  j _ q'n  |  j j r@|  j   n  |  j   |  j   r|  j   |  j   |  j   |  j j r|  j   n  |  j   |  j d  d j t j  GH|  j    |  j d  t j GHn  t! t _ d  S(   Ns6   http://www.vuplus-community.net/rytec/rytecxmltv-UK.gzi   t   E2m3u2Bouquett   USERNAMEt   PASSWORDs   get.phpii    i   s0   ?username={}&password={}&type=dreambox&output=tss%   ----Creating EPG-Importer config ----s   
{}s   EPG-Importer config created...("   R   R}   R~   R   R   R   R   R;   t
   PICONSPATHRZ   Re   Rf   t   extract_user_details_from_urlR   R@   R   t
   quote_plusR   R   t   findR   t   download_panel_bouquett   download_m3uR7  t	   parse_m3ut   parse_map_xmltvsources_xmlt   save_map_xmlRm   t   download_piconst   create_bouquetsR   R   t   create_epgimporter_configR5   (   R
   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   process_provider  sD    	$HHK7





c         C   s2   |  j  j r. |  j  j r. |  j  j r. |  j   St S(   N(   R   R   Re   Rf   RF  R5   (   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   provider_update3  s    $
c         C   s   |  j  j r t j |  j  j  } t j | j  j d  } | rU | d |  j  _ n  t j | j  j d  } | r | d |  j  _ q n  d S(   s)   Extract username & password from m3u_url Re   i    Rf   N(   R   R   R   t   parse_qst   queryR4   Re   Rf   (   R
   t   parsedt   username_paramt   password_param(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRK  8  s    c         C   s   t  j   } t j j | d  } |  j d  d j t j  GHt	 r\ d j |  j
 j  GHn  y t j |  j
 j |  Wn. t k
 r } |  j d  t j GHd } n X| |  _ d S(   s   Download m3u file from urls   e2m3u2bouquet.m3us   ----Downloading m3u file----s   
{}s   m3uurl = {}s$   Unable to download m3u file from urlN(   R<  R=  R   R%   R&   R   R   R}   R   R   R   R   R   R   R-   R   R   (   R
   R%   RY   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRO  C  s    
c      
   C   s  |  j  d  d j t j  GHy< t j j |  j  sY d } | GHt rY t	 |   qY n  Wn$ t	 k
 r } | GHt r   q n Xi  } t
 |  j d  ^} xT| D]L} d | k r q q d | k r:i d d 6d d	 6d d
 6d d 6d d 6d d 6t d 6d d 6d d 6d d 6t d 6} | j d  d k rRd } | GHt rRt	 |   qRn  | j d  } | d j d  } | d | | d <xR t d t |  d d  D]4 } | | d j d  | | | j   j d  <qW| j d  }	 |	 d k r| |	 d j   j d  | d <n  | d d k rd | d <qq d | k s^d  | k s^d! | k r d | k rd } | GHt rt	 |   qn  | j   | d <|  j |  | d |  j k r| g |  j | d <q|  j | d j |  q q WWd" QX|  j   }
 |  j j   |  _ |
 j |  j  t d#   |
 D  j   |  _ |  j   d$ } d% } d& } | } x&|  j D]} | } | |  j k rp| j d'  s;| |  j k r|  j | d( d k r|  j | d( } q| |  j | d( <n i | d( 6|  j | <xl|  j | D](} |  j  |  } d) j | | d*  | d*  } | d j d+  s*|  j! r| j" d  r| d j d,  } | d k r| d t | d  k r| d | d } | |  j! k rd- j | d. |  j! |  | d <qqqn  | j" d  sd/ j | d. |  | d <n  | d 7} qt# | d <qWqpx2 |  j | D]  } d0 j | d. |  | d <qIWn  x | | k  r| | 7} qsWqrWd" } d' |  j k r|  j j% d'  } n9 x6 |  j D]+ } | j d'  r|  j j% |  } PqqW| d" k	 rt& d1   |  j D  } t |  rg  |  j D] } | | k r/| ^ q/|  _ | |  j | | +y |  j j' d'  Wqt( k
 rqXqn  t r|t) r|t
 t j j* t+ d2  d3  } x |  j D] } | |  j k rx |  j | D] } d } xZ | j,   D]L \ } } t- |  t. k r3| t/ |  d4 7} q| | j0 d  d4 7} qW| j1 d5 j |   qWqqW| j2   n  |  j  d6  t j GHt st j j3 |  j  rt j' |  j  qn  d" S(7   s   core parsing routines   ----Parsing m3u file----s   
{}s,   M3U file is empty. Check username & passwordR   t   EXTM3Us   EXTINF:RQ   s   tvg-ids   tvg-names   tvg-logos   group-titles   stream-names
   stream-urlR   R3   R   R   R   s   tvg-isH   No extended playlist info found. Check m3u url should be 'type=m3u_plus'RH   i    RJ   i   i   s   utf-8s    =R9  u   Nones   http:s   https:s   rtmp:Nc         s   s   |  ] } | t  f Vq d  S(   N(   R   (   R   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    i  iЄ  i   R   R   s   {:x}:{}:{}:0i   R   RL   s   {}:{}s   stream-types   {}:0:1:{}:0:0:0s   {}:0:1:{:x}:0:0:0:0:0:0c         s   s$   |  ] } | j  d   r | Vq d S(   s   VOD -N(   R   (   R   R6   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    s   channels.debugs   w+R   s   {}
s   Completed parsing m3u data...(4   R   R   R}   R   R   R%   t   getsizeR   R   R-   R*   R   R5   RM  R   R#  R   R   RX   R   R   R   R   R   R   R  R   R  R   R  R   R   R6  R   R4   R  R   R   R"  R$   t
   ValueErrorR;   R&   R   t   itemsR   t   boolR   RS   R+   R,   R   (   R
   R	   R   R   R   R1   R9   R   R
  t   stream_name_post   sorted_categoriest   vod_service_idt   serviceid_startt   category_offsett   catstartnumR6   t   numR   t   cat_idt   service_reft   m3u_stream_filet	   vod_indexR   t   vodcategoriest   datafilet   linevalsR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRP  T  s    

&#2$$%
&!!+
!c         C   s   t  j   } t j j | d  } |  j d  d j t j  GHt	 r\ d j |  j
 j  GHn  y t j |  j
 j |  Wn- t k
 r } d } | GHt	 r |  q n X| |  _ |  j   d S(   s-   Download panel bouquet file from url
        s   userbouquet.panel.tvs)   ---Downloading providers bouquet file----s   
{}s   bouqueturl = {}s/   Unable to download providers panel bouquet fileN(   R<  R=  R   R%   R&   R   R   R}   R   R   R   R   R   R   R-   R   R   (   R
   R%   RY   R   R	   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRN     s    	c         C   s   |  j  d  d j t j  GHd GHy t j |  j j  Wn+ t k
 rg } | j	 t	 j
 k rh   qh n Xxk |  j D]` } | j d  sr xH |  j | D]6 } | d j d  s |  j | d t |   q q Wqr qr W|  j  d  d j t j  GHd	 GHd  S(
   Ns2   ----Downloading Picon files, please be patient----s   
{}s/   If no Picons exist this will take a few minutesR   s   stream-nameR   s   tvg-logos   Picons download completed...s-   Box will need restarted for Picons to show...(   R   R   R}   R   R   R   R   R   R   R   R   R   R   R   R:   (   R
   R   R6   R   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRS    s     
(c   	      C   s   i  |  _  |  j   } | r yv t j d |  } x] | j d  D]L } | j j d  } g  } x | D] } | j | j  qe W| |  j  | <q@ WWq t	 k
 r } d } | GHt
 r |  q q Xn  d S(   sA   Check for a mapping override file and parses it if found
        R   s   .//xmltvextrasources/groupt   ids   Corrupt override.xml fileN(   R   R   R   R   R   R   R4   R   t   textR-   R   (	   R
   R   R   R,  t
   group_namet   urllistt   urlR   R	   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRQ  '  s     	c         C   s8
  t  j j t t |  j j  d  } d } t } |  j r4
t	 | d  	} | j
 d  | j
 d j |   | j
 d j |   | j
 d j |   | j
 d j |   | j
 d	 j |   | j
 d
 j | t |  j j    | j
 d  | j
 d  | j
 d j |   |  j s| j
 d j d |   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d |   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d | d   | j
 d j d |   | j
 d j d | d    | j
 d j d | d!   | j
 d j d | d"   | j
 d j d | d#   | j
 d j d | d$   | j
 d j d | d%   | j
 d j d |   | j
 d j d | d&   | j
 d j d | d'   | j
 d j d | d(   | j
 d j d | d)   | j
 d j d | d*   | j
 d j d | d+   | j
 d j d |   | j
 d j d | d,   | j
 d j d | d-   | j
 d j d | d.   | j
 d j d | d/   | j
 d j d | d0   | j
 d j d | d1   | j
 d j d |   | j
 d j d | d2   | j
 d j d | d3   | j
 d j d | d4   | j
 d j d | d5   | j
 d j d | d6   | j
 d j d | d7   | j
 d j d |   | j
 d8 j d |   n x |  j D]~ } | j
 d j d | t |    x8 |  j | D]) } | j
 d j d | t |    qMW| j
 d j d |   qW| j
 d9 j |   | j
 d: j |   x|  j D]x} | |  j k r| j d;  s|  j | j d< d=  } | j
 d> j d | t |  j d?  t |  j d?  |  j | j d@ d=  t |  j | j dA t   j   t |  j | j dB t   j     qF| sFd= } t }	 dC |  j k r|  j dC j d< d=  } |  j dC j dA t  }	 n  | j
 dD j d | dC t |  j d?  t |	  j     t } qFqqW| j
 dE j |   | j
 dF j |   x|  j D]} | |  j k r| j dC  s
| j
 dG j d | t t | j d?      x(|  j | D]}
 |
 dH j dI  s	| j
 dJ j d | t |
 dH j d?   t |
 j d< d=  j d?   t |
 dK j d?   t |
 dA  j   t |
 dL j d?   t |
 j dM d=  j d?   t |
 dN  |
 dO r	dP n dQ 	  q| j
 dR j d | dS t | j d?     qWq
qqW| j
 dT j |   | j
 dU  WdV QXn  dV S(W   s   Create mapping files   -sort-current.xmls     t   wbs   <!--
s&   {} E2m3u2bouquet Custom mapping file
s9   {} Rearrange bouquets or channels in the order you wish
s?   {} Disable bouquets or channels by setting enabled to "false"
s`   {} Map DVB EPG to IPTV by changing channel serviceRef attribute to match DVB service reference
sG   {} Map XML EPG to different feed by changing channel tvg-id attribute
sB   {} Rename this file as {}-sort-override.xml for changes to apply
s   -->
s   <mapping>
s   {}<xmltvextrasources>
s   {}<!-- Example Config
i   s   {}<group id="{}">
s   UK - Freeview (xz)s   {}<url>{}</url>
i   s'   http://www.xmltvepg.nl/rytecUK_Basic.xzs6   http://rytecepg.ipservers.eu/epg_data/rytecUK_Basic.xzs-   http://rytecepg.wanwizard.eu/rytecUK_Basic.xzs9   http://91.121.106.172/~rytecepg/epg_data/rytecUK_Basic.xzs6   http://www.vuplus-community.net/rytec/rytecUK_Basic.xzs   {}</group>
s   UK - FTA (xz)s%   http://www.xmltvepg.nl/rytecUK_FTA.xzs4   http://rytecepg.ipservers.eu/epg_data/rytecUK_FTA.xzs+   http://rytecepg.wanwizard.eu/rytecUK_FTA.xzs7   http://91.121.106.172/~rytecepg/epg_data/rytecUK_FTA.xzs4   http://www.vuplus-community.net/rytec/rytecUK_FTA.xzs   UK - International (xz)s%   http://www.xmltvepg.nl/rytecUK_int.xzs4   http://rytecepg.ipservers.eu/epg_data/rytecUK_int.xzs+   http://rytecepg.wanwizard.eu/rytecUK_int.xzs7   http://91.121.106.172/~rytecepg/epg_data/rytecUK_int.xzs4   http://www.vuplus-community.net/rytec/rytecUK_int.xzs   UK - Sky Live (xz)s)   http://www.xmltvepg.nl/rytecUK_SkyLive.xzs8   http://rytecepg.ipservers.eu/epg_data/rytecUK_SkyLive.xzs/   http://rytecepg.wanwizard.eu/rytecUK_SkyLive.xzs;   http://91.121.106.172/~rytecepg/epg_data/rytecUK_SkyLive.xzs8   http://www.vuplus-community.net/rytec/rytecUK_SkyLive.xzs   UK - Sky Dead (xz)s)   http://www.xmltvepg.nl/rytecUK_SkyDead.xzs8   http://rytecepg.ipservers.eu/epg_data/rytecUK_SkyDead.xzs/   http://rytecepg.wanwizard.eu/rytecUK_SkyDead.xzs;   http://91.121.106.172/~rytecepg/epg_data/rytecUK_SkyDead.xzs8   http://www.vuplus-community.net/rytec/rytecUK_SkyDead.xzs   UK - Sports/Movies (xz)s-   http://www.xmltvepg.nl/rytecUK_SportMovies.xzs<   http://rytecepg.ipservers.eu/epg_data/rytecUK_SportMovies.xzs3   http://rytecepg.wanwizard.eu/rytecUK_SportMovies.xzs?   http://91.121.106.172/~rytecepg/epg_data/rytecUK_SportMovies.xzs<   http://www.vuplus-community.net/rytec/rytecUK_SportMovies.xzs   {}-->
s   {}</xmltvextrasources>
s   {}<categories>
s   VOD -R3   RQ   sY   {}<category name="{}" nameOverride="{}" idStart="{}" enabled="{}" customCategory="{}"/>
s   utf-8R   R   R   R   s9   {}<category name="{}" nameOverride="{}" enabled="{}" />
s   {}</categories>
s   {}<channels>
s   {}<!-- {} -->
s   stream-nameR   s   {}<channel name="{}" nameOverride="{}" tvg-id="{}" enabled="{}" category="{}" categoryOverride="{}" serviceRef="{}" clearStreamUrl="{}" />
s   tvg-ids   group-titleR   R   s
   stream-urlR   R   s'   {}<channel name="{}" category="{}" />
R   s   {}</channels>
s
   </mapping>N(   R   R%   R&   R   R[   R   RZ   R5   R   R*   R+   R   R   RG   R   R   R   R4   RS   R   R   RX   RB   (   R
   t   mappingfileR-  t   vod_category_outputR   R,  R1  R6   R   t   cat_enabledR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRR  ;  s    %	%	











#'",2'c      
   C   sG  |  j  d  d j t j  GH|  j r xZ t j t  D]F } d j t |  j	 j
   | k r7 t j t j j t |   q7 q7 Wn  g  } |  j	 j r |  j   } n  t d   |  j D  } t } t } t } xN|  j D]C} | j d  s|  j j | i   j d t  } n! |  j j d i   j d t  } | |  j k r | r t | |  j  }	 t |	  }
 t |  j	 j
  } | | k r|  j	 j rd }
 n  t j j t d j | |
   } t rd	 j |  GHn  | | k s|  j	 j rt | d
  } d j |  j	 j
 |	 j d   j d  } | j d  s~| |  j k r|  j | j d t  r|  j | d j d  } qnf d |  j k r|  j d j d t  rd j |  j d d j d  |	 j d d  j d   } n  d } | j d j | j d    | r]|  j	 j r]x* t d  D] } | j d j t    q*Wt } | d 7} n  xT |  j | D]E } | j d  s| d j d  r|  j! | |  n  | d 7} qkWx4 | d d k	 r| j d j t    | d 7} qWWd QXn| r|  j	 j rt | d
  } d j |  j	 j
  j d  } d |  j k r}|  j d j d t  r}|  j d d j d  } n  d } | j d j | j d    | r|  j	 j rx* t d  D] } | j d j t    qWt } | d 7} n  x | D] } | |  j k r| j d  | j d j | j d    x/ |  j | D]  } |  j! | |  | d 7} qLWx7 | d d k	 r| j d j t    | d 7} qsWqqWt } Wd QXn  | | k s| | k r| r| j" |  j# |
 |   | | k r|  j	 j rt } qqq q W|  j$ |  |  j  d  t j GHd S(   s$   Create the Enigma2 bouquets
        s   ----Creating bouquets----s   
{}s   userbouquet.suls_iptv_{}c         s   s$   |  ] } | j  d   r | Vq d S(   s   VOD -N(   R   (   R   R6   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pys	   <genexpr>  s    s   VOD -R   R   s   userbouquet.suls_iptv_{}_{}.tvs   Creating: {}s   w+s   {} - {}s   utf-8R3   s   VOD - RQ   i    s	   #NAME {}
id   s   {}
i   s   stream-nameR   Ns   {} - VODs   #SERVICE 1:64:0:0:0:0:0:0:0:0:
s   #DESCRIPTION {}
s   bouquets created ...(%   R   R   R}   R   R   R   R"   R#   R[   R   RZ   R$   R%   R&   R   R*  R"  R   R5   R   R   R4   R   R8   R   R   R*   RS   R   R@   R+   R#  R  R  R   R  R   (   R
   R.   t   iptv_bouquet_listR%  Rw  t   vod_bouquet_entry_outputt"   channel_number_start_offset_outputR6   Rx  R)  R  R  R'  R   R&  R(  R
  R   t   vodcat(    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRT    s    	!&$!	*( (!"(c         C   sx  d } t  r d GHn  y t j t  Wn+ t k
 rR } | j t j k rS   qS n Xt j j t d j	 t
 |  j j    } |  j rtt | d  } | j d  xu|  j D]j} | |  j k r |  j j | i   j d t  r | j d  st | |  j  } | j d j	 | t t | j d	      x |  j | D] } | d
 j d  sB| d ro| d n	 t |  } | d r| d }	 |	 j d  }
 |
 d k rd j	 |	 |
  }	 n  | j d j	 | t | j d	   |	 t t t |  j d	      qqBqBWqq q W| j d  Wd  QX|  j |  j j g  x+ |  j D] } |  j |  j | |  qPWn  d  S(   Ns     s   creating EPGImporter configs   suls_iptv_{}_channels.xmls   w+s   <channels>
R   R   s   {}<!-- {} -->
s   utf-8s   stream-nameR   s   tvg-idR   R   is   1{}sB   {}<channel id="{}">{}:http%3a//example.m3u8</channel> <!-- {} -->
s   </channels>
(   R   R   R   R(   R   R   R   R%   R&   R   R[   R   RZ   R   R*   R+   R   R   R4   R   R   R8   RB   RG   RS   R:   RM  R2  R   R   (   R
   R-  R   R/  R   R6   R)  R   t   tvg_idt   epg_service_refR   R,  (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyRU  B  s@    
*	0. 

9N("   R   R   R   R   R   R   R   R   R   R   R  R   R  R  R   R  R*  R   R2  R6  R7  R   RF  RV  RW  RK  RO  RP  RN  RS  RQ  RR  RT  RU  (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR     s>   	
	$		'				.	k	
					1				0	;									nt   Configc           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   s   t    |  _ d  S(   N(   R   t	   providers(   R
   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR   p  s    c         C   s=   d j  t j j t d   GHt | d  } | j d  d  S(   Ns)   Default configuration file created in {}
s
   config.xmlRu  s  <!--
    E2m3u2bouquet supplier config file
    Add as many suppliers as required and run the script with no parameters
    this config file will be used and the relevant bouquets set up for all suppliers entered
    0 = No/false
    1 = Yes/true
    For elements with <![CDATA[]] enter value between brackets e.g. <![CDATA[mypassword]]>
-->
<config>
    <supplier>
        <name>Supplier Name 1</name><!-- Supplier Name -->
        <enabled>1</enabled><!-- Enable or disable the supplier (0 or 1) -->
        <m3uurl><![CDATA[http://address.yourprovider.com:80/get.php?username=USERNAME&password=PASSWORD&type=m3u_plus&output=ts]]></m3uurl><!-- Extended M3U url -->
        <epgurl><![CDATA[http://address.yourprovider.com:80/xmltv.php?username=USERNAME&password=PASSWORD]]></epgurl><!-- XMLTV EPG url -->
        <username><![CDATA[]]></username><!-- (Optional) will replace USERNAME placeholder in urls -->
        <password><![CDATA[]]></password><!-- (Optional) will replace PASSWORD placeholder in urls -->
        <iptvtypes>0</iptvtypes><!-- Change all streams to IPTV type (0 or 1) -->
        <streamtypetv></streamtypetv><!-- (Optional) Custom TV stream type (e.g. 1, 4097, 5001 or 5002) -->
        <streamtypevod></streamtypevod><!-- (Optional) Custom VOD stream type (e.g. 4097, 5001 or 5002) -->
        <multivod>0</multivod><!-- Split VOD into seperate categories (0 or 1) -->
        <allbouquet>1</allbouquet><!-- Create all channels bouquet -->
        <picons>0</picons><!-- Automatically download Picons (0 or 1) -->
        <iconpath>/usr/share/enigma2/picon/</iconpath><!-- Location to store picons -->
        <xcludesref>1</xcludesref><!-- Disable service ref overriding from override.xml file (0 or 1) -->
        <bouqueturl><![CDATA[]]></bouqueturl><!-- (Optional) url to download providers bouquet - to map custom service references -->
        <bouquetdownload>0</bouquetdownload><!-- Download providers bouquet (use default url) must have username and password set above - to map custom service references -->
        <bouquettop>0</bouquettop><!-- Place IPTV bouquets at top (0 or 1)-->
    </supplier>
    <supplier>
        <name>Supplier Name</name><!-- Supplier Name -->
        <enabled>0</enabled><!-- Enable or disable the supplier (0 or 1) -->
        <m3uurl><![CDATA[http://address.yourprovider.com:80/get.php?username=USERNAME&password=PASSWORD&type=m3u_plus&output=ts]]></m3uurl><!-- Extended M3U url -->
        <epgurl><![CDATA[http://address.yourprovider.com:80/xmltv.php?username=USERNAME&password=PASSWORD]]></epgurl><!-- XMLTV EPG url -->
        <username><![CDATA[]]></username><!-- (Optional) will replace USERNAME placeholder in urls -->
        <password><![CDATA[]]></password><!-- (Optional) will replace PASSWORD placeholder in urls -->
        <iptvtypes>0</iptvtypes><!-- Change all streams to IPTV type (0 or 1) -->
        <streamtypetv></streamtypetv><!-- (Optional) Custom TV service type (e.g. 1, 4097, 5001 or 5002) -->
        <streamtypevod></streamtypevod><!-- (Optional) Custom VOD service type (e.g. 4097, 5001 or 5002) -->
        <multivod>0</multivod><!-- Split VOD into seperate categories (0 or 1) -->
        <allbouquet>1</allbouquet><!-- Create all channels bouquet -->
        <picons>0</picons><!-- Automatically download Picons (0 or 1) -->
        <iconpath>/usr/share/enigma2/picon/</iconpath><!-- Location to store picons -->
        <xcludesref>1</xcludesref><!-- Disable service ref overriding from override.xml file (0 or 1) -->
        <bouqueturl><![CDATA[]]></bouqueturl><!-- (Optional) url to download providers bouquet - to map custom service references -->
        <bouquetdownload>0</bouquetdownload><!-- Download providers bouquet (use default url) must have username and password set above - to map custom service references -->
        <bouquettop>0</bouquettop><!-- Place IPTV bouquets at top (0 or 1)--> 
    </supplier>
</config>(   R   R   R%   R&   R   R*   R+   (   R
   t
   configfileR   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   make_default_configs  s    /c         C   s  t    |  _ yt j d |  } x| j d  D]w} t   } | d k	 rx=| D]2} | j d k r | j d k r} d n | j j	   | _
 n  | j d k r | j d k r t n t | _ n  | j d k r | j d k r d n | j j	   | _ n  | j d k r4| j d k rd n | j j	   | _ n  | j d	 k rm| j d k rXd n | j j	   | _ n  | j d
 k r| j d k rd n | j j	   | _ n  | j d k r| j d k rd n | j j	   | _ n  | j d k r| j d k rd n | j j	   | _ n  | j d k rH| j d k r<t n t | _ n  | j d k r| j d k rld n | j j	   | _ n  | j d k r| j d k rd n | j j	   | _ n  | j d k r| j d k rt n t | _ n  | j d k r| j d k rt n t | _ n  | j d k rJ| j d k r>t n t | _ n  | j d k r| j d k rnd n | j j	   | _ n  | j d k r| j d k rt n t | _ n  | j d k r| j d k rd n | j j	   | _ n  | j d k r| j d k rt n t | _ n  | j d k rL| j d k r@t n t | _ n  | j d k rS | j d k rpd n | j j	   | _ qS qS Wn  | j
 r1 | |  j | j
 <q1 q1 WWn3 t  k
 r} d } | GHt! rt  |   qn Xd S(   s    Read Config from file R   s   .//supplierRZ   RQ   R   R   t   settingslevelR_   Rc   Re   Rf   t   providerupdateRg   t   streamtypetvt   streamtypevodRk   Rl   Rm   Rn   Ro   R   Rp   Rq   Rr   t   lastproviderupdatei    s   Corrupt config.xml fileN("   R   R  R   R   R   R   R   t   tagRq  R   RZ   R   R5   R   R   R   R   Re   Rf   R   R   R   R   R   R   Rm   R   R   R   R   R   R   R-   R   (   R
   R  R   R   t   providert   childR   R	   (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   read_config  sl    	*!******!**!!!*!*!!1	c      
   C   s  t  j j t  j j t d   } d } |  j rDt | d   } | j d  | j d j |   | j d j |   | j d j |   | j d j |   | j d	 j |   | j d
 j |   | j d  | j d  x>|  j j   D]-\ } } | j d j |   | j d j d | t	 | j
    | j d j d | | j rgd n d   | j d j d | | j   | j d j d | | j   | j d j d | | j   | j d j d | | j   | j d j d | | j   | j d j d | | j   | j d j d | | j rSd n d   | j d j d | | j   | j d j d | | j   | j d j d | | j rd n d   | j d j d | | j rd n d   | j d j d | | j rd n d   | j d j d | | j rF| j n d    | j d! j d | | j rrd n d   | j d" j d | | j   | j d# j d | | j rd n d   | j d$ j d | | j rd n d   | j d% j d | | j   | j d& j |   q W| j d'  Wd( QXnE t  j j t  j j t d   rd) GHt  j t  j j t d   n  d( S(*   s   Write providers to config file
        Manually write instead of using ElementTree so that we can format the file for easy human editing
        (inc. Windows line endings)
        s
   config.xmls     Ru  s   <!--
s&   {}E2m3u2bouquet supplier config file
s%   {}Add as many suppliers as required
s\   {}this config file will be used and the relevant bouquets set up for all suppliers entered
s   {}0 = No/False
s   {}1 = Yes/True
s`   {}For elements with <![CDATA[]] enter value between empty brackets e.g. <![CDATA[mypassword]]>
s   -->
s
   <config>
s   {}<supplier>
s)   {}<name>{}</name><!-- Supplier Name -->
i   sI   {}<enabled>{}</enabled><!-- Enable or disable the supplier (0 or 1) -->
R   R   s%   {}<settingslevel>{}</settingslevel>
s=   {}<m3uurl><![CDATA[{}]]></m3uurl><!-- Extended M3U url --> 
s9   {}<epgurl><![CDATA[{}]]></epgurl><!-- XMLTV EPG url -->
sd   {}<username><![CDATA[{}]]></username><!-- (Optional) will replace USERNAME placeholder in urls -->
sd   {}<password><![CDATA[{}]]></password><!-- (Optional) will replace PASSWORD placeholder in urls -->
sZ   {}<providerupdate><![CDATA[{}]]></providerupdate><!-- (Optional) Provider update url -->
sQ   {}<iptvtypes>{}</iptvtypes><!-- Change all TV streams to IPTV type (0 or 1) -->
sh   {}<streamtypetv>{}</streamtypetv><!-- (Optional) Custom TV stream type (e.g. 1, 4097, 5001 or 5002 -->
sh   {}<streamtypevod>{}</streamtypevod><!-- (Optional) Custom VOD stream type (e.g. 4097, 5001 or 5002 -->
sO   {}<multivod>{}</multivod><!-- Split VOD into seperate categories (0 or 1) -->
sL   {}<allbouquet>{}</allbouquet><!-- Create all channels bouquet (0 or 1) -->
sF   {}<picons>{}</picons><!-- Automatically download Picons (0 or 1) -->
s=   {}<iconpath>{}</iconpath><!-- Location to store picons) -->
RQ   sf   {}<xcludesref>{}</xcludesref><!-- Disable service ref overriding from override.xml file (0 or 1) -->
s   {}<bouqueturl><![CDATA[{}]]></bouqueturl><!-- (Optional) url to download providers bouquet - to map custom service references -->
s   {}<bouquetdownload>{}</bouquetdownload><!-- Download providers bouquet (uses default url) must have username and password set above - to map custom service references -->
sK   {}<bouquettop>{}</bouquettop><!-- Place IPTV bouquets at top (0 or 1) -->
sD   {}<lastproviderupdate>{}</lastproviderupdate><!-- Internal use -->
s   {}</supplier>
s   </config>
Ns   no providers remove config(    R   R%   R&   R   R  R*   R+   R   t	   iteritemsRG   RZ   R   R   R   R   Re   Rf   R   R   R   R   R   R   Rm   R   R   R   R   R   R   R   R$   (   R
   t   config_fileR-  R   R   R  (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   write_config  sP    !	&,      ,  ,,,/, ,, !(   R   R   R   R  R  R  (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyR  o  s   		5	<c         C   s;  |  d  k r t j }  n t j j |   t j j t j d  } d t } t t	  } d j
 | |  } t j d  d } d j
 | t t   } y&t | |  } | j   } | j }	 t   t _ t j d  t   |	 rt   t   d GHt j d  n t   t   }
 | j |
 _ | j |
 _ | j  |
 _! | j" |
 _# | j$ |
 _% | j& |
 _' | j( |
 _) | j* |
 _* | j+ |
 _, | j- |
 _. | j/ |
 _0 | j1 |
 _2 | j3 |
 _3 | j4 |
 _4 | j5 |
 _6 | j7 |
 _8 |
 j rd	 GHd
 GHd GHt9 |
  } | j:   nd GHd GHd GHt;   } t j j< t j j= t> d   ri| j? t j j= t> d   t@ } x | jA jB   D] \ } } | jC r0| j2 jD d  rd j
 t j j= t> d   GHt j d  qAd GHd j
 | j2  GHd GHt9 |  } tE tF jF    tE | jG jH  d k r#| jI   } n  | j:   qqd j
 | j2  GHqqW| rX| jJ   n  t   tK   nP | jL t j j= t> d   d j
 t j j= t> d   GH| jM   t j d  Wnz tN k
 rd StO k
 r6} tP r|  n  tQ |  d } t jR jS | d tT |  d  t jR jS | d  d SXd  S(   Ni    s   v%ss   %(prog)s {} ({})s   
i   s   {}

  Copyright 2017. All rights reserved.
  Created on {}.
  Licensed under GNU GENERAL PUBLIC LICENSE version 3
  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE
i   s#   Uninstall only, program exiting ...s'   
**************************************s(   E2m3u2bouquet - Command line based setups'   **************************************
s!   
********************************s"   E2m3u2bouquet - Config based setups!   ********************************
s
   config.xmls   Supplier Names4   Please enter your details in the config file in - {}i   s   Config based setup - {}i`T  s.   
Provider: {} is disabled - skipping.........
s}   Please ensure correct command line options are passed to the program 
or populate the config file in {} 
for help use --help
RJ   s   : s     for help use --help(U   R   R   t   argvR  R   R%   t   basenameR   R   t   __updated__R   R   R   t   __date__R|   t
   parse_argsRs   R   R   t
   _urlopenert   sockett   setdefaulttimeoutR   R2   R?   t   exitR    R   R_   R   Rc   R   Rg   R   Rk   R   Rl   R   Rp   R   Rq   R   Rm   Rn   R   Ro   R   Rr   R   Rd   RZ   Re   Rf   Ri   R   Rj   R   R   RV  R  R   R&   R   R  R5   R  R  R   R   Rv   RA  R   R   RW  R  R   R  t   print_usaget   KeyboardInterruptR-   R   R   t   stderrR+   t   repr(   R  t   program_namet   program_versiont   program_build_dateRx   t   program_shortdescRw   Ry   t   argsRs   t   args_configt   args_providert   e2m3u2b_configt   providers_updatedR   t   provider_configR  R   R-  (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   main  s    

				!	(

	"t   __main__s   H:/Satelite Stuff/epgimport/s   H:/Satelite Stuff/enigma2/s   H:/Satelite Stuff/picons/(H   R   RA  R   R   R   RV   RT   R   R   R   R   R<  R   R>  R3  R  t   PILR    t   collectionsR   t   xml.etree.cElementTreet   etreet   cElementTreeR   t   ImportErrort   xml.etree.ElementTreeR   t   enigmaR   R   t   argparseR   R   t   __all__R   R  R  R   R;   R#   R(   R%   R&   R   RJ  R5   R   R  R-   R   t   FancyURLopenerR   R   R   R    R2   R8   R:   R?   RB   RG   RI   R[   R|   R}   R   R   R  R  R   R  R   (    (    (    sI   /usr/lib/enigma2/python/Plugins/Extensions/E2m3u2bouquet/e2m3u2bouquet.pyt   <module>   s   
			
	"								
	-    hx