Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

python - How to read all files and do same job for one def?

I'm using this def

m3ufile= 'IPTV1'
M3UPATH= '/tmp/IPTV/IPTV1.m3u'
BFNAME= 'userbouquet.IPTV1.tv'
LastScanned= 'userbouquet.LastScanned.tv'
WGET='wget --no-check-certificate'
BOUQUETSPATH='/etc/enigma2/bouquets.tv'
E2SETTINGSPATH='/etc/enigma2'
##################################################################

def convert():
       with open('%s/%s' % (E2SETTINGSPATH, BFNAME), 'w') as outfile:
        desk_tmp = ''
        outfile.write('#NAME %s
' % m3ufile)
        if os.path.isfile(M3UPATH):
          for line in open(M3UPATH):
            if line.startswith('http://'):
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s' % line.replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s' % desk_tmp)
            elif line.startswith('#EXTINF'):
                    desk_tmp = '%s' % line.split(',list.alliptvlinks.com ')[-1]
            elif '<stream_url><![CDATA' in line:
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s
' % line.split('[')[-1].split(']')[0].replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s
' % desk_tmp)
            elif '<title>' in line:
                    if '<![CDATA[' in line:
                        desk_tmp = '%s
' % line.split('[')[-1].split(']')[0]
                    else:
                        desk_tmp = '%s
' % line.split('<')[1].split('>')[1]
          outfile.write('
')           
          outfile.close()
          if os.path.isfile(BOUQUETSPATH):
           for line in open(BOUQUETSPATH):
            if BFNAME in line:
                  if os.path.isfile('%s/%s' % (E2SETTINGSPATH, BFNAME)) and os.path.isfile(BOUQUETSPATH):
                      remove_line(BOUQUETSPATH, BFNAME)
           with open(BOUQUETSPATH, 'a') as outfile:
            outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet
' % BFNAME)
            outfile.close()
            if LastScanned in line:
                  remove_line(BOUQUETSPATH, LastScanned)
                  with open(BOUQUETSPATH, 'a') as outfile:
                      outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet
' % LastScanned)
                      outfile.close()
           os.system('wget -q -O - http://root%[email protected]/web/servicelistreload?mode=0 > /dev/null 2>&1  && sleep 2')
           return
        else:
           print("File missing %s" % M3UPATH)

To convert date from (/tmp/IPTV/IPTV1.m3u) to (/etc/enigma2/userbouquet.IPTV1.tv)

What I need is to do same job for all files inside (/tmp/IPTV) such as (IPTV1, IPTV2 ,IPTV3 .... IPTV100) and convert it to (/etc/enigma2/userbouquet.IPTV1.tv, /etc/enigma2/userbouquet.IPTV2.tv ..... /etc/enigma2/userbouquet.IPTV100.tv)

Thank you

question from:https://stackoverflow.com/questions/65847629/how-to-read-all-files-and-do-same-job-for-one-def

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok.... I found the solution


M3UPATH= '/tmp/IPTV'
LastScanned= 'userbouquet.LastScanned.tv'
WGET='wget --no-check-certificate'
BOUQUETSPATH='/etc/enigma2/bouquets.tv'
E2SETTINGSPATH='/etc/enigma2'
##################################################################
import os
def convert(m3ufile,BFNAME):
       with open('%s/%s' % (E2SETTINGSPATH, BFNAME), 'w') as outfile:
        desk_tmp = ''
        outfile.write('#NAME %s
' % m3ubase)
        if os.path.isfile(m3ufile):
          for line in open(m3ufile):
            if line.startswith('http://'):
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s' % line.replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s' % desk_tmp)
            elif line.startswith('#EXTINF'):
                    desk_tmp = '%s' % line.split(',list.alliptvlinks.com ')[-1]
            elif '<stream_url><![CDATA' in line:
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s
' % line.split('[')[-1].split(']')[0].replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s
' % desk_tmp)
            elif '<title>' in line:
                    if '<![CDATA[' in line:
                        desk_tmp = '%s
' % line.split('[')[-1].split(']')[0]
                    else:
                        desk_tmp = '%s
' % line.split('<')[1].split('>')[1]
          outfile.write('
')           
          outfile.close()
          if os.path.isfile(BOUQUETSPATH):
           for line in open(BOUQUETSPATH):
            if BFNAME in line:
                  if os.path.isfile('%s/%s' % (E2SETTINGSPATH, BFNAME)) and os.path.isfile(BOUQUETSPATH):
                      remove_line(BOUQUETSPATH, BFNAME)
           with open(BOUQUETSPATH, 'a') as outfile:
            outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet
' % BFNAME)
            outfile.close()
            if LastScanned in line:
                  remove_line(BOUQUETSPATH, LastScanned)
                  with open(BOUQUETSPATH, 'a') as outfile:
                      outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet
' % LastScanned)
                      outfile.close()
           os.system('wget -q -O - http://root%[email protected]/web/servicelistreload?mode=0 > /dev/null 2>&1  && sleep 2')
           return
        else:
           print("File missing %s" % M3UPATH)       
counter=0
for subdir, dirs, files in os.walk(M3UPATH):
    for file in files:
        
        if file.endswith(".m3u"):
                  counter=counter+1
                  m3ufile=os.path.join(subdir, file)
                  print "m3ufile",m3ufile
                  m3ubase=file[:-4]
                  print "m3ubase",m3ubase
                  BFNAME= 'userbouquet.%s.tv'%m3ubase
                  print "m3ubase",m3ubase
                  convert(m3ufile,BFNAME)
print "%s m3ufiles converted to bouquets"%str(counter)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...