File size: 5,786 Bytes
df4a9b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import os
file_path = "c:/Users/Gonzalo/Downloads/Twisted Brody Waifus & Creampie/utils.py"
# New content for the function
new_function_code = '''def format_tracemoe_results(data):
"""Formatea resultados de trace.moe + anilist info EXTENDIDO."""
if not data or isinstance(data, str) or 'result' not in data:
return "🤷♂️ **Trace.moe: Sin resultados.**", None, False
results = data['result']
if not results:
return "🤷♂️ **Trace.moe: Sin coincidencias.**", None, False
# Filtrar y tomar el mejor
best = results[0]
similarity = float(best.get('similarity', 0)) * 100
if similarity < 85.0:
return "🤷♂️ **Trace.moe: Sin coincidencias sólidas (<85%).**", None, False
# Obtener info extra de Anilist
anilist_id = best.get('anilist', None)
meta = {}
if anilist_id:
from utils import get_anilist_info # Ensure import if needed, or rely on global
meta = get_anilist_info(anilist_id) or {}
# --- PROCESAR DATOS DE ANILIST ---
# 1. Títulos
titles = meta.get('title', {})
title_romaji = titles.get('romaji', 'Desconocido')
title_native = titles.get('native')
# 2. Info
episodes = meta.get('episodes', '?')
duration = meta.get('duration', '?')
fmt = meta.get('format', 'TV')
desc_type = f"{episodes} ep. ({duration} min) {fmt} anime."
start = meta.get('startDate', {})
end = meta.get('endDate', {})
s_date = f"{start.get('year', '?')}-{start.get('month', '?')}-{start.get('day', '?')}"
e_date = f"{end.get('year', '?')}-{end.get('month', '?')}-{end.get('day', '?')}"
aired = f"Airing from {s_date} to {e_date}."
# 3. Alias
aliases = meta.get('synonyms', [])
alias_str = aliases[0] if aliases else "None"
# 4. Géneros
genres = meta.get('genres', [])
genre_str = ", ".join(genres[:3]) if genres else "Unknown"
# 5. Studios
studios_node = meta.get('studios', {}).get('nodes', [])
studios_str = "Unknown"
if studios_node:
studios_str = ", ".join([s['name'] for s in studios_node])
# 6. Clip Match
match_episode = best.get('episode', '?')
time_pos = best.get('from', 0)
m, s = divmod(int(time_pos), 60)
time_str = f"{m}:{s:02d}"
# NSFW Check
is_adult = meta.get('isAdult', False)
video_preview = best.get('video', '')
# --- CONSTRUIR EL MENSAJE FINAL (EXACTO) ---
msg = f"""
📁 Trace moe resultados
📁 Similitud: {similarity:.2f}%
📁 Title: `{title_romaji}`
📁 `{title_native if title_native else ''}`
📁 Info: {desc_type}
{aired}
📁 Alias: `{alias_str}`
📁 Genre: {genre_str}
📁 Studio: {studios_str}
📁 Clip Match: Episode: {match_episode} (Time: {time_str})
"""
return msg, video_preview, is_adult
'''
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
output_lines = []
# State machine to skip the FIRST definition of format_tracemoe_results
# and REPLACE the SECOND definition.
found_first_def = False
skipping_first = False
found_second_def = False
skipping_second = False
replaced_second = False
i = 0
while i < len(lines):
line = lines[i]
# Detect function start
if line.strip().startswith("def format_tracemoe_results(data):"):
if not found_first_def:
# Found the first one (the duplicate/bad one). Start skipping.
found_first_def = True
skipping_first = True
# Check where this function ends (naive check: next def or special comment)
# In the file, it seems followed by # --- TRACE.MOE API SERVICE --- (approx)
# We will skip until we see a line starting with 'def ' or '# ---' that is NOT this function
i += 1
continue
else:
# Found the second one! This is where we want to put the NEW code.
found_second_def = True
skipping_second = True
if not replaced_second:
output_lines.append(new_function_code + "\n")
replaced_second = True
i += 1
continue
if skipping_first:
# We are inside the first unwanted function.
# Stop skipping if we hit a new function definition or section header
if line.strip().startswith("def ") or line.strip().startswith("# --- TRACE.MOE API SERVICE ---"):
skipping_first = False
output_lines.append(line) # Keep this line (it's the next thing)
# Else: Drop the line
i += 1
continue
if skipping_second:
# We are inside the second function (which we already replaced).
# Stop skipping if we hit end of file or next def (though it's likely EOF)
if line.strip().startswith("def "): # Safety break if there is another function
skipping_second = False
output_lines.append(line)
# Else: Drop the line
i += 1
continue
# Normal line, keep it
output_lines.append(line)
i += 1
# Write back
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(output_lines)
print("Successfully cleaned up utils.py")
except Exception as e:
print(f"Error: {e}")
|