Skip to content

Instantly share code, notes, and snippets.

View chapmanjacobd's full-sized avatar
🥅
goal_net

Jacob Chapman chapmanjacobd

🥅
goal_net
View GitHub Profile
View cover_scan.py
def cover_scan(media_duration, scan_percentage):
num_scans = max(2, int(math.log(media_duration) * (scan_percentage / 10)))
scan_duration_total = max(1, media_duration * (scan_percentage / 100))
scan_duration = max(1, int(scan_duration_total / num_scans))
scan_interval = media_duration / num_scans
scans = sorted(set(int(scan * scan_interval) for scan in range(num_scans)))
if scans[-1] < media_duration - (scan_duration * 2):
scans.append(math.floor(media_duration - scan_duration))
View get_file_part.py
def get_file_part(file_path, specifier):
size = file_path.stat().st_size
if specifier.isdigit():
specifier = int(specifier)
if specifier > 0:
with file_path.open('rb') as file:
return file.read(specifier)
else:
View merge.py
def compare_stats(destination_files, destination_folders, source_files, source_empty_folders):
new_empty_folders = source_empty_folders - destination_folders
source_dirs = {p.parent for p in source_files}
destination_dirs = {p.parent for p in destination_files}
new_directories = source_dirs - destination_dirs
new_files = source_files - destination_files
replaced_files = source_files & destination_files
trumped_files = replaced_files.intersection(source_files | destination_files)
View ffprobe_class.py
class FFProbe:
def __init__(self, path, *args):
args = ["ffprobe", "-show_format", "-show_streams", "-show_chapters", "-of", "json", *args, path]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
raise RuntimeError(out, err)
d = json.loads(out.decode("utf-8"))
View btrfs_raid1_metadata_ENOSPC.txt
kern :info : [ +0.000006] BTRFS info (device sde: state A): dumping space info:
kern :info : [ +0.000006] BTRFS info (device sde: state A): space_info DATA has 142864384 free, is not full
kern :info : [ +0.000006] BTRFS info (device sde: state A): space_info total=9971804340224, used=9688658702336, pinned=283002445824, reserved=0, may_use=0, readonly=327680 zone_unusable=0
kern :info : [ +0.000009] BTRFS info (device sde: state A): space_info METADATA has 0 free, is full
kern :info : [ +0.000005] BTRFS info (device sde: state A): space_info total=15032385536, used=14061862912, pinned=329777152, reserved=640679936, may_use=0, readonly
@chapmanjacobd
chapmanjacobd / decompress_stream.py
Created November 29, 2023 19:33
gunzip requests stream
View decompress_stream.py
import zlib
def decompress_stream(stream):
o = zlib.decompressobj(16 + zlib.MAX_WBITS)
for chunk in stream:
yield o.decompress(chunk)
yield o.flush()
@chapmanjacobd
chapmanjacobd / nosql_to_sqlv1.py
Created November 21, 2023 05:20
simple normalized JSON
View nosql_to_sqlv1.py
def nosql_to_sqlv1(dict_or_arr):
import pandas as pd
dict_or_arr = objects.flatten_dict_single_parents(dict_or_arr)
dict_or_arr = objects.flatten_grandparents(dict_or_arr)
df = pd.json_normalize(dict_or_arr, sep="_")
df = pd_utils.columns_snake_case(df)
dict_or_arr = df.to_dict(orient="records")
@chapmanjacobd
chapmanjacobd / bchunk_multi_bin.fish
Created November 4, 2023 01:46
multiple track BIN / single CUE on linux; afterwards get metadata from MusicBrainz Picard
View bchunk_multi_bin.fish
for i in (seq -w 1 50)
bchunk -w Dolphin\'s\ Mind\ -\ Fluid\ \(Germany\)\ \(The\ Full\ Dream\ Dance\ Megamix\)\ \(Track\ $i\).bin (
echo "FILE \"Dolphin\'s Mind - Fluid (Germany) (The Full Dream Dance Megamix) (Track $i).bin\" BINARY
TRACK $i AUDIO
"| psub
) DolpinMindMegamix
end
@chapmanjacobd
chapmanjacobd / shadows_be_gone.py
Created October 29, 2023 05:32
Shadow DOM WebElement generator
View shadows_be_gone.py
def flatten_shadows(driver):
# Shadow DOM can go to hell !!
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def get_all_elements(driver, elements):
for el in elements:
shadow_root = driver.execute_script("return arguments[0].shadowRoot", el)
if shadow_root:
@chapmanjacobd
chapmanjacobd / gitls.md
Last active December 1, 2023 05:09
a few interesting git commands
View gitls.md

gitls

git ls-files --sparse --full-name -z | 
  xargs -0 -I FILE -P 20 git log --date=iso-strict-local --format='%ad %>(14) %cr %<(5) %an  %h ./FILE' -- FILE | 
  sort --general-numeric-sort

This lists the files and hashes for each git commit and file:

2021-12-05T13:32:32-06:00  1 year, 11 months ago  Jacob Chapman  cc91fa0 ./.gitattributes

2021-12-05T13:32:32-06:00 1 year, 11 months ago Jacob Chapman cc91fa0 ./.gitignore