-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulysu.py
More file actions
330 lines (265 loc) · 11.4 KB
/
Copy pathmulysu.py
File metadata and controls
330 lines (265 loc) · 11.4 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os
import sys
from dataclasses import dataclass, field, asdict
from typing import List, Optional, Dict, Any
from functools import cmp_to_key
DB_FILE = "db.json"
@dataclass
class Resource:
"""Represents a found resource"""
type: str # 'vid.subs', 'vid.lyrics', 'text.lyrics'
url: str
language: List[str] = field(default_factory=list)
votes: int = 0
content: Optional[str] = None
@dataclass
class Song:
"""Represents a song entry"""
artist: str
song: str
artist_original: str
song_original: str
resources: List[Resource] = field(default_factory=list)
class SongDatabase:
"""Main database manager class"""
def __init__(self):
self.songs: List[Song] = []
self.load()
def load(self) -> None:
"""Load database from file"""
if not os.path.exists(DB_FILE):
self.save()
return
try:
with open(DB_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
self.songs = []
for song_data in data:
resources = [
Resource(**res) for res in song_data.get('resources', [])
]
song = Song(
artist=song_data['artist'],
song=song_data['song'],
artist_original=song_data['artist_original'],
song_original=song_data['song_original'],
resources=resources
)
self.songs.append(song)
except (json.JSONDecodeError, KeyError) as e:
print(f"Error loading database: {e}")
self.songs = []
def save(self) -> None:
"""Save database to file"""
data = []
for song in self.songs:
song_dict = asdict(song)
# Convert resources
song_dict['resources'] = [
asdict(resource) for resource in song.resources
]
data.append(song_dict)
with open(DB_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def _sort_resources(self, resources: List[Resource]) -> List[Resource]:
"""Sort resources by votes (descending), then by type"""
def compare(r1: Resource, r2: Resource) -> int:
# First by votes (descending)
if r1.votes != r2.votes:
return r2.votes - r1.votes
# Then by type alphabetically
return (r1.type > r2.type) - (r1.type < r2.type)
return sorted(resources, key=cmp_to_key(compare))
def search(self, query: str) -> List[Song]:
"""Search for songs matching query"""
query = query.lower()
results = []
for song in self.songs:
if (query in song.artist.lower() or
query in song.song.lower() or
query in song.artist_original.lower() or
query in song.song_original.lower()):
results.append(song)
return results
def add_song(self) -> None:
"""Add a new song interactively"""
print("\n=== Add New Song ===")
artist = input("Artist name: ").strip()
if not artist:
print("Artist name is required.")
return
artist_original = input(f"Original artist name [{artist}]: ").strip() or artist
song_name = input("Song name: ").strip()
if not song_name:
print("Song name is required.")
return
song_original = input(f"Original song name [{song_name}]: ").strip() or song_name
resources = []
while True:
if input("\nAdd a resource? (y/n): ").lower() != 'y':
break
print("\n--- New Resource ---")
res_type = input("Type (e.g., vid.subs, vid.lyrics): ").strip()
url = input("URL: ").strip()
language = input("Language code (en/es/ja/etc) [en]: ").strip() or "en"
content = input("Content/notes (optional, press Enter to skip): ").strip()
if not content:
content = None
resources.append(Resource(
type=res_type,
url=url,
language=language,
content=content,
votes=0
))
print("Resource added.")
new_song = Song(
artist=artist,
song=song_name,
artist_original=artist_original,
song_original=song_original,
resources=resources
)
self.songs.append(new_song)
self.save()
print(f"\n✓ Song '{song_name}' by '{artist}' has been added.")
def display_song(self, song: Song, index: Optional[int] = None) -> None:
"""Display a single song with its resources"""
prefix = f"{index}. " if index is not None else ""
print(f"\n{prefix}{song.artist} - {song.song}")
if song.song_original != song.song:
print(f" Original: {song.song_original}")
if song.resources:
sorted_resources = self._sort_resources(song.resources)
print(" Resources (sorted by votes):")
for i, resource in enumerate(sorted_resources, 1):
vote_str = f"▲ {resource.votes}" if resource.votes > 0 else ""
print(f" {i}. [{resource.type}] {resource.language} {vote_str}")
print(f" URL: {resource.url}")
if resource.content:
content = resource.content
if len(content) > 60:
content = content[:57] + "..."
print(f" Content: {content}")
else:
print(" No resources available")
def display_search_results(self, results: List[Song]) -> None:
"""Display search results"""
if not results:
print("No matches found.")
return
print(f"\nFound {len(results)} result(s):")
for i, song in enumerate(results, 1):
self.display_song(song, i)
def vote_on_resource(self) -> None:
"""Vote on a specific resource"""
if not self.songs:
print("Database is empty.")
return
query = input("Search for song to vote on: ").strip().lower()
if not query:
return
results = self.search(query)
if not results:
print("No matches found.")
return
print(f"\nFound {len(results)} result(s):")
for i, song in enumerate(results, 1):
print(f"{i}. {song.artist} - {song.song}")
try:
song_choice = int(input("\nSelect song number: ")) - 1
if not (0 <= song_choice < len(results)):
print("Invalid selection.")
return
selected_song = results[song_choice]
# Find the actual song in database (not the search result copy)
for i, song in enumerate(self.songs):
if (song.artist == selected_song.artist and
song.song == selected_song.song):
if not song.resources:
print("This song has no resources.")
return
# Display sorted resources
sorted_resources = self._sort_resources(song.resources)
print(f"\nResources for '{song.song}':")
for j, resource in enumerate(sorted_resources, 1):
vote_str = f"▲ {resource.votes}" if resource.votes > 0 else ""
print(f" {j}. {resource.type} ({resource.language}) {vote_str}")
# Get resource choice
resource_choice = int(input("Select resource to upvote: ")) - 1
if not (0 <= resource_choice < len(sorted_resources)):
print("Invalid selection.")
return
# Find the actual resource in the original list
selected_resource = sorted_resources[resource_choice]
for k, resource in enumerate(song.resources):
if (resource.type == selected_resource.type and
resource.url == selected_resource.url):
song.resources[k].votes += 1
self.save()
print(f"\n✓ Vote added! Now has {song.resources[k].votes} votes.")
return
except ValueError:
print("Please enter a valid number.")
def list_all(self) -> None:
"""List all songs in the database"""
if not self.songs:
print("Database is empty.")
return
print(f"\n=== All Songs ({len(self.songs)} total) ===")
for i, song in enumerate(self.songs, 1):
print(f"\n{i}. {song.artist} - {song.song}")
if song.song_original != song.song:
print(f" Original: {song.song_original}")
if song.resources:
sorted_resources = self._sort_resources(song.resources)
total_votes = sum(r.votes for r in song.resources)
print(f" Resources: {len(song.resources)} (Total votes: {total_votes})")
# Show top 2 resources
for j, resource in enumerate(sorted_resources[:2], 1):
vote_str = f"▲ {resource.votes}" if resource.votes > 0 else ""
print(f" {j}. {resource.type} ({resource.language}) {vote_str}")
def main():
"""Main program loop"""
print("🎵 Song Database Manager")
print("Using dataclasses with full UTF-8 support")
db = SongDatabase()
while True:
try:
print("\nOptions:")
print(" 1. Search songs")
print(" 2. Add new song")
print(" 3. List all songs")
print(" 4. Vote on resource")
print(" 5. Exit")
choice = input("\nYour choice: ").strip()
if choice == "1":
query = input("Search for song or artist: ").strip()
results = db.search(query)
db.display_search_results(results)
elif choice == "2":
db.add_song()
elif choice == "3":
db.list_all()
elif choice == "4":
db.vote_on_resource()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid option. Please choose 1-5.")
except KeyboardInterrupt:
print("\n\nInterrupted. Use option 5 to exit properly.")
continue
except EOFError:
print("\n\nExiting...")
break
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nProgram terminated by user.")
sys.exit(0)