Skip to content

Commit

Permalink
fix language extensions and make schedule/paths customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
John Corser committed Jan 26, 2025
1 parent f4e18c7 commit 40190a5
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,18 @@ jobs:
image: mrorbitman/subsyncarr:latest
container_name: subsyncarr
volumes:
- ${MEDIA_PATH:-/path/to/your/media}:/scan_dir
# Any path configured with SCAN_PATHS env var must be mounted
# If no scan paths are specified, it defaults to scan `/scan_dir` like example below.
# - ${MEDIA_PATH:-/path/to/your/media}:/scan_dir
- /path/to/movies:/movies
- /path/to/tv:/tv
- /path/to/anime:/anime
restart: unless-stopped
environment:
- TZ=${TZ:-UTC}
- CRON_SCHEDULE=0 0 * * * # Runs every day at midnight by default
- SCAN_PATHS=/movies,/tv,/anime # Remember to mount these as volumes. Must begin with /. Default valus is `/scan_dir`
- EXCLUDE_PATHS=/movies/temp,/tv/downloads # Exclude certain sub-directories from the scan
```
Docker Hub URL: https://hub.docker.com/r/mrorbitman/subsyncarr/tags
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ COPY . .
RUN npm run build

# Create startup script
# Set default cron schedule (if not provided by environment variable)
ENV CRON_SCHEDULE="0 0 * * *"

# Create startup script with environment variable
RUN echo '#!/bin/bash\n\
# Add cron job\n\
echo "0 0 * * * cd /app && /usr/local/bin/node /app/dist/index.js >> /var/log/cron.log 2>&1" > /etc/cron.d/subsyncarr\n\
echo "${CRON_SCHEDULE} cd /app && /usr/local/bin/node /app/dist/index.js >> /var/log/cron.log 2>&1" > /etc/cron.d/subsyncarr\n\
chmod 0644 /etc/cron.d/subsyncarr\n\
crontab /etc/cron.d/subsyncarr\n\
\n\
Expand Down
10 changes: 9 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ services:
image: mrorbitman/subsyncarr:latest
container_name: subsyncarr
volumes:
- ${MEDIA_PATH:-/path/to/your/media}:/scan_dir
# Any path configured with SCAN_PATHS env var must be mounted
# If no scan paths are specified, it defaults to scan `/scan_dir` like example below.
# - ${MEDIA_PATH:-/path/to/your/media}:/scan_dir
- /path/to/movies:/movies
- /path/to/tv:/tv
- /path/to/anime:/anime
restart: unless-stopped
environment:
- TZ=${TZ:-UTC}
- CRON_SCHEDULE=0 0 * * * # Runs every day at midnight by default
- SCAN_PATHS=/movies,/tv,/anime # Remember to mount these as volumes. Must begin with /. Default valus is `/scan_dir`
- EXCLUDE_PATHS=/movies/temp,/tv/downloads # Exclude certain sub-directories from the scan
46 changes: 46 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export interface ScanConfig {
includePaths: string[];
excludePaths: string[];
}

function validatePath(path: string): boolean {
// Add any path validation logic you need
return path.startsWith('/') && !path.includes('..');
}

export function getScanConfig(): ScanConfig {
const scanPaths = process.env.SCAN_PATHS?.split(',').filter(Boolean) || ['/scan_dir'];
const excludePaths = process.env.EXCLUDE_PATHS?.split(',').filter(Boolean) || [];

// Validate paths
const validIncludePaths = scanPaths.filter((path) => {
const isValid = validatePath(path);
if (!isValid) {
console.warn(`Invalid include path: ${path}`);
}
return isValid;
});

const validExcludePaths = excludePaths.filter((path) => {
const isValid = validatePath(path);
if (!isValid) {
console.warn(`Invalid exclude path: ${path}`);
}
return isValid;
});

if (validIncludePaths.length === 0) {
console.warn('No valid scan paths provided, defaulting to /scan_dir');
validIncludePaths.push('/scan_dir');
}

console.log('Scan configuration:', {
includePaths: validIncludePaths,
excludePaths: validExcludePaths,
});

return {
includePaths: validIncludePaths,
excludePaths: validExcludePaths,
};
}
14 changes: 12 additions & 2 deletions src/findAllSrtFiles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { readdir } from 'fs/promises';
import { extname, join } from 'path';
import { ScanConfig } from './config';

export async function findAllSrtFiles(dir: string): Promise<string[]> {
export async function findAllSrtFiles(config: ScanConfig): Promise<string[]> {
const files: string[] = [];

async function scan(directory: string): Promise<void> {
// Check if this directory should be excluded
if (config.excludePaths.some((excludePath) => directory.startsWith(excludePath))) {
return;
}

const entries = await readdir(directory, { withFileTypes: true });

for (const entry of entries) {
Expand All @@ -18,6 +24,10 @@ export async function findAllSrtFiles(dir: string): Promise<string[]> {
}
}

await scan(dir);
// Scan all included paths
for (const includePath of config.includePaths) {
await scan(includePath);
}

return files;
}
16 changes: 15 additions & 1 deletion src/findMatchingVideoFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ export function findMatchingVideoFile(srtPath: string): string | null {
const directory = dirname(srtPath);
const srtBaseName = basename(srtPath, '.srt');

// Try to find a video file with the same name but different extension
// Try exact match first
for (const ext of VIDEO_EXTENSIONS) {
const possibleVideoPath = join(directory, `${srtBaseName}${ext}`);
if (existsSync(possibleVideoPath)) {
return possibleVideoPath;
}
}

// Progressive tag removal - split by dots and try removing one segment at a time
const segments = srtBaseName.split('.');
while (segments.length > 1) {
segments.pop(); // Remove the last segment
const baseNameToTry = segments.join('.');

for (const ext of VIDEO_EXTENSIONS) {
const possibleVideoPath = join(directory, `${baseNameToTry}${ext}`);
if (existsSync(possibleVideoPath)) {
return possibleVideoPath;
}
}
}

return null;
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { findAllSrtFiles } from './findAllSrtFiles';
import { findMatchingVideoFile } from './findMatchingVideoFile';
import { generateFfsubsyncSubtitles } from './generateFfsubsyncSubtitles';
import { generateAutosubsyncSubtitles } from './generateAutosubsyncSubtitles';
import { getScanConfig } from './config';

const SCAN_DIR = '/scan_dir';

Expand All @@ -12,7 +13,8 @@ async function main(): Promise<void> {

try {
// Find all .srt files
const srtFiles = await findAllSrtFiles(scanDir);
const scanConfig = getScanConfig();
const srtFiles = await findAllSrtFiles(scanConfig);
console.log(`${new Date().toLocaleString()} Found ${srtFiles.length} SRT files`);

// Process each SRT file
Expand Down

0 comments on commit 40190a5

Please sign in to comment.