Skip to content

Commit

Permalink
Introduced Linting (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
coldsofttech authored Apr 8, 2024
1 parent c8c9e8b commit b3edfcf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 120
exclude = .git,__pycache__,venv
35 changes: 33 additions & 2 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,43 @@ on:
- '*'

jobs:
lint:
name: Python Linux (flake8)
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Run Lint
run: flake8 --verbose --color auto --count --statistics --format=json --output-file=flake8-report.json || echo "::set-output name=flake8_failed::true"
continue-on-error: true

- name: Upload Report
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: flake8-report
path: flake8-report.json

build_and_test:
name: Build and Test
runs-on: ubuntu-latest
needs: lint

steps:
- name: Checkout Repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set Branch Name
run: |
Expand All @@ -20,7 +51,7 @@ jobs:
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
- name: Setup Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: '3.10'

Expand Down
6 changes: 3 additions & 3 deletions pym3u8downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
]
__author__ = "coldsofttech"
__description__ = """
M3U8 Downloader is a Python class designed to download and concatenate video files from M3U8 playlists.
This class provides functionality to handle M3U8 playlist files, download video segments, concatenate them
M3U8 Downloader is a Python class designed to download and concatenate video files from M3U8 playlists.
This class provides functionality to handle M3U8 playlist files, download video segments, concatenate them
into a single video file, and manage various error conditions.
"""
__name__ = "pym3u8downloader"
__version__ = "0.1.0"
__version__ = "0.1.1"

from pym3u8downloader.__main__ import M3U8Downloader, M3U8DownloaderError
33 changes: 17 additions & 16 deletions pym3u8downloader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def get_content_length(file: str) -> int:
:return: The content length of the file, or 0 if the file is not found or cannot be accessed.
:rtype: int
"""

def get_final_url(url: str) -> str:
"""
Get the final URL after following redirects.
Expand Down Expand Up @@ -461,7 +462,7 @@ def _concatenate_video_files(self) -> None:
"""
Concatenates the downloaded video files into a single video file.
"""
self._debug_logger.debug(f'Build started') if self._debug else None
self._debug_logger.debug('Build started') if self._debug else None
total_files = len(self._playlist_files)
completed_files = 0
playlist_file_path = os.path.join(self._temp_directory_path, 'playlist_files')
Expand Down Expand Up @@ -498,8 +499,8 @@ def _configure_debug_logger(self) -> None:

if self._debug:
format_string = (
f'%(time)s :: %(logger_name)s :: %(level_name)s :: %(file_name)s :: %(class_name)s'
f' :: %(function_name)s :: %(thread_name)s :: %(message)s'
'%(time)s :: %(logger_name)s :: %(level_name)s :: %(file_name)s :: %(class_name)s'
' :: %(function_name)s :: %(thread_name)s :: %(message)s'
)
formatter = pyloggermanager.formatters.DefaultFormatter(format_str=format_string)
handler = pyloggermanager.handlers.FileHandler(
Expand All @@ -522,7 +523,7 @@ def _create_temp_directory(self) -> None:
self._debug_logger.debug(f'Temporary Directory Full Path: {self._temp_directory_path}') if self._debug else None
self._playlist_files = []

self._debug_logger.debug(f'Creating temporary directory') if self._debug else None
self._debug_logger.debug('Creating temporary directory') if self._debug else None
os.makedirs(self._temp_directory_path, exist_ok=True)

def _download_and_write(self, sequence: int, url: str, files: TextIO) -> None:
Expand Down Expand Up @@ -558,7 +559,7 @@ def _download_files_with_progress(self):
"""
Downloads the video files from the playlist with progress indication.
"""
self._debug_logger.debug(f'Download started') if self._debug else None
self._debug_logger.debug('Download started') if self._debug else None
total_files = len(self._playlist_files)
completed_files = 0

Expand Down Expand Up @@ -597,7 +598,7 @@ def _download_index_file(self) -> bool:
index_file_path = os.path.join(self._temp_directory_path, 'index.m3u8')
self._debug_logger.debug(f'Index File Path: {index_file_path}') if self._debug else None
UtilityClass.download_file(self._input_file_path, index_file_path)
self._debug_logger.debug(f'Index file downloaded') if self._debug else None
self._debug_logger.debug('Index file downloaded') if self._debug else None
return True
except requests.RequestException as e:
self._debug_logger.debug(f'Index file download failed. {e}') if self._debug else None
Expand Down Expand Up @@ -630,7 +631,7 @@ def _get_playlist_files(self) -> list[str]:
:return: The list of video file URLs.
:rtype: list[str]
"""
self._debug_logger.debug(f'Gathering playlist from input file') if self._debug else None
self._debug_logger.debug('Gathering playlist from input file') if self._debug else None
playlist_files: list[str] = []
with open(os.path.join(self._temp_directory_path, 'index.m3u8'), 'r') as index_file:
for line in index_file:
Expand All @@ -653,7 +654,7 @@ def _get_playlist_size(self) -> int:
:return: The total size of the playlist files in bytes.
:rtype: int
"""
self._debug_logger.debug(f'Verify started') if self._debug else None
self._debug_logger.debug('Verify started') if self._debug else None
total_files = len(self._playlist_files)
completed_files = 0

Expand Down Expand Up @@ -689,7 +690,7 @@ def _is_master_file(self) -> bool:
:rtype: bool
"""
is_master = False
self._debug_logger.debug(f'Verifying if input file is master') if self._debug else None
self._debug_logger.debug('Verifying if input file is master') if self._debug else None
with open(os.path.join(self._temp_directory_path, 'index.m3u8'), 'r') as index_file:
for line in index_file:
if line.startswith('#EXT-X-STREAM-INF'):
Expand All @@ -703,17 +704,17 @@ def _remove_temp_directory(self) -> None:
"""
Removes the temporary directory used for storing downloaded files.
"""
self._debug_logger.debug(f'Cleaning temporary directory') if self._debug else None
self._debug_logger.debug('Cleaning temporary directory') if self._debug else None
shutil.rmtree(self._temp_directory_path)

def _validate(self) -> None:
"""
Validates the input parameters and conditions before starting the download process.
"""
if not UtilityClass.is_url(self._input_file_path):
raise ValueError(f'input_file_path is not a valid url.')
raise ValueError('input_file_path is not a valid url.')
elif not UtilityClass.is_m3u8_url(self._input_file_path):
raise ValueError(f'input_file_path is not a valid m3u8 url.')
raise ValueError('input_file_path is not a valid m3u8 url.')
elif not UtilityClass.is_internet_connected():
raise M3U8DownloaderError('Internet connection required.')

Expand Down Expand Up @@ -744,14 +745,14 @@ def download_playlist(self) -> None:
self._playlist_files = self._get_playlist_files()

if not self._skip_space_check:
self._debug_logger.debug(f'Verifying if required space '
f'is available for download') if self._debug else None
self._debug_logger.debug('Verifying if required space '
'is available for download') if self._debug else None
playlist_size = self._get_playlist_size()
self._debug_logger.debug(f'Required space: {playlist_size}') if self._debug else None
self._check_required_disk_space(playlist_size)
self._debug_logger.debug(f'Required space is available') if self._debug else None
self._debug_logger.debug('Required space is available') if self._debug else None
else:
self._debug_logger.debug(f'Verification of space required skipped') if self._debug else None
self._debug_logger.debug('Verification of space required skipped') if self._debug else None

self._download_files_with_progress()
self._concatenate_video_files()
Expand Down

0 comments on commit b3edfcf

Please sign in to comment.