k4d3 commited on
Commit
6f57a6d
·
1 Parent(s): 4695d3c

add silly code

Browse files

Signed-off-by: Balazs Horvath <acsipont@gmail.com>

Files changed (3) hide show
  1. fixcringe.py +56 -0
  2. shortcode.py +40 -0
  3. yiffdata.py +46 -0
fixcringe.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+
4
+ def fix_blurhash_block(content):
5
+ # Pattern to match the blurhash block
6
+ pattern = r'{{<\s*blurhash\s*\n((?:[^>}]|\n)*?)>}}'
7
+
8
+ def replace_block(match):
9
+ lines = match.group(1).strip().split('\n')
10
+ # Process each line to add proper indentation and clean up
11
+ processed_lines = []
12
+ for line in lines:
13
+ # Strip existing whitespace
14
+ clean_line = line.strip()
15
+ # Add 2 spaces indentation
16
+ processed_lines.append(f" {clean_line}")
17
+
18
+ # If this is the alt line, add grid="true" after it
19
+ if 'alt=' in clean_line:
20
+ grid_line = ' grid="true"'
21
+ processed_lines.append(grid_line)
22
+
23
+ # Reconstruct the block
24
+ return '{{< blurhash\n' + '\n'.join(processed_lines) + '\n>}}'
25
+
26
+ # Replace all occurrences in the content
27
+ return re.sub(pattern, replace_block, content, flags=re.MULTILINE)
28
+
29
+ def process_file(file_path):
30
+ try:
31
+ # Read the file content
32
+ with open(file_path, 'r', encoding='utf-8') as f:
33
+ content = f.read()
34
+
35
+ # Fix the content
36
+ modified_content = fix_blurhash_block(content)
37
+
38
+ # Write back only if changes were made
39
+ if content != modified_content:
40
+ with open(file_path, 'w', encoding='utf-8') as f:
41
+ f.write(modified_content)
42
+ print(f"Processed: {file_path}")
43
+
44
+ except Exception as e:
45
+ print(f"Error processing {file_path}: {str(e)}")
46
+
47
+ def main():
48
+ # Walk through all directories recursively
49
+ for root, dirs, files in os.walk('.'):
50
+ for file in files:
51
+ if file.endswith('.cringe'):
52
+ file_path = os.path.join(root, file)
53
+ process_file(file_path)
54
+
55
+ if __name__ == '__main__':
56
+ main()
shortcode.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ def process_json_file(json_path, base_dir):
5
+ with open(json_path, 'r') as file:
6
+ data = json.load(file)
7
+ filename = data['filename']
8
+ width = data['width']
9
+ height = data['height']
10
+ blurhash = data['blurhash']
11
+ caption = data['caption']
12
+
13
+ image_filename, image_fileextension = os.path.splitext(filename)
14
+ image_fileextension = image_fileextension.lstrip('.')
15
+
16
+ directory = os.path.relpath(os.path.dirname(json_path), base_dir)
17
+ directory = directory.replace('static/', '', 1) # Remove the first occurrence of 'static/'
18
+
19
+ cringe_filename = f"{image_filename}.{image_fileextension}.cringe"
20
+ cringe_content = f"""{{{{< blurhash
21
+ src="https://huggingface.co/k4d3/yiff_toolkit/resolve/main/static/{directory}/{filename}"
22
+ blurhash="{blurhash}"
23
+ width="{width}"
24
+ height="{height}"
25
+ alt="{caption}"
26
+ >}}}}"""
27
+
28
+ with open(os.path.join(os.path.dirname(json_path), cringe_filename), 'w') as cringe_file:
29
+ cringe_file.write(cringe_content)
30
+
31
+ def recurse_directories(base_dir):
32
+ for root, _, files in os.walk(base_dir):
33
+ for file in files:
34
+ if file.endswith('.json'):
35
+ process_json_file(os.path.join(root, file), base_dir)
36
+
37
+ if __name__ == "__main__":
38
+ base_directory = os.path.join(os.getcwd(), 'yiff_toolkit')
39
+ recurse_directories(base_directory)
40
+
yiffdata.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from PIL import Image
4
+
5
+ def get_image_info(image_path):
6
+ with Image.open(image_path) as img:
7
+ width, height = img.size
8
+ return width, height
9
+
10
+ def process_directory(directory):
11
+ for root, _, files in os.walk(directory):
12
+ for file in files:
13
+ if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
14
+ image_path = os.path.join(root, file)
15
+ width, height = get_image_info(image_path)
16
+
17
+ base_name, ext = os.path.splitext(file)
18
+ blurhash_file = os.path.join(root, f"{base_name}{ext}.bh")
19
+ caption_file = os.path.join(root, f"{base_name}.caption")
20
+
21
+ blurhash = None
22
+ caption = None
23
+
24
+ if os.path.exists(blurhash_file):
25
+ with open(blurhash_file, 'r') as bh_file:
26
+ blurhash = bh_file.read().strip()
27
+
28
+ if os.path.exists(caption_file):
29
+ with open(caption_file, 'r') as cap_file:
30
+ caption = cap_file.read().strip()
31
+
32
+ json_data = {
33
+ "filename": file,
34
+ "width": width,
35
+ "height": height,
36
+ "blurhash": blurhash,
37
+ "caption": caption
38
+ }
39
+
40
+ json_file = os.path.join(root, f"{base_name}{ext}.json")
41
+ with open(json_file, 'w') as jf:
42
+ json.dump(json_data, jf, indent=4)
43
+
44
+ if __name__ == "__main__":
45
+ process_directory(os.getcwd())
46
+