Initial commit

This commit is contained in:
Kyle Brown 2020-12-15 19:43:09 -08:00
parent cc48802cc6
commit 3fd71c0123
8 changed files with 170 additions and 0 deletions

1
.gitignore vendored
View file

@ -2,4 +2,5 @@
nimcache/
nimblecache/
htmldocs/
renamer

25
Makefile Normal file
View file

@ -0,0 +1,25 @@
##
# Renamer
#
# @file
# @version 0.1
SRC = renamer.nim
BIN = renamer
debug:
nimble build '-d:ssl --cc:clang'
release:
nimble build '-d:ssl --cc:clang -d:release'
clean:
rm -f ./${BIN}
run:
./${BIN}
test:
nimble test
# end

0
[BLA] Test anime 02 [590p].mkv Executable file
View file

15
renamer.nimble Normal file
View file

@ -0,0 +1,15 @@
# Package
version = "0.1.0"
author = "Kyle Brown"
description = "Renames files"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
bin = @["renamer"]
# Dependencies
requires "nim >= 1.4.2"
requires "zero_functional >= 1.2.1"

107
src/renamer.nim Normal file
View file

@ -0,0 +1,107 @@
import lists
import os
import re
import strformat
import strutils
import terminal
import zero_functional
proc findYear*(input: string):string {. noSideEffect .} =
let seq_year = input.split(".") --> filter(re"^\d{4}$" in it)
seq_year.join()
proc findExt*(input: string):string {. noSideEffect .} =
let seq_input = input.split(".")
seq_input[len(seq_input)-1].join()
proc findMovie*(input: string):string {. noSideEffect .} =
let year = findYear(input)
let seq_unsplit_title = input.split(year) --> sub(0, 0)
let seq_title = seq_unsplit_title.join().replace(".", " ")
let ext = findExt(input)
seq_title.join() & "(" & year & ")" & "." & ext
proc isMovie*(input: string):bool {. noSideEffect .} =
not isEmptyOrWhitespace(findYear(input))
proc findSubber*(input: string):string {. noSideEffect .} =
let seq_input = input.split(" ")
let first_str = seq_input[0].join()
if first_str.contains("[") and first_str.contains("]"):
return first_str
else:
return ""
proc isSubber*(input: string):bool {. noSideEffect .} =
not isEmptyOrWhitespace(findSubber(input))
proc findEP*(input: string):string {. noSideEffect .} =
let seq_ep = input.split(" ") --> filter(re"^\d{2}$" in it)
seq_ep.join()
proc isEP*(input: string):bool {. noSideEffect .} =
not isEmptyOrWhitespace(findEP(input))
proc findAnime*(input: string, season: string):string {. noSideEffect .} =
let ep = findEP(input)
var fmtSeason = ""
if not isEmptyOrWhiteSpace(season):
fmtSeason = fmt"S{season}E"
# Strips information to the right of EP
let seq_unsplit_title = input.split(ep) --> sub(0, 0)
let rstripped_title = seq_unsplit_title.join(sep=" ")
# Strips subber
let seq_title = rstripped_title.split(" ") --> sub(1, ^1)
let title = seq_title.join(sep=" ")
let ext = findExt(input)
title & fmtSeason & ep & "." & ext
proc findAnimeMovie*(input: string):string {. noSideEffect .} =
# Strips subber
let seq_title = input.split(" ") --> sub(1, ^1)
seq_title.join(sep=" ")
proc isAnime*(input: string):bool {. noSideEffect .} =
not isEmptyOrWhitespace(findSubber(input))
var file_list = initDoublyLinkedList[string]()
when isMainModule:
# Initializes season to be blank. Can be overridden with command
var
season = ""
fromDir = ""
toDir = ""
# Parses for a season on command line
let params = commandLineParams()
for i in 0 .. (params.len()-1):
if params[i] == "-s" or params[i] == "--season":
# Sets format as S00E00 format
season = params[i+1]
elif params[i] == "-f" or params[i] == "--from":
fromDir = params[i+1] & "/"
elif params[i] == "-t" or params[i] == "--to":
toDir = params[i+1] & "/"
if not isatty(stdin):
while true:
try:
file_list.append(readLine(stdin))
except EOFError:
break
for i in file_list:
if isMovie(i):
echo fmt"""mv "{fromDir}{i}" "{toDir}{findMovie(i)}""""
if isAnime(i):
if isEP(i):
echo fmt"""mv "{fromDir}{i}" "{toDir}{findAnime(i, season)}""""
else: discard # echo "Anime Movie?"

0
test.movie.2033.[960p].lol Executable file
View file

1
tests/config.nims Normal file
View file

@ -0,0 +1 @@
switch("path", "$projectDir/../src")

21
tests/test1.nim Normal file
View file

@ -0,0 +1,21 @@
# This is just an example to get you started. You may wish to put all of your
# tests into a single file, or separate them into multiple `test1`, `test2`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.
import src/renamer
let animeTestStr = "[BLA] Test show 01 [1080p].mkv"
let animeMovieTestStr = "[BLA] Test Movie [1080p].mkv"
let movieTestStr = "Gotta.go.fast.2043.[2048p].mp4"
assert isSubber(animeTestStr) == true
assert isAnime(animeTestStr) == true
assert findEP(animeTestStr) == "01"
assert findSubber(animeTestStr) == "[BLA]"
assert findYear(movieTestStr) == "2043"
assert findExt(movieTestStr) == "mp4"
assert findAnime(animeTestStr, "") == "Test show 01.mkv"
assert findAnime(animeTestStr, "03") == "Test show S03E01.mkv"
assert findAnimeMovie(animeMovieTestStr) == "Test Movie [1080p].mkv"