-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtomahawk-to-axe.js
More file actions
250 lines (206 loc) · 6.91 KB
/
tomahawk-to-axe.js
File metadata and controls
250 lines (206 loc) · 6.91 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
#!/usr/bin/env node
// Tomahawk Resolver to Harmonix .axe Converter
// Usage: node tomahawk-to-axe.js spotify-resolver.js
const fs = require('fs');
const path = require('path');
if (process.argv.length < 3) {
console.error('Usage: node tomahawk-to-axe.js <tomahawk-resolver.js>');
process.exit(1);
}
const inputFile = process.argv[2];
const outputFile = inputFile.replace(/\.js$/, '.axe');
console.log('🔄 Converting Tomahawk resolver to .axe format...');
console.log('Input:', inputFile);
console.log('Output:', outputFile);
// Read Tomahawk resolver
const tomahawkCode = fs.readFileSync(inputFile, 'utf8');
// Extract resolver metadata
const nameMatch = tomahawkCode.match(/name:\s*['"]([^'"]+)['"]/);
const iconMatch = tomahawkCode.match(/icon:\s*['"]([^'"]+)['"]/);
const weightMatch = tomahawkCode.match(/weight:\s*(\d+)/);
// Extract functions (basic pattern matching - may need refinement)
const resolveFnMatch = tomahawkCode.match(/resolve:\s*function\s*\([^)]*\)\s*{([\s\S]*?)(?=,\s*\w+:|}\s*\);)/);
const searchFnMatch = tomahawkCode.match(/search:\s*function\s*\([^)]*\)\s*{([\s\S]*?)(?=,\s*\w+:|}\s*\);)/);
if (!nameMatch) {
console.error('❌ Could not extract resolver name');
process.exit(1);
}
const resolverName = nameMatch[1];
const resolverId = resolverName.toLowerCase().replace(/\s+/g, '-');
console.log('📦 Resolver:', resolverName);
console.log('🔑 ID:', resolverId);
// Create .axe structure
const axe = {
manifest: {
id: resolverId,
name: resolverName,
version: '1.0.0',
author: 'Converted from Tomahawk',
description: `${resolverName} resolver (converted from Tomahawk)`,
icon: iconMatch ? iconMatch[1] : '🎵',
color: '#6366F1'
},
capabilities: {
resolve: !!resolveFnMatch,
search: !!searchFnMatch,
stream: true,
browse: false,
urlLookup: false
},
settings: {
requiresAuth: false,
authType: 'none',
configurable: {}
},
implementation: {}
};
// Convert functions
console.log('\n⚠️ MANUAL CONVERSION REQUIRED:');
console.log('The following functions need manual conversion from Tomahawk API to standard JS:');
console.log('');
if (resolveFnMatch) {
console.log('📝 Resolve function found - needs conversion:');
console.log(' - Replace Tomahawk.asyncRequest() with fetch()');
console.log(' - Replace Tomahawk.addTrackResults() with return statement');
console.log(' - Convert callbacks to async/await');
console.log(' - Update parameter names: (qid, artist, album, title) → (artist, track, album, config)');
console.log('');
// Placeholder - manual conversion needed
axe.implementation.resolve = "async function(artist, track, album, config) { /* TODO: Convert from Tomahawk format - see original code */ throw new Error('Manual conversion required'); }";
}
if (searchFnMatch) {
console.log('🔍 Search function found - needs conversion:');
console.log(' - Replace Tomahawk.asyncRequest() with fetch()');
console.log(' - Replace Tomahawk.addTrackResults() with return statement');
console.log(' - Convert callbacks to async/await');
console.log(' - Update parameter names: (qid, searchString) → (query, config)');
console.log('');
// Placeholder - manual conversion needed
axe.implementation.search = "async function(query, config) { /* TODO: Convert from Tomahawk format - see original code */ throw new Error('Manual conversion required'); }";
}
console.log('💡 Conversion steps:');
console.log(' 1. Open both files side-by-side');
console.log(' 2. Copy the logic from Tomahawk functions');
console.log(' 3. Replace Tomahawk APIs with standard JavaScript:');
console.log(' - Tomahawk.asyncRequest() → fetch()');
console.log(' - Tomahawk.addTrackResults() → return array');
console.log(' - Callbacks → async/await');
console.log(' 4. Test the resolver in Harmonix');
console.log('');
// Write .axe file
fs.writeFileSync(outputFile, JSON.stringify(axe, null, 2));
console.log(`✅ Created ${outputFile}`);
console.log('⚠️ This is a TEMPLATE - manual conversion of functions is required!');
console.log('');
console.log('Original Tomahawk code has been preserved in:', inputFile);
// Write a conversion guide
const guideFile = outputFile.replace('.axe', '-conversion-guide.md');
const guide = `# Conversion Guide: ${resolverName}
## Original Tomahawk Resolver
\`${inputFile}\`
## Target .axe Format
\`${outputFile}\`
## Functions to Convert
${resolveFnMatch ? `### Resolve Function
**Original:**
\`\`\`javascript
${resolveFnMatch[0]}
\`\`\`
**Convert to:**
\`\`\`javascript
async function(artist, track, album, config) {
// 1. Replace Tomahawk.asyncRequest with fetch
const response = await fetch(url);
const data = await response.json();
// 2. Process results
const results = data.items.map(item => ({
id: 'prefix-' + item.id,
title: item.name,
artist: item.artist,
album: item.album,
duration: item.duration,
sources: ['${resolverId}']
}));
// 3. Return results (not Tomahawk.addTrackResults)
return results[0] || null;
}
\`\`\`
` : ''}
${searchFnMatch ? `### Search Function
**Original:**
\`\`\`javascript
${searchFnMatch[0]}
\`\`\`
**Convert to:**
\`\`\`javascript
async function(query, config) {
// 1. Replace Tomahawk.asyncRequest with fetch
const response = await fetch(url);
const data = await response.json();
// 2. Process results
const results = data.items.map(item => ({
id: 'prefix-' + item.id,
title: item.name,
artist: item.artist,
album: item.album,
duration: item.duration,
sources: ['${resolverId}']
}));
// 3. Return results array
return results;
}
\`\`\`
` : ''}
## Common Conversions
### API Calls
\`\`\`javascript
// Tomahawk → Harmonix
Tomahawk.asyncRequest(url, callback) → await fetch(url)
Tomahawk.addTrackResults(results) → return results
Tomahawk.log(msg) → console.log(msg)
\`\`\`
### Callbacks → Async/Await
\`\`\`javascript
// Before
Tomahawk.asyncRequest(url, function(response) {
var data = JSON.parse(response);
processData(data);
});
// After
const response = await fetch(url);
const data = await response.json();
return processData(data);
\`\`\`
### Result Format
\`\`\`javascript
// Tomahawk format
{
artist: "Artist Name",
track: "Track Name",
source: "Spotify",
url: "spotify:track:123"
}
// Harmonix format
{
id: "spotify-123",
title: "Track Name",
artist: "Artist Name",
album: "Album Name",
duration: 180,
sources: ["spotify"],
spotifyUri: "spotify:track:123"
}
\`\`\`
## Testing
1. Complete the conversion
2. Validate JSON: \`cat ${outputFile} | jq\`
3. Install in Harmonix: Settings → Install New Resolver
4. Test search and playback
## Notes
- The original Tomahawk resolver is in \`${inputFile}\`
- This .axe file needs manual completion before it will work
- Focus on converting the logic, not just copy/paste
- Test thoroughly after conversion
`;
fs.writeFileSync(guideFile, guide);
console.log(`📖 Conversion guide written to: ${guideFile}`);