Skip to content

Commit

Permalink
fixing minor errors and adding filepath regex #6
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Jun 14, 2021
1 parent 1e820d8 commit 36273e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 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.1-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-0.1.3-blue.svg?cacheSeconds=2592000" />
</p>

> A parser for m3u files.
Expand Down
50 changes: 33 additions & 17 deletions m3u_parser/m3u_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import aiohttp
import pycountry
import requests
import traceback
import time
import ssl
from urllib.parse import urlparse, unquote

try:
from helper import is_present, ndict_to_csv, run_until_completed
except ModuleNotFoundError:
from .helper import is_present, ndict_to_csv, run_until_completed

ssl.match_hostname = lambda cert, hostname: hostname == cert['subjectAltName'][0][1]
logging.basicConfig(
stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(message)s"
)
Expand Down Expand Up @@ -64,6 +65,7 @@ def __init__(self, useragent=None, timeout=5):
r"a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*["
r"a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:/\S*)?$"
)
self.__file_regex = re.compile(r"^[a-zA-Z]:\\((?:.*?\\)*).*\.[\d\w]{3,5}$|^(/[^/]*)+/?.[\d\w]{3,5}$")

def parse_m3u(self, path, check_live=True):
"""Parses the content of local file/URL.
Expand All @@ -85,15 +87,15 @@ def parse_m3u(self, path, check_live=True):
self.__content = requests.get(path).text
except:
logging.info("Cannot read anything from the url!!!")
exit()
return
else:
logging.info("Started parsing m3u file...")
try:
with open(unquote(path), errors="ignore") as fp:
self.__content = fp.read()
except FileNotFoundError:
logging.info("File doesn't exist!!!")
exit()
return

# splitting contents into lines to parse them
self.__lines = [
Expand All @@ -113,6 +115,7 @@ async def __run_until_completed(tasks):

def __parse_lines(self):
num_lines = len(self.__lines)
self.__streams_info.clear()
try:
self.__loop = asyncio.get_event_loop()
except RuntimeError:
Expand All @@ -128,25 +131,33 @@ def __parse_lines(self):
except BaseException:
pass
else:
self.__streams_info_backup = self.__lines.copy()
self.__streams_info_backup = self.__streams_info.copy()
self.__loop.run_until_complete(asyncio.sleep(0))
while self.__loop.is_running():
time.sleep(0.3)
if not self.__loop.is_running():
self.__loop.close()
break
logging.info("Parsing completed !!!")

async def __parse_line(self, line_num):
line_info = self.__lines[line_num]
stream_link = ""
streams_link = []
status = "BAD"
try:
for i in [1, 2]:
if self.__lines[line_num + i] and re.search(
self.__url_regex, self.__lines[line_num + i]
):
streams_link.append(self.__lines[line_num + i])
break
elif self.__lines[line_num + i] and re.search(
self.__file_regex, self.__lines[line_num + i]
):
status = "GOOD"
streams_link.append(self.__lines[line_num +i])
break
stream_link = streams_link[0]
except IndexError:
pass
Expand All @@ -166,8 +177,7 @@ async def __parse_line(self, line_num):
language_code = language_obj.alpha_3 if language_obj else ""

timeout = aiohttp.ClientTimeout(total=self.__timeout)
status = "BAD"
if self.__check_live:
if self.__check_live and status == "BAD":
try:
async with aiohttp.ClientSession() as session:
async with session.request(
Expand Down Expand Up @@ -389,7 +399,6 @@ def to_file(self, filename, format="json"):
:type format: str
:rtype: None
"""
logging.info("Saving to file...")
format = filename.split(".")[-1] if len(filename.split(".")) > 1 else format

def with_extension(name, ext):
Expand All @@ -398,16 +407,23 @@ def with_extension(name, ext):
return name
else:
return name + f".{ext}"

if format == "json":
data = json.dumps(self.__streams_info, indent=4)
with open(with_extension(filename, format), "w") as fp:
fp.write(data)

elif format == "csv":
ndict_to_csv(self.__streams_info, with_extension(filename, format))
else:
logging.info("Unrecognised format!!!")

filename = with_extension(filename, format)
logging.info("Saving to file: %s"%filename)
try:
if format == "json":
data = json.dumps(self.__streams_info, indent=4)
with open(filename, "w") as fp:
fp.write(data)
logging.info("Saved to file: %s"%filename)

elif format == "csv":
ndict_to_csv(self.__streams_info, filename)
logging.info("Saved to file: %s"%filename)
else:
logging.info("Unrecognised format!!!")
except Exception as error:
logging.warning(str(error))


if __name__ == "__main__":
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.1"
VERSION = "0.1.3"

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

Expand Down

0 comments on commit 36273e0

Please sign in to comment.