Archived
1
0
Disbranĉigi 0
This commit is contained in:
Jaidyn Lev 2019-01-20 14:51:47 -06:00
commit 7049ef8054
3 changed files with 270 additions and 0 deletions

47
README.txt Normal file
View File

@ -0,0 +1,47 @@
===============================================================================
OSUBDL Fetch subs, senprobleme!
===============================================================================
It's kind of annoying to visit OpenSubtitle's website.
First off, their site's shit.
Second off, it uses Javascript.
Let's get around that.
With `osubdl`, you can download subtitles for a given file in the
given language "automagically"~
----------------------------------------
PRE-REQUISITES
----------------------------------------
You'll need:
* To compile "oshash.h" to "oshash" and put it in your $PATH
* a POSIX-compatible shell (tested with `pdksh` and `bash`)
* "unzip" installed
* "curl" installed"
* Some videos <3
----------------------------------------
USAGE
----------------------------------------
Just run "osubdl" like so:
osubdl file [language] [destination]
The file should be, obviously, some TV episode or movie's video file.
The language defaults to "eng" (english), but you can choose any
three-char language code.
The destination defaults to the file's name, but with "srt" as the
file-extension.
----------------------------------------
BORING STUFF
----------------------------------------
License is CC-0
Author is Jenga Phoenix <jadedctrl@teknik.io>
Sauce is at https://git.eunichx.us/osubdl

34
oshash.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
/* This file is from https://github.com/tmadeira/downloadsubtitle */
/* License? I don't know. ;___: */
void usage(char *name) {
printf("Usage: %s <file>\n", name);
exit(1);
}
int main(int argc, char *argv[]) {
unsigned long long buf[16384], c = 0;
FILE *in;
int i;
if (argc != 2) {
usage(argv[0]);
}
in = fopen(argv[1], "rb");
if (in == NULL) {
usage(argv[0]);
}
fread(buf, 8192, 8, in);
fseek(in, -65536, SEEK_END);
fread(&buf[8192], 8192, 8, in);
for (i = 0; i < 16384; i++) {
c+= buf[i];
}
c+= ftell(in);
fclose(in);
printf("%016llx\n", c);
return 0;
}

189
osubdl Normal file
View File

@ -0,0 +1,189 @@
#!/bin/sh
# --------------------------------------
# generic
# NIL --> STRING
# Read from stdin until eof hit; return all input--
# good for writing functions that take piped info.
function reade {
local stack=""
while read input; do
stack="$(printf '%s\n%s' "$stack" "$input")"
done
echo "$stack"
}
# STRING STRING --> STRING
# Replace the file-extension of a filename with another.
function replace_file_ext {
local filename="$1"
local ext="$2"
local new_filename="$(echo "$filename" | sed 's%\....$%\.'"$ext"'%')"
if test "$new_filename" = "$filename"; then
echo "${filename}.${ext}"
else
echo "${new_filename}"
fi
}
#---------------------------------------
# STRING --> STRING
# Hash a video file.
function vhash {
local file="$1"
oshash "$file"
}
#---------------------------------------
# STRING STRING --> STRING
# Get search results/subtitle-page of a file of certain hash and language.
# Make sure the second argument, $lang, is set to the 3-char language code,
# *not* the 2-char. I.E., "esp" over "es"
function search {
local hash="$1"
local lang="$2"
local url="https://www.opensubtitles.org/en/search"
url="${url}/sublanguageid-${lang}"
url="${url}/moviehash-${hash}"
curl --silent "$url"
}
# |STRING --> [BOOLEAN]
# Return 0 if page is a search-results; 1 if it's a subtitle page.
# Sometimes, when searching for a file, it'll redirect you to the only result;
# other times, it lists results. So we need this. :p
function search_resultsp {
local html="$(reade)"
local result="$(echo "$html" | grep "Subtitles -" | grep -v "img wid")"
if test -z "$result"; then
return 1
else
return 0
fi
}
# --------------------------------------
# |STRING --> STRING
# Return the dl.opensubtitles URL, from the given subtitle page HTML.
function subtitle_dl_url {
local html="$(reade)"
echo "$html" \
| grep "subtitleserve" \
| grep "directUrl" \
| head -1 \
| sed 's/.*=.//' \
| sed 's/.;//' \
| sed 's%.*/%%' \
| sed 's%^%https://dl.opensubtitles.org/en/download/sub/%'
}
# STRING --> STRING
# Return the regular subtitle-page URL from the dl.opensubtitles URL.
function subtitle_url {
local dl_url="$1"
local temp="$(mktemp)"
curl --verbose "$dl_url" \
2> $temp
grep "location:" $temp \
| sed 's%.*location: %%'
}
# --------------------------------------
# STRING STRING --> NIL
# Download the subtitle zip of a video, from subpage URL and dl URL.
function subtitle_get {
local url="$1"
local dl_url="$2"
local dest="$3"
local temp="/tmp/$(echo "$RANDOM")"
mkdir "$temp"
local useragent='Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101'
useragent="$useragent Firefox/52.0"
# curl line shamelessly borrowed from jwilk/opensubtitles-dl <3
curl --show-error --user-agent "$useragent" --referer "$url" "$dl_url" \
> "${temp}sub.zip"
unzip -qq -d "$temp" "${temp}/sub.zip"
mv ${temp}/*.srt "$dest"
rm -rf "${temp}"
}
# --------------------------------------
# STRING STRING PATH --> NIL
# Download a file's subtitles in given language to given path.
function get_file_subs {
local file="$1"
local language="$2"
local dest="$3"
local hash="$(vhash "$file")"
local html="$(search "$hash" "$language")"
if echo "$html" | search_resultsp; then
# echo "$html"
echo "No results found."
return 3
fi
local dl_url="$(echo "$html" | subtitle_dl_url | tr -d '\r')"
local url="$(subtitle_url "$dl_url" | tr -d '\r')"
subtitle_get "$url" "$dl_url" "$dest"
}
# --------------------------------------
# invocation
function usage {
echo "usage: osubdl file [language] [path]"
echo " 'file' should be a video file."
echo " 'language' should be a three-char language code."
echo " 'path' should be the destination of the srt file."
exit 2
}
# --------------------------------------
FILE="$1"
LANGUAGE="$2"
DESTINATION="$3"
if test -z "$FILE"; then
usage
elif test -z "$LANG"; then
LANGUAGE="eng"
elif test -z "$DESTINATION"; then
DESTINATION="$(replace_file_ext "$FILE" "srt")"
fi
# --------------------------------------
echo "$FILE --> $DESTINATION"
get_file_subs "$FILE" "$LANGUAGE" "$DESTINATION"