Skip to content

Commit

Permalink
use poetry to make a pip
Browse files Browse the repository at this point in the history
  • Loading branch information
4c0d3r authored and 4c0d3r committed Mar 2, 2022
1 parent 5b0ccdd commit 8f7ced9
Show file tree
Hide file tree
Showing 28 changed files with 1,094 additions and 203 deletions.
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[run]
branch = True
include = *namer*.py
omit = *tests*
include = namer/*, test/*
#[report]
#exclude_lines = pragma: no cover
#[html]
Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
**/.DS_Store
.vscode
__pycache__/
test/dest/*
test/working/*
test/watch/*
test/failed/*
.coverage
/htmlcov/
cov.xml
coverage.xml
UNKNOWN.egg-info/
dist/
29 changes: 18 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:impish as APP
FROM ubuntu:impish as BASE

ARG DEBIAN_FRONTEND=noninteractive

Expand All @@ -15,18 +15,26 @@ RUN apt-get update \
wget \
python3-pip \
python3-dev \
python3.9-venv \
curl \
&& rm -rf /var/lib/apt/lists/* \
&& rm -Rf /usr/share/doc && rm -Rf /usr/share/man \
&& apt-get clean

COPY . /opt/renamer/
RUN pip3 install pylint -r /opt/renamer/requirements.txt

FROM APP as TEST
RUN /usr/bin/python3 -m unittest discover -s /opt/renamer -p '*.py'
RUN pylint --rcfile=/opt/renamer/.pylintrc /opt/renamer/*.py
FROM APP
RUN rm -r /opt/renamer/test/
FROM BASE as BUILD
RUN mkdir /work/
COPY . /work/
RUN curl -sSL https://install.python-poetry.org | python3 - \
&& export PATH="/root/.local/bin:$PATH" \
&& cd /work/ \
&& poetry install \
&& poetry run pytest \
&& poetry run pylint namer \
&& poetry build
FROM BASE
COPY --from=BUILD /work/dist/namer-0.1.0.tar.gz /
RUN pip3 install /namer-0.1.0.tar.gz \
&& rm /namer-0.1.0.tar.gz

ARG BUILD_DATE
ARG GIT_HASH
Expand All @@ -35,6 +43,5 @@ ENV PYTHONUNBUFFERED=1
ENV NAMER_CONFIG=/config/namer.cfg
ENV BUILD_DATE=$BUILD_DATE
ENV GIT_HASH=$GIT_HASH
CMD ["/opt/renamer/namer_watchdog.py"]
ENTRYPOINT ["python3"]
ENTRYPOINT ["python3", "-m", "namer", "watchdog"]

8 changes: 4 additions & 4 deletions docker_build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
CLEAN=$(git diff-index --quiet HEAD; echo $?)
if [[ "${CLEAN}" == "0" ]]; then
#if [[ "${CLEAN}" == "0" ]]; then
GIT_HASH=$(git rev-parse --verify HEAD)
docker build . --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "GITHASH=${GIT_HASH}" -t porndb/namer
else
echo Not building for dirty code.
fi
#else
# echo Not building for dirty code.
#fi
Empty file added namer/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions namer/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Namer, the porn db file renamer. It can be a command line tool to rename mp4/mkvs and to embed tags in mp4s,
or a watchdog service to do the above watching a directory for new files. File names are assumed to be of
the form SITE.[YY]YY.MM.DD.String.of.performers.and.or.scene.name.<IGNORED_INFO>.[mp4|mkv]. In the name, read the
periods, ".", as any number of spaces " ", dashes "-", or periods ".".
Provided you have an access token to the porndb (free sign up) https://www.metadataapi.net/, this program will
attempt to match your file's name to search results from the porndb. Please note that the site must at least be
a substring of the actual site name on the porndb, and the date must be within one day or the release date on the
porndb for a match to be considered. If the log file flag is enabled then a <original file name minus ext>_namer.log
file will be written with all the potential matches sorted, descending by how closely the scene name/performer names
match the file.
"""
import pathlib
import sys
from typing import List
from namer.types import default_config
import namer.watchdog
import namer.namer

DESCRIPTION=namer.namer.DESCRIPTION+"""
The first argument should be 'watchdog', 'rename', or 'help' to see this message, for more help on rename, call
namer 'namer rename -h'
watchdog and help take no arguments (please see the config file example https://github.com/4c0d3r/namer/blob/main/namer.cfg)
"""

def create_default_config_if_missing():
"""
Find or create config.
"""
conffile = pathlib.Path(".namer.conf")
print("Creating default config file here: %s", conffile)
print("please edit the token or any other settings whose defaults you want changed.")


def main(arglist: List[str]):
"""
Call main method in namer.namer or namer.watchdog.
"""
arg1 = None if len(arglist) == 0 else arglist[0]
if arg1 == 'watchdog':
namer.watchdog.create_watcher(default_config()).run()
elif arg1 == 'rename':
namer.namer.main(arglist[1:])
elif arg1 in ['-h','help', None]:
print(DESCRIPTION)

if __name__ == "__main__":
main(sys.argv[1:])
File renamed without changes.
2 changes: 1 addition & 1 deletion namer_file_parser.py → namer/filenameparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import sys
from typing import List
from namer_types import FileNameParts
from namer.types import FileNameParts


def name_cleaner(name: str) -> str:
Expand Down
4 changes: 2 additions & 2 deletions namer_metadataapi.py → namer/metadataapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import urllib.request
import rapidfuzz
import requests
from namer_types import LookedUpFileInfo, Performer, FileNameParts, ComparisonResult, default_config
from namer_file_parser import parse_file_name
from namer.types import LookedUpFileInfo, Performer, FileNameParts, ComparisonResult, default_config
from namer.filenameparser import parse_file_name

logger = logging.getLogger('metadata')

Expand Down
2 changes: 1 addition & 1 deletion namer_moviexml.py → namer/moviexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from pathlib import Path
from lxml import objectify
from namer_types import LookedUpFileInfo, Performer
from namer.types import LookedUpFileInfo, Performer

def parse_movie_xml_file(xmlfile: Path) -> LookedUpFileInfo:
"""
Expand Down
4 changes: 2 additions & 2 deletions namer_mutagen.py → namer/mutagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import logging
from pathlib import Path
from mutagen.mp4 import MP4, MP4Cover
from namer_types import LookedUpFileInfo, NamerConfig
from namer_ffmpeg import get_resolution, update_audio_stream_if_needed
from namer.types import LookedUpFileInfo, NamerConfig
from namer.ffmpeg import get_resolution, update_audio_stream_if_needed

logger = logging.getLogger('metadata')

Expand Down
29 changes: 20 additions & 9 deletions namer.py → namer/namer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,29 @@
import sys
import logging
from typing import List
from namer_moviexml import parse_movie_xml_file
from namer.moviexml import parse_movie_xml_file

from namer_types import LookedUpFileInfo, NamerConfig, ComparisonResult, ProcessingResults, default_config, from_config
from namer_file_parser import parse_file_name
from namer_mutagen import update_mp4_file
from namer_metadataapi import get_poster, match
from namer.types import LookedUpFileInfo, NamerConfig, ComparisonResult, ProcessingResults, default_config, from_config
from namer.filenameparser import parse_file_name
from namer.mutagen import update_mp4_file
from namer.metadataapi import get_poster, match

logger = logging.getLogger('namer')

DESCRIPTION="""
Namer, the porndb local file renamer. It can be a command line tool to rename mp4/mkvs and to embed tags in mp4s,
or a watchdog service to do the above watching a directory for new files and moving matched files to a target location.
File names are assumed to be of the form SITE.[YY]YY.MM.DD.String.of.performers.and.or.scene.name.<IGNORED_INFO>.[mp4|mkv]. In the name, read the
periods, ".", as any number of spaces " ", dashes "-", or periods ".".
Provided you have an access token to the porndb (free sign up) https://www.metadataapi.net/, this program will
attempt to match your file's name to search results from the porndb. Please note that the site must at least be
a substring of the actual site name on the porndb, and the date must be within one day or the release date on the
porndb for a match to be considered. If the log file flag is enabled then a <original file name minus ext>_namer.log
file will be written with all the potential matches sorted, descending by how closely the scene name/performer names
match the file.
"""

def write_log_file(movie_file: Path, match_attempts: List[ComparisonResult]) -> str:
"""
Given porndb scene results sorted by how closely they match a file, write the contents
Expand Down Expand Up @@ -248,10 +262,7 @@ def main(arglist: List[str]):
Used to tag and rename files from the command line.
See usage function above.
"""
description="""
Names a file or directories largest mp4 or mkv file based on a lookup against metadataapi.net.
"""
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument("-c", "--configfile", help="config file, defaults first to env var NAMER_CONFIG,"
+" then local path namer.cfg, and finally ~/.namer.cfg.", type=pathlib.Path)
group = parser.add_mutually_exclusive_group(required=True)
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions namer_watchdog.py → namer/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from watchdog.observers.polling import PollingObserver
from watchdog.events import PatternMatchingEventHandler, FileSystemEvent, EVENT_TYPE_DELETED, EVENT_TYPE_MOVED
import schedule
from namer import move_to_final_location, process
from namer_types import NamerConfig, default_config
from namer.namer import move_to_final_location, process
from namer.types import NamerConfig, default_config

logger = logging.getLogger('watchdog')

Expand Down Expand Up @@ -182,7 +182,6 @@ def start(self):
starts a background thread to check for files.
"""
config = self.__namer_config
logger.info(str(config))
logger.info("Start porndb scene watcher.... watching: %s",config.watch_dir)
if os.environ.get('BUILD_DATE'):
build_date = os.environ.get('BUILD_DATE')
Expand Down Expand Up @@ -217,7 +216,9 @@ def create_watcher(namer_watchdog_config: NamerConfig) -> MovieWatcher:
"""
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
namer_watchdog_config.verify_watchdog_config()
logger.info(str(namer_watchdog_config))
if not namer_watchdog_config.verify_watchdog_config():
sys.exit(-1)
if namer_watchdog_config.retry_time is not None:
schedule.every().day.at(namer_watchdog_config.retry_time).do(lambda: retry_failed(namer_watchdog_config))
movie_watcher = MovieWatcher(namer_watchdog_config)
Expand Down
Loading

0 comments on commit 8f7ced9

Please sign in to comment.