Please help to correct this script

Friends, I have a problem with the Python script - where we used it with plugin to download epg for the channels, downloaded - as xml file
This current lines in xml, which I need help to correct it

<channel id="TeN.eg">
    <display-name lang="en">Ten</display-name>
  </channel

The channel name in the second line must be the same as the channel name in the first line, include the same extension, eg must be added to the name.

This the correct line , which I need to be as follows

   <channel id="TeN.eg">
    <display-name lang="en">Ten.eg</display-name>
  </channel>

The modification will be in the next line of the script - I tried, but it didn’t work

         def Change_arabia_to_elcinema():
            List_Chang = [('<channel','  <channel'),
            ('<display-name>','\n    <display-name lang="en">'),
            ('<url>','\n    <url>'),('</channel>',
            '\n  </channel>'),

This is the script file which I ussw

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import requests
import os
import io
import re
import sys
import json
try:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT
except:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT

import xml.etree.ElementTree as ET
import subprocess
# Ignore insecure request warnings
import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning
warnings.simplefilter('ignore', InsecureRequestWarning)
path = EPG_ROOT + '/arabia.xml'
path_1 = EPG_ROOT + '/out.xml'
print('**************El_Cinma_iet5 EPG******************')
sys.stdout.flush()
print("Downloading El_Cinma_iet5 EPG guide\nPlease wait....")
sys.stdout.flush()
def Suprim_fil_renom_fil():
    os.remove(path)
    old_name = path_1
    new_name = path
    os.rename(old_name, new_name)
    print(" Cool .... congratulations your arabia.xml")
    print(" file is created -  successfully done     ")
    print("                                          ")
    print("############################################################")
    print("The time is set to +0200, and if your time is different,    ")
    print("you can modify the elcinmaiet5.py file in the following     ")
    print("path /usr/lib/enigma2/python/Plugins/Extensions/EPGGrabber/ ")    
    print("providers/                                                  ") 
    print("############################################################")
 
def Suprim_Doubl():
    lines_seen = set()
    with open(path_1, 'w') as output_file:
        for each_line in open(path, 'r'):
            if each_line not in lines_seen:
                output_file.write(each_line)
                lines_seen.add(each_line)
try:
    response = requests.get('https://www.bevy.be/***/arabia.xml', verify=False)
    if response.status_code == 200:
        with io.open(path, 'w', encoding="utf-8") as f:
            f.write(response.text)
        print("##########################################")
        print("                                          ")
        print("Arabia.xml Downloaded Successfully        ")
        print("                                          ")
        print("##########################################")

        import fileinput,sys
        def ChangeDataList(Mege, Megerep, fil):
            Mege = Mege
            Megerep = Megerep
            for line in fileinput.input(path_1, inplace=1):
                if Mege in line:
                    line = line.replace(Mege, Megerep)
                sys.stdout.write(line)
        def Change_arabia_to_elcinema():
            List_Chang = [('<channel','  <channel'),
            ('<display-name>','\n    <display-name lang="en">'),
            ('<url>','\n    <url>'),('</channel>',
            '\n  </channel>'),
                             
            ('<programme','  <programme'),
            ('<title','\n    <title'),('<desc','\n    <desc'),
            ('</programme>','\n  </programme>'),
            ('<icon','\n    <icon'),
            ('<category','\n    <category')]


            for Exprt in List_Chang:
                ChangeDataList(Exprt[0],Exprt[1],'arabia.xml')
        #Change_arabia_to_elcinema()
    else:
        print("Failed to download /arabia.xml. Status code:", response.status_code)
    from datetime import datetime, timedelta
    with io.open(path, 'r', encoding="utf-8") as f:
        xml_data = f.read()
    # Convert the start and stop times to datetime objects and adjust the timezone from +0000 to your desired timezone
    start_time = datetime.strptime(re.search(r'start="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)
    stop_time = datetime.strptime(re.search(r'stop="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)
    # Replace the timezone from +0000 to your desired timezone
    xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
    with io.open(path, 'w', encoding="utf-8") as f:
        f.write(xml_data)
    Suprim_Doubl()
    print("Please wait....out.xml Deletion fil")
    sleep(5)
    Change_arabia_to_elcinema()
    print("Please wait.... rename the file")
    sleep(5)
    Suprim_fil_renom_fil()
    from datetime import datetime
    with open(PROVIDERS_ROOT, 'r') as f:
        data = json.load(f)
        for channel in data['bouquets']:
            if channel["bouquet"] == "elcinmaiet5":
                channel['date'] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
        with open(PROVIDERS_ROOT, 'w') as f:
            json.dump(data, f)
    print('**************FINISHED******************')
    sys.stdout.flush()
except requests.exceptions.RequestException as e:
    print("Failed to download /arabia.xml:", e)
# Remove lines containing specified data and empty lines
with open(path, 'r') as f:
    lines = f.readlines()
with open(path, 'w') as f:
    for line in lines:
        if '<icon src="https://' not in line and '<url>https://' not in line and line and '<category' not in line and line.strip():
            f.write(line)
  1. I’d recommend moving all of the imports together at the top of the file.

  2. What is the point of this?

try:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT
except:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT

It’s a ‘bare’ except, which is a very bad idea, and both parts of it are the same!

  1. Instead of using sys.stdout.flush() you can add , flush=True to the ‘print’ arguments:
print('**************El_Cinma_iet5 EPG******************', flush=True)
  1. Is Suprim_Doubl() removing duplicate lines from the XML file? Won’t there be legitimate reasons for duplicate lines, e.g.:
<channel id="channel1.eg">
    <display-name lang="en">Channel 1</display-name>
</channel>
<channel id="channel2.eg">
    <display-name lang="en">Channel 2</display-name>
</channel>

Notice how there are multiple lines that are '</channel>\n'.

  1. sleep(5)? Really?

  2. These lines ae useless:

Mege = Mege
Megerep = Megerep
  1. Change_arabia_to_elcinema and ChangeDataList are reading and writing the entire file line by line for each change.

I’d suggest reading the entire file, doing all of the changes, and then writing it back out.

  1. Given item 7, replacing the display name with the channel ID would be:
xml_text = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="en">)[^<]+', r'\1\2', xml_text)

giving:

   <channel id="TeN.eg">
    <display-name lang="en">TeN.eg</display-name>
  </channel>

my fried thank you for replay m thew original xml file which was created - it contained a lot of data that was not compatible with the plugin, so we had to make the script to delete it and make the new XML file small - any way i have use ur Line and merged with the script

 """   xml_text = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="ar">)[^<]+', r'\1\2', xml_data) 

but did not success

here the out put file same same as the old and no change “”

 <channel id="TeN.eg">
    <display-name lang="ar">Ten</display-name>
  </channel>

please find below full script after merge you line with the script “”“”

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import requests
import os
import io
import re
import sys
import json
import xml.etree.ElementTree as ET
import subprocess
try:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT
except:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT

# Ignore insecure request warnings
import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning
warnings.simplefilter('ignore', InsecureRequestWarning)
path = EPG_ROOT + '/arabia.xml'
path_1 = EPG_ROOT + '/out.xml'
print('**************El_Cinma_iet5 EPG******************')
sys.stdout.flush()
print("Downloading El_Cinma_iet5 EPG guide\nPlease wait....")
sys.stdout.flush()
def Suprim_fil_renom_fil():
    os.remove(path)
    old_name = path_1
    new_name = path
    os.rename(old_name, new_name)
    print(" Cool .... congratulations your arabia.xml")
    print(" file is created -  successfully done     ")
    print("                                          ")
    print("############################################################")
    print("The time is set to +0200, and if your time is different,    ")
    print("you can modify the elcinmaiet5.py file in the following     ")
    print("path /usr/lib/enigma2/python/Plugins/Extensions/EPGGrabber/ ")    
    print("providers/                                                  ") 
    print("############################################################")
 
def Suprim_Doubl():
    lines_seen = set()
    with open(path_1, 'w') as output_file:
        for each_line in open(path, 'r'):
            if each_line not in lines_seen:
                output_file.write(each_line)
                lines_seen.add(each_line)
try:
    response = requests.get('https://www.bevy.be/bevyfiles/arabia.xml', verify=False)
    if response.status_code == 200:
        with io.open(path, 'w', encoding="utf-8") as f:
            f.write(response.text)
        print("##########################################")
        print("                                          ")
        print("Arabia.xml Downloaded Successfully        ")
        print("                                          ")
        print("##########################################")

        import fileinput,sys
        def ChangeDataList(Mege, Megerep, fil):
            Mege = Mege
            Megerep = Megerep
            for line in fileinput.input(path_1, inplace=1):
                if Mege in line:
                    line = line.replace(Mege, Megerep)
                sys.stdout.write(line)
        def Change_arabia_to_elcinema():
            List_Chang = [('<channel','  <channel'),
			('<display-name>','\n    <display-name lang="ar">'),
			('<url>','\n    <url>'),('</channel>',
			'\n  </channel>'),
            ('<channel id=".">','<display-name lang="ar"> = .'),

            ('<programme','  <programme'),
            ('<title','\n    <title'),('<desc','\n    <desc'),
            ('</programme>','\n  </programme>'),
            ('<icon','\n    <icon'),
            ('<category','\n    <category')]

            for Exprt in List_Chang:
                ChangeDataList(Exprt[0],Exprt[1],'arabia.xml')
        #Change_arabia_to_elcinema()
    else:
        print("Failed to download /arabia.xml. Status code:", response.status_code)
    from datetime import datetime, timedelta

    # Update the start and stop times and adjust the timezone
    with io.open(path, 'r', encoding="utf-8") as f:
        xml_data = f.read()
    # Convert the start and stop times to datetime objects and adjust the timezone from +0000 to your desired timezone
    start_time = datetime.strptime(re.search(r'start="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)
    stop_time = datetime.strptime(re.search(r'stop="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)
    # Replace the timezone from +0200 to your desired timezone
    xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
    with io.open(path, 'w', encoding="utf-8") as f:
        f.write(xml_data)

    xml_text = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="ar">)[^<]+', r'\1\2', xml_data) 

    Suprim_Doubl()
    print("Please wait....out.xml Deletion fil")
    sleep(5)
    Change_arabia_to_elcinema()
    print("Please wait.... rename the file")
    sleep(5)
    Suprim_fil_renom_fil()
    from datetime import datetime
    with open(PROVIDERS_ROOT, 'r') as f:
        data = json.load(f)
        for channel in data['bouquets']:
            if channel["bouquet"] == "elcinmaiet5":
                channel['date'] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
        with open(PROVIDERS_ROOT, 'w') as f:
            json.dump(data, f)

    print('**************FINISHED******************')
    sys.stdout.flush()
except requests.exceptions.RequestException as e:
    print("Failed to download /arabia.xml:", e)


# Remove lines containing specified data and empty lines
with open(path, 'r') as f:
    lines = f.readlines()
with open(path, 'w') as f:
    for line in lines:
        if '<icon src="https://' not in line and '<url>https://' not in line and line and '<category' not in line and line.strip():
            f.write(line)

There was no change because you’re doing the change on xml_data and putting the result in xml_text, but not doing anything with it. That the only place that xml_text occurs in the program.

thanks brother , ok i got ur point , i amend the line , can you check “”“”

arabia = re.sub(r’(<channel id=“([^”]+)">\s+)[^<]+‘, r’\1\2’, xml_data)

is it ok now or not , i need to be at the same folder

So, you’re now putting the result into a variable called arabia? What do you do with its contents? If the only thing you did was change this one line, then you’re no further forward.

You should change this:

    xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
    with io.open(path, 'w', encoding="utf-8") as f:
        f.write(xml_data)

    xml_text = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="ar">)[^<]+', r'\1\2', xml_data) 

to this:

    xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
    xml_data = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="ar">)[^<]+', r'\1\2', xml_data) 

    with io.open(path, 'w', encoding="utf-8") as f:
        f.write(xml_data)

my friend thank you for reply ,
i still have the same pb , The modification did not happen as I wanted, the file is as it is .
this the new file

  <channel id="TeN.eg">
    <display-name lang="ar">Ten</display-name>
  </channel>

-i want to change **Ten ** in second line to be same as ** TeN.eg *** at first line

here is the full script which i used .

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import requests
import os
import io
import re
import sys
import json
import xml.etree.ElementTree as ET
import subprocess
try:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT
except:
    from __init__ import EPG_ROOT, PROVIDERS_ROOT

# Ignore insecure request warnings
import warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning
warnings.simplefilter('ignore', InsecureRequestWarning)
path = EPG_ROOT + '/arabia.xml'
path_1 = EPG_ROOT + '/out.xml'
print('**************El_Cinma_iet5 EPG******************')
sys.stdout.flush()
print("Downloading El_Cinma_iet5 EPG guide\nPlease wait....")
sys.stdout.flush()

def Suprim_fil_renom_fil():
    os.remove(path)
    old_name = path_1
    new_name = path
    os.rename(old_name, new_name)
    print(" Cool .... congratulations your arabia.xml")
    print(" file is created -  successfully done     ")
    print("                                          ")
    print("############################################################")
    print("The time is set to +0200, and if your time is different,    ")
    print("you can modify the elcinmaiet5.py file in the following     ")
    print("path /usr/lib/enigma2/python/Plugins/Extensions/EPGGrabber/ ")
    print("providers/                                                  ")
    print("############################################################")
 
def Suprim_Doubl():
    lines_seen = set()
    with open(path_1, 'w') as output_file:
        for each_line in open(path, 'r'):
            if each_line not in lines_seen:
                output_file.write(each_line)
                lines_seen.add(each_line)

try:
    response = requests.get('https://www.bevy.be/bevyfiles/arabia.xml', verify=False)
    if response.status_code == 200:
        with io.open(path, 'w', encoding="utf-8") as f:
            f.write(response.text)
        print("##########################################")
        print("                                          ")
        print("Arabia.xml Downloaded Successfully        ")
        print("                                          ")
        print("##########################################")

        import fileinput,sys
        def ChangeDataList(Mege, Megerep, fil):
            Mege = Mege
            Megerep = Megerep
            for line in fileinput.input(path_1, inplace=1):
                if Mege in line:
                    line = line.replace(Mege, Megerep)
                sys.stdout.write(line)

        def Change_arabia_to_elcinema():
            List_Chang = [('<channel','  <channel'),
            ('<display-name>','\n    <display-name lang="ar">'),
            ('<url>','\n    <url>'),('</channel>',
            '\n  </channel>'),
            ('<channel id=".">','<display-name lang="ar"> = .'),

            ('<programme','  <programme'),
            ('<title','\n    <title'),('<desc','\n    <desc'),
            ('</programme>','\n  </programme>'),
            ('<icon','\n    <icon'),
            ('<category','\n    <category')]

            # Read the contents of the file into the xml_data variable
            with io.open(path, 'r', encoding="utf-8") as f:
                xml_data = f.read()
            
            xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
            xml_data = re.sub(r'(<channel id="([^"]+)">\s+<display-name lang="ar">)[^<]+', r'\1\2', xml_data)

            for Exprt in List_Chang:
                ChangeDataList(Exprt[0],Exprt[1],'arabia.xml')
        Change_arabia_to_elcinema()

    else:
        print("Failed to download /arabia.xml. Status code:", response.status_code)

    from datetime import datetime, timedelta

    # Update the start and stop times and adjust the timezone
    with io.open(path, 'r', encoding="utf-8") as f:
        xml_data = f.read()
    # Convert the start and stop times to datetime objects and adjust the timezone from +0200 to your desired timezone
    start_time = datetime.strptime(re.search(r'start="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)
    stop_time = datetime.strptime(re.search(r'stop="(\d{14}) \+0000"', xml_data).group(1), '%Y%m%d%H%M%S') + timedelta(hours=2)

    # Replace the timezone from +0200 to your desired timezone
    xml_data = re.sub(r' \+0000"', ' +0200"', xml_data)
    with io.open(path, 'w', encoding="utf-8") as f:
        f.write(xml_data)

    Suprim_Doubl()
    print("Please wait....out.xml Deletion fil")
    sleep(5)
    Change_arabia_to_elcinema()
    print("Please wait.... rename the file")
    sleep(5)
    Suprim_fil_renom_fil()

    from datetime import datetime
    with open(PROVIDERS_ROOT, 'r') as f:
        data = json.load(f)
        for channel in data['bouquets']:
            if channel["bouquet"] == "elcinmaiet5":
                channel['date'] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
        with open(PROVIDERS_ROOT, 'w') as f:
            json.dump(data, f)

    print('**************FINISHED******************')
    sys.stdout.flush()

except requests.exceptions.RequestException as e:
    print("Failed to download /arabia.xml:", e)

# Remove lines containing specified data and empty lines
with io.open(path, 'r', encoding="utf-8") as f:
    xml_data = f.read()

with io.open(path, 'w', encoding="utf-8") as f:
    lines = xml_data.splitlines()
    for line in lines:
        if '<icon src="https://' not in line and '<url>https://' not in line and line and '<category' not in line and line.strip():
            f.write(line + '\n')

thank you for ur help

Look very closely at what you’re doing in Change_arabia_to_elcinema:

  • You’re reading the file arabia.xml into the variable xml_data.

  • You’re making some changes to xml_data.

  • You’re calling ChangeDataList multiple times, which changes the file out.xml.

And then you reach the end of the function.

At no point do you write xml_data back out to a file, so any changes that you did to xml_data are lost.

As I said in a previous post, my recommendation is to:

  • Read the file arabia.xml into a variable xml_data.

  • Make all of the changes to xml_data.

  • Write xml_data out to the file out.xml.