1
0
Fork 0

Restructure directory layout

To make this better than the unorganized mess it used to be.
This commit is contained in:
Luca Beltrame 2021-01-03 15:26:29 +01:00
parent 58a36ed632
commit c4f7279f2e
Signed by: einar
GPG key ID: 4707F46E9EC72DEC
25 changed files with 0 additions and 727 deletions

119
misc/nico_comment.py Executable file
View file

@ -0,0 +1,119 @@
#!/usr/bin/python3
# SPDX-FileCopyrightText: 2021 Luca Beltrame <lbeltrame@kde.org>
#
# SPDX-License-Identifier: BSD-3-Clause
import asyncio
import argparse
from datetime import datetime
import netrc
import os
from urllib.parse import urlparse
import pandas as pd
import pysrt
from nicotools.download import Comment, Info, utils
import simplejson as json
def process_element(element):
element = element["chat"]
video_pos = element["vpos"] * 10
comment = element["content"]
video_time = datetime.fromtimestamp(video_pos / 1000).strftime("%M:%S.%f")
return (video_time, comment)
def extract_ids(urls):
video_ids = list()
for url in urls:
parsed = urlparse(url)
nico_id = os.path.split(parsed.path)[-1]
video_ids.append(nico_id)
return nico_id
def build_srt(data):
srt_file = pysrt.SubRipFile()
for index, proc in enumerate(data):
time, text = proc
time = datetime.strptime(time, "%M:%S.%f")
subtime_st = pysrt.SubRipTime(minutes=time.minute, seconds=time.second,
milliseconds=time.microsecond / 1000)
subtime_end = pysrt.SubRipTime(minutes=time.minute,
seconds=time.second + 2,
milliseconds=time.microsecond / 1000)
entry = pysrt.SubRipItem(index, subtime_st, subtime_end, text=text)
srt_file.append(entry)
return srt_file
class CommentStream(Comment):
srt = False
def saver(self, video_id: str, is_xml: bool,
coroutine: asyncio.Task) -> bool:
if is_xml:
super().saver(video_id, is_xml, coroutine)
return True
comment_data = coroutine.result()
conts = list()
data = json.loads(comment_data)
contents = [process_element(item)
for item in data if "chat" in item]
file_path = utils.make_name(self.glossary[video_id], "",
extention="txt")
file_srt = utils.make_name(self.glossary[video_id], "",
extention="srt")
df = pd.DataFrame.from_records(contents,
columns=["Time", "Comment"])
df["Time"] = pd.to_datetime(df["Time"],
format="%M:%S.%f").dt.time
df = df.sort_values(by="Time")
with file_path.open("w", encoding="utf-8") as f:
df.to_csv(f, sep="\t", index=False)
if self.srt:
srt_data = build_srt(contents)
srt_data.save(str(file_srt))
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument("video", help="Video URL(s)", nargs="+")
parser.add_argument("-d", "--destination", help="Destination directory",
default="./")
parser.add_argument("--no-srt", action="store_false",
help="Don't generate SRT")
options = parser.parse_args()
user, _, password = netrc.netrc().hosts["niconico"]
video_ids = extract_ids(options.video)
database = Info(video_ids, mail=user, password=password).info
com = CommentStream(database, user, password, save_dir=options.destination)
com.srt = options.no_srt
com.start()
if __name__ == "__main__":
main()

View file

@ -0,0 +1,107 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# SPDX-FileCopyrightText: 2014-2021 Luca Beltrame <lbeltrame@kde.org>
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
from enum import Enum
import os
from pathlib import Path
import re
import stat
import transmissionrpc
from xdg import Mime
class FileType(Enum):
music = 1
video = 2
application = 3
image = 4
unknown = 100
@staticmethod
def detect(filelist: dict):
# Get the largest file
largest = max(filelist, key=lambda x: filelist[x])
category = Mime.get_type_by_name(largest).media
if category == "audio":
return FileType.music
elif category == "video":
return FileType.video
elif category == "application":
return FileType.application
elif category == "image":
return FileType.image
else:
return FileType.unknown
def destination_dir(self) -> Path:
if self is FileType.music:
return Path("/home/storage/music/Anime/")
elif self is FileType.video:
return Path("/home/storage/video/")
elif self is FileType.application:
return Path("/home/storage/applications/")
elif self is FileType.image:
return Path("/home/storage/images/")
else:
return Path("/home/storage/transmission-downloads/")
def main():
# Get the information from Transmission
source_directory = os.environ.get("TR_TORRENT_DIR")
source_file = os.environ.get("TR_TORRENT_NAME")
torrent_id = os.environ.get("TR_TORRENT_ID")
with open("/var/lib/transmission/rpcpass", "r") as handle:
rpc_pass = handle.read().strip()
if source_directory is None or source_file is None:
print("Please run this script from Transmission.")
exit(1)
source = Path(source_directory) / source_file
assert source.exists()
client = transmissionrpc.Client("localhost", port=9091,
user="transmission", password=rpc_pass)
# Returns a dictionary with the torrent_id as key
filelist = client.get_files(torrent_id)[int(torrent_id)]
filelist = {filelist[item]["name"]: filelist[item]["size"] for item in
filelist}
filetype = FileType.detect(filelist)
destination = filetype.destination_dir()
if destination == source_directory:
return
dest_path = destination / source.name
source.rename(dest_path)
permissions = dest_path.stat().st_mode
permissions = permissions | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH
dest_path.chmod(permissions)
client.stop_torrent(torrent_id)
client.remove_torrent(torrent_id)
print("Moved {} to {}".format(source, dest_path))
if __name__ == "__main__":
main()

30
misc/update_wg_dns Executable file
View file

@ -0,0 +1,30 @@
#!/bin/bash
NETWORKMANAGER_NETCONFIG="/var/run/netconfig/NetworkManager.netconfig"
dns-update() {
tmpfile=$(mktemp)
sed --regexp-extended "s/(DNSSERVERS=)'(.*?)'/\1'$@ \2'/g" "${NETWORKMANAGER_NETCONFIG}" > "${tmpfile}"
netconfig modify -s NetworkManager -I "${tmpfile}"
rm -f "${tmpfile}"
}
dns-restore() {
tmpfile=$(mktemp)
sed --regexp-extended "s/$@ //g" "${NETWORKMANAGER_NETCONFIG}" > "${tmpfile}"
netconfig modify -s NetworkManager -I ${tmpfile}
rm -f ${tmpfile}
}
dns() {
local cmdname=$1; shift
"dns-$cmdname" "$@"
}
if declare -f "$1" >/dev/null 2>&1; then
# invoke that function, passing arguments through
"$@" # same as "$1" "$2" "$3" ... for full argument list
else
echo "Function $1 not recognized" >&2
exit 1
fi