Skip to content

Commit

Permalink
Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dachaz committed Feb 18, 2019
0 parents commit e4a9d16
Show file tree
Hide file tree
Showing 45 changed files with 1,430 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
__MACOSX
.vscode
.coverage
.pytest_cache
target/*
*.pyc
test/test-files

# WDC proprietary binary, see README.md
mksapkg*
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Dachaz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<span style="display:block;text-align:center">![Scenery.app icon](https://apps.dachaz.net/IMG/scenery/Scenery.png)</span>

# Scenery

A command-line tool that automates renaming of so-called "Scene Release" files by fetching episode names (from TVMaze) and which uses pattern-based generic building blocks (show name, season number, episode number, episode title) to format the output.

Essentially, a Python-based port of [Scenery.app](http://apps.dachaz.net/scenery/) which was originally available exclusively for macOS.

The intended goal of this port is to be compatible with more platforms, including NASes. (e.g. **WD My Cloud Mirror Gen 2**).

# Installation

## Using pip (cross-platform)

Almost all systems running Python have [pip](https://pip.pypa.io/). On those systems, installation is as easy as:

```bash
pip install scenery
```

## On WD My Cloud Mirror Gen 2

1. Download a precompiled binary from the releases page (e.g. `WDMyCloudMirrorGen2_scenery_1.0.0.bin(18022019)`)
1. Log into the _WD My Cloud Mirror_ admin interface of your device
1. Click "Apps"
1. Click "Install an app manually"
1. Choose the binary you downloaded previously
1. Wait for the confirmation

⚠️ This will only install the command-line utility. You'll still have to ssh into the device to use it!

# Usage
```
usage: scenery [-h] [-p PATTERN] [-s] [-e] [-o] [-d] [-v] [-f] path
positional arguments:
path Which path to process.
If a directory is given, it's scanned recursively and all files are processed.
If a file is given, only it is processed.
optional arguments:
-p PATTERN, --pattern PATTERN
Output format pattern. Syntax:
%a - Show name,
%s - Season #,
%n - Season #,
%t - Episode title
(default: "%a S%sE%n %t")
-s, --season-zeroes Leading zeroes in season numbers
-e, --episode-zeroes Leading zeroes in episode numbers
-o, --overwrite Overwrite existing target files
-d, --dry-run Do not do the actual renaming, but just show what
would happen instead
-v, --verbose Output successful actions as well
-f, --force Rename files even if the show name couldn't be
resolved
```

# Developer notes

The project has been implemented in Python 2 to be compatible with a fairly outdated NAS that is running it.

For the main part of the codebase, [PyBuilder](http://pybuilder.github.io) is used to do analysis (flake8, coverage), run the tests tests and bundle the package.

```bash
$ git clone https://github.com/dachaz/scenery
$ cd scenery
$ pyb
```

The NAS-specific part of the codebase is in the `wdc` folder.

To compile a binary that My Cloud OS3-based NAS will want to install, it needs to be packaged with `mksapkg`. Since this is a proprietary WD binary, I'm not allowed to include it in the codebase. Furthermore, `mksapkg` has a bunch of platform-specific dependencies that my machine didn't meet, so I bundled all of them in a Docker image, including a step that downloads `mksapkg` from WDC. Thus, to build a binary of `scenery` that will run on your NAS, you just need to have Docker running and run:

```bash
$ ./wdc/build.sh
```

To understand the full building process of a My Cloud OS3 binary, please refer to [My Cloud OS3 SDK](https://developer.westerndigital.com/develop/wd/sdk.html).

# License

Copyright © 2019 Dachaz. This software is licensed under the **MIT License**.
52 changes: 52 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pybuilder.core import init, use_plugin, Author

use_plugin('python.core')
use_plugin('python.flake8')
use_plugin('python.unittest')
use_plugin('python.coverage')
use_plugin('python.distutils')
use_plugin("python.install_dependencies")

authors = [Author('Dachaz', 'dachaz@dachaz.net')]
license = 'MIT'
name = 'scenery'
summary = 'A pattern-based scene release renamer'
description = """A command-line tool that automates renaming of so-called "Scene Release"
files by fetching episode names (from TVMaze) and which uses pattern-based generic building
blocks (show name, season number, episode number, episode title) to format the output.
"""
url = 'https://github.com/dachaz/scenery'
version = '1.0.0'
equires_python = "=2.7"

default_task = ["install_dependencies", "analyze", "publish"]


@init
def initialize(project):
project.build_depends_on("mockito")

project.set_property('dir_source_main_python', 'src')
project.set_property('dir_source_unittest_python', 'test')

project.set_property('flake8_break_build', True)
project.set_property('flake8_include_test_sources', True)
project.set_property('flake8_include_scripts', True)

project.get_property('coverage_exceptions').append('scenery.__main__') # From pyb
project.get_property('coverage_exceptions').append('scenery') # just __init__, relevant tests are in Scenery_tests.py

project.set_property('distutils_console_scripts', ['scenery = scenery:main'])
project.set_property('distutils_classifiers', [
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: End Users/Desktop',
'License :: OSI Approved :: MIT License',
'Topic :: Communications :: File Sharing',
'Topic :: Multimedia',
'Topic :: Multimedia :: Video',
'Topic :: Utilities'
])
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[wheel]
universal = True

[flake8]
ignore = E402,E501,W504
53 changes: 53 additions & 0 deletions src/scenery/MetadataTools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import re
import os.path
from scenery.model.SceneFileMetadata import SceneFileMetadata

REGEX_SPLITTER = "([sS]?\\d+?\\s?[eExX-]\\d+)"
REGEX_EPISODE_SPLITTER = "[sS]?(\\d+?)\\s?[eExX-](\\d+)"
REGEX_CLEANER = "[^a-zA-Z0-9]+$"


def extractMetadata(fileName):
# Normalise to whitespace-separated words
fileName = re.sub("[._]", " ", fileName)

# Look for relevant parts
parts = re.split(REGEX_SPLITTER, fileName)

# If we can't split into SHOWNAME - SEASON+EPISODE move on.
if len(parts) < 2:
return None

# Additionally clean the show name (remove special characters, capitalise)
show = re.sub(REGEX_CLEANER, "", parts[0]).title()

# TODO: Apply substitutions on the show name

# Get season and episode numbers
subparts = re.split(REGEX_EPISODE_SPLITTER, parts[1])
season = int(subparts[1])
episode = int(subparts[2])

meta = SceneFileMetadata(show=show, season=season, episode=episode)
return meta


def generateFilename(sceneFile, pattern, zeroesSeason=False, zeroesEpisodes=False):
meta = sceneFile.meta
# Keep the extension of the original file
extension = os.path.splitext(sceneFile.file)[1]

# Scenery.app's pattern syntax parsing magic
episodeString = ('%02d' if zeroesEpisodes else '%d') % meta.episode
replacements = {
'%a': str(meta.show),
'%s': ('%02d' if zeroesSeason else '%d') % meta.season,
'%n': episodeString,
'%t': meta.title or 'Episode %s' % episodeString,
}

out = pattern
for symbol, replacement in replacements.iteritems():
out = out.replace(symbol, replacement)

return out + extension
57 changes: 57 additions & 0 deletions src/scenery/Options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse


def getOptions(args):
parser = argparse.ArgumentParser(
prog='scenery',
description='Scenery - A pattern-based scene-release renamer.'
)

parser.add_argument('path',
action='store',
help='''Which path to process.
If a directory is given, it's scanned recursively and all files are processed.
If a file is given, only it is processed.''')

parser.add_argument('-p', '--pattern',
action='store',
dest='pattern',
default='%a S%sE%n %t',
help='''Output format pattern. Syntax:
%%a - Show name,
%%s - Season #,
%%n - Season #,
%%t - Episode title
(default: %%a S%%sE%%n %%t)''')

parser.add_argument('-s', '--season-zeroes',
action='store_true',
dest='zeroesSeason',
help='Leading zeroes in season numbers')

parser.add_argument('-e', '--episode-zeroes',
action='store_true',
dest='zeroesEpisodes',
help='Leading zeroes in episode numbers')

parser.add_argument('-o', '--overwrite',
action='store_true',
dest='overwrite',
help='Overwrite existing target files')

parser.add_argument('-d', '--dry-run',
action='store_true',
dest='dryRun',
help='Do not do the actual renaming, but just show what would happen instead')

parser.add_argument('-v', '--verbose',
action='store_true',
dest='verbose',
help='Output successful actions as well')

parser.add_argument('-f', '--force',
action='store_true',
dest='force',
help="Rename files even if the show name couldn't be resolved")

return parser.parse_args(args)
74 changes: 74 additions & 0 deletions src/scenery/Scenery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
import os
from scenery.model.SceneFile import SceneFile
from scenery.MetadataTools import extractMetadata, generateFilename
from scenery.TitleFetcher import getTitle


class Scenery():
def __init__(self, options):
self.options = options

def run(self):
sceneFiles = self.__listFiles(self.options.path)
for sceneFile in sceneFiles:
# Extract basic metadata from the filename
meta = extractMetadata(sceneFile.file)
if meta is None:
logging.warning("Couldn't parse %s", sceneFile.file)
continue
sceneFile.meta = meta
# Fetch the episode title from TVMaze
sceneFile.meta.title = getTitle(sceneFile.meta)
# Do the actual renaming
self.__renameFile(sceneFile)

def __listFiles(self, path):
sceneFiles = []
if os.path.isfile(path):
(root, fileName) = os.path.split(path)
sceneFile = SceneFile(fileName, root)
sceneFiles.append(sceneFile)
else:
for root, dirs, files in os.walk(path):
for fileName in sorted(files):
sceneFile = SceneFile(fileName, root)
sceneFiles.append(sceneFile)

return sceneFiles

def __renameFile(self, sceneFile):
if not sceneFile.meta.isComplete() and not self.options.force:
logging.warning("No title found for %s, skipping", sceneFile.file)
return

# Generate the target filename and the full path
targetFile = generateFilename(
sceneFile=sceneFile,
pattern=self.options.pattern,
zeroesSeason=self.options.zeroesSeason,
zeroesEpisodes=self.options.zeroesEpisodes)
targetPath = os.path.join(sceneFile.root, targetFile)
targetDir = os.path.dirname(targetPath)
sourcePath = os.path.join(sceneFile.root, sceneFile.file)

# Unless we're happy overwriting, make sure that the target file name is unique
if os.path.exists(targetPath) and not self.options.overwrite:
targetParts = os.path.splitext(targetPath)
i = 1
while os.path.exists(targetPath):
targetPath = "%s (%d)%s" % (targetParts[0], i, targetParts[1])
i += 1

if self.options.dryRun or self.options.verbose:
logging.getLogger().setLevel(logging.INFO)
logging.info("Renaming: %s to %s", sourcePath, targetPath)

if self.options.dryRun:
return

# Create all necessary sub folders
if not os.path.exists(targetDir):
os.makedirs(targetDir)
# Rename
os.rename(sourcePath, targetPath)
41 changes: 41 additions & 0 deletions src/scenery/TitleFetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import logging
import urllib2
from scenery.model.SceneFileMetadata import SceneFileMetadata
from scenery.model.SceneFileMetadataCache import SceneFileMetadataCache

cache = SceneFileMetadataCache()
TVMAZE_SEARCH_URL = "http://api.tvmaze.com/singlesearch/shows?embed=episodes&q="


def getTitle(meta):
fetchEpisodes(meta.show)
return cache.getTitle(meta)


def fetchEpisodes(showName):
# Make sure to fetch from TVMaze only once per application run
if cache.hasShow(showName):
return

try:
url = TVMAZE_SEARCH_URL + showName
response = urllib2.urlopen(url)
data = json.loads(response.read())

# If the server decided to return no episode data, mark the show as unknown
if '_embedded' not in data:
cache.markAsUnknown(showName)
else:
for episode in data['_embedded']['episodes']:
meta = SceneFileMetadata(
show=showName,
season=episode['season'],
episode=episode['number'],
title=episode['name']
)
cache.add(meta)

except BaseException:
logging.warning("Couldn't find show %s", showName)
cache.markAsUnknown(showName)
Loading

0 comments on commit e4a9d16

Please sign in to comment.