-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathapply_grazer_patch.py
More file actions
124 lines (95 loc) · 4.25 KB
/
apply_grazer_patch.py
File metadata and controls
124 lines (95 loc) · 4.25 KB
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
#!/usr/bin/env python3
"""Apply Grazer integration to autonomous agent daemon.
Task: #1588 - Add type hints to Python functions
"""
from __future__ import annotations
from typing import Tuple, Optional
DAEMON_FILE: str = "/root/bottube/bottube_autonomous_agent.py"
def read_daemon_file(file_path: str) -> str:
"""Read the daemon file content."""
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
def write_daemon_file(file_path: str, content: str) -> None:
"""Write content to the daemon file."""
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
def add_grazer_import(content: str) -> Tuple[bool, str]:
"""Add Grazer import to the content.
Returns:
Tuple of (success, new_content)
"""
if "from grazer_integration import grazer" in content:
return False, content
import_section_end: int = content.find("# ---------------------------------------------------------------------------")
if import_section_end > 0:
import_line: str = "\nfrom grazer_integration import grazer\n"
new_content: str = content[:import_section_end] + import_line + content[import_section_end:]
return True, new_content
return False, content
def replace_browse_section(content: str) -> Tuple[bool, str]:
"""Replace random video selection with Grazer filtering.
Returns:
Tuple of (success, new_content)
"""
old_browse: str = """ r = api_get("/api/videos", params={"per_page": 30})
if not r or r.status_code != 200:
return False
videos = r.json().get("videos", [])
# Filter out own videos and already-commented
candidates = [
v for v in videos
if v["agent_name"] != bot_name
and not brain.already_commented_on(v["video_id"])
]
if not candidates:
return False
video = random.choice(candidates)"""
new_browse: str = """ # Use Grazer for intelligent content discovery
videos = grazer.discover_bottube(limit=30)
if not videos:
# Fallback to direct API if Grazer fails
r = api_get("/api/videos", params={"per_page": 30})
if not r or r.status_code != 200:
return False
videos = r.json().get("videos", [])
# Filter out own videos and already-commented
candidates = [
v for v in videos
if v.get("agent_name") != bot_name
and not brain.already_commented_on(v.get("video_id"))
]
if not candidates:
return False
# Grazer returns videos ranked by quality - take top one instead of random
video = candidates[0] # Best quality video
# Mark as seen in Grazer to avoid re-engagement
grazer.filter.mark_seen(video.get("video_id"))"""
if old_browse in content:
return True, content.replace(old_browse, new_browse)
return False, content
def apply_grazer_patch(daemon_file: str = DAEMON_FILE) -> None:
"""Apply Grazer integration patch to the daemon file."""
content: str = read_daemon_file(daemon_file)
# Patch 1: Add Grazer import
import_added: bool
import_added, content = add_grazer_import(content)
if import_added:
print("✓ Added Grazer import")
# Patch 2: Replace random video selection with Grazer filtering
browse_replaced: bool
browse_replaced, content = replace_browse_section(content)
if browse_replaced:
print("✓ Replaced random selection with Grazer intelligent filtering")
else:
print("⚠ Browse section not found - may need manual integration")
# Write patched content
write_daemon_file(daemon_file, content)
print("\n✓ Grazer integration applied successfully!")
print(" - Bots will now use intelligent quality-based content selection")
print(" - Videos are ranked by engagement, novelty, and relevance")
print(" - Duplicate engagement is automatically prevented")
def main() -> None:
"""Main entry point."""
apply_grazer_patch()
if __name__ == "__main__":
main()