Skip to content

Commit

Permalink
added support for saving to m3u format #7
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Jul 20, 2021
1 parent b6ec317 commit 5e284b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">Welcome to m3u_parser</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-0.1.4-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-0.1.5-blue.svg?cacheSeconds=2592000" />
</p>

> A parser for m3u files.
Expand Down
2 changes: 1 addition & 1 deletion m3u_parser/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# get matching regex from content
def get_by_regex(regex, content):
match = re.search(re.compile(regex, flags=re.IGNORECASE), content)
return match.group(1) if match else ""
return match.group(1).strip() if match else ""


def is_dict(item, ans=None):
Expand Down
38 changes: 38 additions & 0 deletions m3u_parser/m3u_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,38 @@ def get_random_stream(self, random_shuffle: bool = True):
random.shuffle(self._streams_info)
return random.choice(self._streams_info)

def _get_m3u_content(self) -> str:
"""Save the streams information list to m3u file.
It saves the streams information list to m3u file.
:rtype: None
"""
if len(self._streams_info) == 0:
return ""
content = "#EXTM3U\n"
for stream_info in self._streams_info:
country = stream_info.get("country", {}).get("code", "")
language = stream_info.get("language", {}).get("name", "")
content_list = [
stream_info["tvg"]["id"],
stream_info["tvg"]["name"],
country,
language,
stream_info["logo"],
stream_info["tvg"]["url"],
stream_info["category"],
stream_info["name"],
stream_info["url"],
]
content += (
'#EXTINF:-1 tvg-id="{}" tvg-name="{}" tvg-country="{}" tvg-lang="{}" '
'tvg-logo="{}" tvg-url="{}" group-title="{}", {}\n{}\n'
)
content = content.replace('tvg-country="" ', "").replace('tvg-lang="" ', "")
content = content.format(*content_list)
return content

def to_file(self, filename: str, format: str = "json"):
"""Save to file (CSV or JSON)
Expand Down Expand Up @@ -428,6 +460,12 @@ def with_extension(name, ext):
elif format == "csv":
ndict_to_csv(self._streams_info, filename)
logging.info("Saved to file: %s" % filename)

elif format == "m3u":
content = self._get_m3u_content()
with open(filename, "w") as fp:
fp.write(content)
logging.info("Saved to file: %s" % filename)
else:
logging.error("Unrecognised format!!!")
except Exception as error:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
EMAIL = "[email protected]"
AUTHOR = "Pawan Paudel"
REQUIRES_PYTHON = ">=3.0"
VERSION = "0.1.4"
VERSION = "0.1.5"

REQUIRED = ["requests", "asyncio", "aiohttp", "pycountry"]

Expand Down

0 comments on commit 5e284b1

Please sign in to comment.