Skip to content

Commit

Permalink
introduced -it for #49
Browse files Browse the repository at this point in the history
  • Loading branch information
bebo-dot-dev committed May 3, 2020
1 parent a04780f commit 5adb457
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ usage: m3u-epg-editor-py3.py [-h] [--json_cfg [JSON_CFG]] [--m3uurl [M3UURL]]
[--groupmode [GROUPMODE]]
[--discard_channels [DISCARD_CHANNELS]]
[--include_channels [INCLUDE_CHANNELS]]
[--id_transforms [ID_TRANSFORMS]]
[--group_transforms [GROUP_TRANSFORMS]]
[--channel_transforms [CHANNEL_TRANSFORMS]]
[--range [RANGE]] [--sortchannels [SORTCHANNELS]]
Expand Down Expand Up @@ -121,6 +122,10 @@ optional arguments:
always be kept, effectively overriding of any other
group or channel exclusion configuration. (default:
None)
--id_transforms [ID_TRANSFORMS], -it [ID_TRANSFORMS]
A json array of key value pairs representing source
channel name values to target tvg-id values to be
transformed at processing time (default: [])
--group_transforms [GROUP_TRANSFORMS], -gt [GROUP_TRANSFORMS]
A json array of key value pairs representing source
group names to target groups names to be transformed
Expand Down
26 changes: 20 additions & 6 deletions m3u-epg-editor-py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def close(self):
help='Channels in the m3u to keep. Regex pattern matching is supported. Channel matched in '
'this argument will always be kept, effectively overriding of any other group or channel '
'exclusion configuration.')
arg_parser.add_argument('--id_transforms', '-it', nargs='?', default=[],
help='A json array of key value pairs representing source channel name values to target tvg-id values '
'to be transformed at processing time')
arg_parser.add_argument('--group_transforms', '-gt', nargs='?', default=[],
help='A json array of key value pairs representing source group names to target groups names '
'to be transformed at processing time')
Expand Down Expand Up @@ -271,6 +274,9 @@ def validate_args():
else:
args.include_channels = list()

if args.id_transforms:
args.id_transforms = json.loads(args.id_transforms)["id_transforms"]

if args.group_transforms:
args.group_transforms = json.loads(args.group_transforms)["group_transforms"]

Expand Down Expand Up @@ -357,6 +363,11 @@ def hydrate_args_from_json(args, json_cfg_file_path):
if not type(args.include_channels) is list:
abort_process('include_channels is expected to be a json array in {}'.format(json_cfg_file_path), 1, args)

if "id_transforms" in json_data:
args.id_transforms = json_data["id_transforms"]
else:
args.id_transforms = []

if "group_transforms" in json_data:
args.group_transforms = json_data["group_transforms"]
else:
Expand Down Expand Up @@ -553,11 +564,13 @@ def parse_m3u(m3u_filename, args):


# transforms the given string_value using the supplied transforms list of dictionary items
def transform_string_value(string_value, transforms):
def transform_string_value(string_value, compare_value, transforms):
for transform_item in transforms:
src_value = next(iter(transform_item.keys()))
replacement_value = next(iter(transform_item.values()))
if src_value in string_value:
if compare_value is not None and compare_value.startswith(src_value):
string_value = replacement_value
elif src_value in string_value:
string_value = string_value.replace(src_value, replacement_value)
return string_value

Expand Down Expand Up @@ -592,9 +605,10 @@ def filter_m3u_entries(args, m3u_entries):
channel_always_kept = is_item_matched(args.include_channels, m3u_entry.tvg_name)

if (group_included and not channel_discarded) or channel_always_kept:
m3u_entry.group_title = transform_string_value(m3u_entry.group_title, args.group_transforms)
m3u_entry.tvg_name = transform_string_value(m3u_entry.tvg_name, args.channel_transforms)
m3u_entry.name = transform_string_value(m3u_entry.name, args.channel_transforms)
m3u_entry.tvg_id = transform_string_value(m3u_entry.tvg_id, m3u_entry.tvg_name, args.id_transforms)
m3u_entry.group_title = transform_string_value(m3u_entry.group_title, None, args.group_transforms)
m3u_entry.tvg_name = transform_string_value(m3u_entry.tvg_name, None, args.channel_transforms)
m3u_entry.name = transform_string_value(m3u_entry.name, None, args.channel_transforms)
filtered_m3u_entries.append(m3u_entry)

output_str("filtered m3u contains {} items".format(len(filtered_m3u_entries)))
Expand Down Expand Up @@ -830,7 +844,7 @@ def create_new_epg(args, original_epg_filename, m3u_entries):
new_elem = SubElement(new_channel, elem.tag)
elem_text = elem.text
if new_elem.tag.lower() == "display-name":
elem_text = transform_string_value(elem_text, args.channel_transforms)
elem_text = transform_string_value(elem_text, None, args.channel_transforms)
new_elem.text = elem_text
for attr_key in elem.keys():
attr_val = elem.get(attr_key)
Expand Down

0 comments on commit 5adb457

Please sign in to comment.