From 2f08fc4f549989f30fc6f9a96a00b6132eb8040c Mon Sep 17 00:00:00 2001 From: Martin Potier Date: Mon, 21 Aug 2017 11:56:07 +0200 Subject: [PATCH] Moar scripts --- scripts/prefix/bin/mkstatus.sh | 67 +++ scripts/prefix/bin/msmtp-runqueue-on-notify | 11 + scripts/prefix/bin/subdl | 485 ++++++++++++++++++++ scripts/prefix/bin/task-add-birthdays.sh | 34 ++ scripts/prefix/bin/yt | 5 +- 5 files changed, 599 insertions(+), 3 deletions(-) create mode 100755 scripts/prefix/bin/mkstatus.sh create mode 100755 scripts/prefix/bin/msmtp-runqueue-on-notify create mode 100755 scripts/prefix/bin/subdl create mode 100755 scripts/prefix/bin/task-add-birthdays.sh diff --git a/scripts/prefix/bin/mkstatus.sh b/scripts/prefix/bin/mkstatus.sh new file mode 100755 index 0000000..ba4993f --- /dev/null +++ b/scripts/prefix/bin/mkstatus.sh @@ -0,0 +1,67 @@ +#!/bin/sh +# Samæ (ↄ) 2016 - Public domain + +# Monitors are placed in +MONITORD="/tmp/monitors" +#MONITORLIST="battery mail room volume wireless xmonad cpu" +MONITORLIST="xmonad" + +battery() { + #echo "$(cat $MONITORD/battery) %" + cat /sys/class/power_supply/BAT0/capacity +} + +clock() { + date '+%A %d %B %H:%M %Z' +} + +mail() { + sed 's/0/None/' < "$MONITORD/mail" +} + +cpu() { + echo "$(cat $MONITORD/cpu) %" +} + +room() { + echo "$(cat $MONITORD/room) %" +} + +volume() { + sed 's/-1 %/Mute/' <<< "$(cat $MONITORD/volume) %" +} + +wireless() { + cat "$MONITORD/wireless" +} + +xmonad() { + cat "$MONITORD/xmonad" +} + +# Loop foreva; wait for events. +while :; do + buf="%{S0}" + buf="${buf} $(xmonad)  " + buf="${buf}%{r}" + #buf="${buf} %{+u}CPU:%{-u} %{F#EEE8D5}$(cpu) %{F-}  " + #buf="${buf} %{+u}WFI:%{-u} %{F#EEE8D5}$(wireless) %{F-}  " + #buf="${buf} %{+u}CRL:%{-u} %{F#EEE8D5}$(mail) %{F-}  " + #buf="${buf} %{+u}OQP:%{-u} %{F#EEE8D5}$(room) %{F-}  " + #buf="${buf} %{+u}VPN:%{-u} %{F#EEE8D5}$(network) %{F-}  " + #buf="${buf} %{+u}RAM:%{-u} %{F#EEE8D5}$(memused) % %{F-}  " + #buf="${buf} %{+u}MPD:%{-u} %{F#EEE8D5}$(nowplaying) %{F-}  " + #buf="${buf} %{+u}VOL:%{-u} %{F#EEE8D5}$(volume) %{F-}  " + buf="${buf} %{+u}FUEL:%{-u} %{F#EEE8D5}$(battery) %{F-}  " + buf="${buf} %{F#6C71C4}$(clock) %{F-}" + + echo $buf + + cd ${MONITORD} + inotifywait -q -q -e create,modify ${MONITORLIST} + + # Ugly hack to prevent reading the monitor file to early (thus catching it + # while it is empty ^^) Waiting time is set to approximatively 1/27 of a + # second ie a frame at 27fps, so that it doesn't introduce a feeling of lag + sleep 0.03s +done diff --git a/scripts/prefix/bin/msmtp-runqueue-on-notify b/scripts/prefix/bin/msmtp-runqueue-on-notify new file mode 100755 index 0000000..d87ac0f --- /dev/null +++ b/scripts/prefix/bin/msmtp-runqueue-on-notify @@ -0,0 +1,11 @@ +#!/bin/sh + +while true +do + # Wait here + inotifywait -qq -e modify /home/eeva/.msmtpqueue + + # Send + echo "Queue modified. Calling runqueue…" + /home/eeva/prefix/bin/msmtp-runqueue +done diff --git a/scripts/prefix/bin/subdl b/scripts/prefix/bin/subdl new file mode 100755 index 0000000..2bab98d --- /dev/null +++ b/scripts/prefix/bin/subdl @@ -0,0 +1,485 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i python3 -p python3 + +# subdl - command-line tool to download subtitles from opensubtitles.org. +# +# Uses code from subdownloader (a GUI app). + +__doc__ = '''\ +Syntax: subdl [options] moviefile.avi ... + +Subdl is a command-line tool for downloading subtitles from opensubtitles.org. + +By default, it will search for English subtitles, display the results, +download the highest-rated result in the requested language and save it to the +appropriate filename. + +Options: + --help This text + --version Print version and exit + --lang=LANGUAGES Comma-separated list of languages in 3-letter code, e.g. + 'eng,spa,fre', or 'all' for all. Default is 'eng'. + --list-languages List available languages and exit. + --download=ID Download a particular subtitle by numeric ID. + --download=first Download the first search result [default]. + --download=all Download all search results. + --download=best-rating Download the result with best rating. + --download=most-downloaded Download the most downloaded result. + --download=query Query which search result to download. + --download=none, -n Display search results and exit. + --output=OUTPUT Output to specified output filename. Can include the + following format specifiers: + %I subtitle id + %m movie file base %M movie file extension + %s subtitle file base %S subtitle file extension + %l language (English) %L language (2-letter ISO639) + Default is "%m.%S"; if multiple languages are searched, + then the default is "%m.%L.%S"; if --download=all, then + the default is "%m.%L.%I.%S". + --existing=abort Abort if output filename already exists [default]. + --existing=bypass Exit gracefully if output filename already exists. + --existing=overwrite Overwrite if output filename already exists. + --existing=query Query whether to overwrite. + --imdb-id=id Query by IMDB id. Hash is tried first unless --force-imdb + is used. IMDB URLs are also accepted. + --force-imdb Force IMDB search. --imdb-id must be specified. + --force-filename Force search using filename. + --filter Filter blacklisted texts from subtitle. + --interactive, -i Equivalent to --download=query --existing=query. + ''' + +NAME = 'subdl' +VERSION = '1.0.3' + +VERSION_INFO = '''\ + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +http://code.google.com/p/subdl/''' + +import os, sys +import struct +import xmlrpc.client +import io, gzip, base64 +import getopt +import re + +osdb_server = "https://api.opensubtitles.org/xml-rpc" +xmlrpc_server = xmlrpc.client.ServerProxy(osdb_server) +login = xmlrpc_server.LogIn("", "", "en", NAME+" "+VERSION) +osdb_token = login["token"] + +BLACKLIST = [ + 'opensubtitles', + 'addic7ed', + 'joycasino', + 'bitninja\.io', + 'Please rate this subtitle at www\.osdb\.link', + 'allsubs', + 'firebit\.org', + 'humanguardians\.com', + 'subtitles by', + 'recast\.ai', + 'by mstoll', + 'subs corrected', + 'by tronar', + 'titlovi', + '^_$', + '^- _$', +] + +class Options: pass +options = Options() +options.lang = 'eng' +options.download = 'first' +options.output = None +options.existing = 'abort' +options.imdb_id = None +options.force_imdb = False +options.force_filename = False +options.filter = False + +class SubtitleSearchResult: + def __init__(self, dict): + self.__dict__ = dict + +def file_ext(filename): + return os.path.splitext(filename)[1][1:] + +def file_base(filename): + return os.path.splitext(filename)[0] + +def gunzipstr(zs): + with gzip.open(io.BytesIO(zs)) as gz: + return gz.read() + +def writefile(filename, str): + try: + with open(filename, 'wb') as f: + f.write(str) + except Exception as e: + raise SystemExit("Error writing to %s: %s" % (filename, e)) + +def query_num(s, low, high): + while True: + try: + n = input("%s [%d..%d] " % (s, low, high)) + except KeyboardInterrupt: + raise SystemExit("Aborted") + try: + n = int(n) + if low <= n <= high: + return n + except: + pass + +def query_yn(s): + while True: + try: + r = input("%s [y/n] " % s).lower() + except KeyboardInterrupt: + raise SystemExit("Aborted") + if r.startswith('y'): + return True + elif r.startswith('n'): + return False + +def filtersub(s): + s = s.strip() + line_sep = b'\r\n' if re.search(b'\r\n', s) else b'\n' + subs = re.split(b'(?:\r?\n){2,}', s) + subs = [re.split(b'\r?\n', sub, 2) for sub in subs] + filter_pattern = re.compile('|'.join(BLACKLIST).encode(), re.M | re.I) + for i in range(len(subs) - 1, -1, -1): + if len(subs[i]) < 3: + del subs[i] + continue + text = subs[i][2] + if filter_pattern.search(text): + print("Removed", i + 1, ":", text) + del subs[i] + for i in range(len(subs)): + subs[i][0] = str(i + 1).encode() + subs = map(line_sep.join, subs) + return (line_sep * 2).join(subs) + +def movie_hash(name): + longlongformat = ' 0 + except: + return False + +def ListLanguages(): + languages = xmlrpc_server.GetSubLanguages('')['data'] + for language in languages: + print(language['SubLanguageID'], language['ISO639'], language['LanguageName']) + raise SystemExit + +def default_output_fmt(): + if options.download == 'all': + return "{m}.{L}.{I}.{S}" + elif options.lang == 'all' or ',' in options.lang: + return "{m}.{L}.{S}" + else: + return "{m}.{S}" + + +def parseargs(args): + try: + opts, arguments = getopt.getopt(args, 'h?in', [ + 'existing=', 'lang=', 'search-only=', + 'download=', 'output=', 'interactive', + 'list-languages', 'imdb-id=', 'force-imdb', + 'force-filename', 'filter', 'help', + 'version', 'versionx']) + except getopt.GetoptError as e: + raise SystemExit("%s: %s (see --help)" % (sys.argv[0], e)) + for option, value in opts: + if option == '--help' or option == '-h' or option == '-?': + help() + elif option == '--versionx': + print(VERSION) + raise SystemExit + elif option == '--version': + print("%s %s" % (NAME, VERSION)) + raise SystemExit + elif option == '--existing': + if value in ['abort', 'overwrite', 'bypass', 'query']: + pass + else: + raise SystemExit("Argument to --existing must be one of: abort, overwrite, bypass, query") + options.existing = value + elif option == '--lang': + options.lang = value + elif option == '--download': + if value in ['all', 'first', 'query', 'none', 'best-rating', 'most-downloaded'] or isnumber(value): + pass + else: + raise SystemExit("Argument to --download must be numeric subtitle id or one: all, first, query, none") + options.download = value + elif option == '-n': + options.download = 'none' + elif option == '--output': + options.output = value + elif option == '--imdb-id': + options.imdb_id = value + elif option == '--force-imdb': + options.force_imdb = True + elif option == '--force-filename': + options.force_filename = True + elif option == '--filter': + options.filter = True + elif option == '--interactive' or option == '-i': + options.download = 'query' + options.existing = 'query' + elif option == '--list-languages': + ListLanguages() + else: + raise SystemExit("internal error: bad option '%s'" % option) + if not options.output: + options.output = default_output_fmt() + if len(arguments) == 0: + raise SystemExit("syntax: %s [options] filename.avi (see --help)" % (sys.argv[0])) + if len(arguments) > 1 and options.force_imdb: + raise SystemExit("Can't use --force-imdb with multiple files.") + if len(arguments) > 1 and isnumber(options.download): + raise SystemExit("Can't use --download=ID with multiple files.") + + return arguments + + +def main(args): + files = parseargs(args) + no_search_results = 0 + for file in files: + selected_file = ''; + if not os.path.exists(file): + raise SystemExit("can't find file '%s'" % file) + if options.force_imdb: + if options.imdb_id is None: + raise SystemExit("With --force-imdb a --imdb-id must be provided.") + search_results = SearchSubtitlesByIMDBId(file, options.lang, options.imdb_id) + elif options.force_filename: + search_results = SearchSubtitlesByFileName(file, options.lang) + else: + search_results = SearchSubtitlesByHash(file, options.lang) + if not search_results and options.imdb_id is not None: + print("No results found by hash, trying IMDB id") + search_results = SearchSubtitlesByIMDBId(file, options.lang, options.imdb_id) + elif not search_results: + print("No results found by hash, trying filename") + search_results = SearchSubtitlesByFileName(file, options.lang) + if not search_results: + print("No results found.", file=sys.stderr) + no_search_results = no_search_results + 1 + continue + + DisplaySubtitleSearchResults(search_results) + if options.download == 'none': + raise SystemExit + elif options.download == 'first': + selected_file = search_results[0] + print() + print("Defaulting to first result (try --interactive):") + DisplaySelectedSubtitle(selected_file) + print() + AutoDownloadAndSave(file, search_results[0]) + elif options.download == 'all': + downloaded = {} + for search_result in search_results: + AutoDownloadAndSave(file, search_result, downloaded) + elif options.download == 'query': + n = query_num("Enter result to download:", + 1, len(search_results)) + AutoDownloadAndSave(file, search_results[n-1]) + elif options.download == 'best-rating': + selected_file = max(search_results, key=lambda sub: float(sub.SubRating)) + print() + print("Downloading subtitle with best rating:") + DisplaySelectedSubtitle(selected_file) + print() + AutoDownloadAndSave(file, selected_file) + elif options.download == 'most-downloaded': + selected_file = max(search_results, key=lambda sub: int(sub.SubDownloadsCnt)) + print() + print("Downloading most downloaded subtitle:") + DisplaySelectedSubtitle(selected_file) + print() + AutoDownloadAndSave(file, selected_file) + elif isnumber(options.download): + search_result = select_search_result_by_id(options.download, search_results) + AutoDownloadAndSave(file, search_result) + else: + raise Exception("internal error: bad option.download=%s" % options.download) + + if no_search_results > 0: + raise SystemExit("One or more subtitles were not found.") + +main(sys.argv[1:]) diff --git a/scripts/prefix/bin/task-add-birthdays.sh b/scripts/prefix/bin/task-add-birthdays.sh new file mode 100755 index 0000000..9b5bb0e --- /dev/null +++ b/scripts/prefix/bin/task-add-birthdays.sh @@ -0,0 +1,34 @@ +# Ariane 1984 03 07 +# Clémence 1984 03 05 +# Cécilia 1988 07 09 +# David 1988 11 08 +# Glenn 1988 06 14 +# Grégoire 1988 05 07 +# Idéfix 2011 09 19 +# Isa 1988 01 04 +# Laurent 1989 04 14 +# Léo 1990 10 18 +# Manu 1979 04 28 +# Marcel 1933 07 14 +# Martin 1988 03 01 +# Mman 1958 01 13 +# Nicole 1954 03 23 +# Olivia 1988 07 07 +# Ppa 1957 03 10 +# Samuel 1988 07 20 +# Sergiu 1988 09 17 +# Thibault 1987 12 18 +# Thomas 1988 09 14 + +if [[ $# -ne 4 ]] +then + echo "Usage: $0 name year month day" + exit +fi + +NAME=$1 +DATEY=$2 +DATEM=$3 +DATED=$4 + +echo task add rc.dateformat:M-D +celebrate due:$DATEM-$DATED recur:yearly wait:due-1w until:due+2d "Anniversaire $NAME ($DATEY-$DATEM-$DATED)" diff --git a/scripts/prefix/bin/yt b/scripts/prefix/bin/yt index ab9af98..6f28c78 100755 --- a/scripts/prefix/bin/yt +++ b/scripts/prefix/bin/yt @@ -1,6 +1,5 @@ -#!/bin/bash -#YoutubeBrowser="/usr/bin/google-chrome-stable" -YoutubeBrowser="/home/eeva/.nix-profile/bin/chromium-browser" +#!/bin/sh +YoutubeBrowser=$(which chromium-browser) BaseSearchUrl=" --incognito https://www.youtube.com/results?search_query=" BaseUrl="https://www.youtube.com/feed/subscriptions"