writer.py
13.9 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import uuid
def insert_yinyan_song_records(cur, records: list[dict]) -> None:
"""Initialize yinyan_song_records rows before crawler import."""
if not records:
return
cur.executemany(
"""
INSERT INTO yinyan_song_records (song_id, record_id, platform, is_yinyan_push)
VALUES (%s, %s, %s, FALSE)
ON CONFLICT (song_id, record_id) DO UPDATE
SET platform = EXCLUDED.platform
WHERE yinyan_song_records.platform IS NULL
""",
[(r['song_id'], r['record_id'], r['platform']) for r in records],
)
def fetch_existing_yinyan_song_ids(cur) -> set[int]:
"""返回已完成初始化 platform 的 song_id 集合。"""
cur.execute("SELECT DISTINCT song_id FROM yinyan_song_records WHERE platform IS NOT NULL")
return {row[0] for row in cur.fetchall()}
def fetch_pending_yinyan_song_records(cur, limit: int) -> list[dict]:
cur.execute(
"""
SELECT song_id, record_id
FROM yinyan_song_records
WHERE is_yinyan_push = FALSE
ORDER BY song_id
LIMIT %s
""",
(limit,),
)
rows = cur.fetchall()
return [{'song_id': row[0], 'record_id': row[1]} for row in rows]
def fetch_yinyan_records_missing_platform(cur, limit: int) -> list[dict]:
cur.execute(
"""
SELECT song_id, record_id
FROM yinyan_song_records
WHERE platform IS NULL
ORDER BY song_id
LIMIT %s
""",
(limit,),
)
rows = cur.fetchall()
return [{'song_id': row[0], 'record_id': row[1]} for row in rows]
def update_yinyan_record_platforms(cur, records: list[dict]) -> None:
if not records:
return
values_sql = ', '.join(['(%s::bigint, %s::bigint, %s::varchar)'] * len(records))
params = []
for r in records:
params.extend([r['song_id'], r['record_id'], r['platform']])
cur.execute(
f"""
UPDATE yinyan_song_records AS ysr
SET platform = v.platform
FROM (VALUES {values_sql}) AS v(song_id, record_id, platform)
WHERE ysr.song_id = v.song_id
AND ysr.record_id = v.record_id
AND ysr.platform IS NULL
""",
tuple(params),
)
def upsert_yinyan_song_records(cur, records: list[dict]) -> None:
"""Mark pre-initialized yinyan song-record rows as pushed to crawler.
Matches only on song_id so singer-fallback can use a different record_id than initialized.
"""
if not records:
return
cur.executemany(
"""
UPDATE yinyan_song_records
SET platform = %s,
platform_song_id = %s,
record_id = %s,
is_yinyan_push = TRUE
WHERE song_id = %s AND is_yinyan_push = FALSE
""",
[
(r['platform'], r['platform_song_id'], r['record_id'], r['song_id'])
for r in records
],
)
# ─── QQ Music ────────────────────────────────────────────────────────────────
def upsert_qq_singers(cur, singers: list[dict]) -> None:
if not singers:
return
sql = """
INSERT INTO crawler_qqmusic_singers
(id, mid, name, avatar, sex, area, "index", intro, home_url,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s::singer_sex, %s::singer_area, %s::singer_index, %s, %s,
NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
s['id'], s['mid'], s['name'], s.get('avatar', ''),
s.get('sex') or 'U', s.get('area') or '其他', s.get('index') or '#',
s.get('intro'), s.get('home_url'),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in singers]
cur.executemany(sql, rows)
def upsert_qq_albums(cur, albums: list[dict]) -> None:
if not albums:
return
sql = """
INSERT INTO crawler_qqmusic_albums
(id, mid, cover, title, intro, type, company_id, company, is_owner, published_at,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
a['id'], a.get('mid', ''), a.get('cover', ''), a.get('title', ''),
a.get('intro'), a.get('type', ''), a.get('company_id', 0) or 0,
a.get('company', ''), a.get('is_owner', 0) or 0, a.get('published_at'),
a.get('provider_name'), a.get('crawler_source_data'),
) for a in albums]
cur.executemany(sql, rows)
def upsert_qq_songs(cur, songs: list[dict]) -> None:
if not songs:
return
sql = """
INSERT INTO crawler_qqmusic_songs
(id, platform_song_id, mid, album_id, cover, title, name, duration,
lyric, composer_name, lyricist_name, url, lyric_url,
platform_index_url, published_at, album, singers, status, created_at, updated_at,
version, audio_md5, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s::json, %s::jsonb, 0, NOW(), NOW(), %s, %s, %s, %s::json)
ON CONFLICT (platform_song_id) DO NOTHING
"""
rows = [(
s['song_uuid'], s['platform_song_id'], s['mid'], s.get('album_id'),
s.get('cover', ''), s.get('title', ''), s.get('name', ''), s.get('duration', 0) or 0,
s.get('lyric'), s.get('composer_name'), s.get('lyricist_name'),
s.get('url', ''), s.get('lyric_url'),
s.get('platform_index_url'), s.get('published_at'), s.get('album_json'), s.get('singers_json', '[]'),
s.get('version'),
s.get('audio_md5'),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in songs]
cur.executemany(sql, rows)
def upsert_qq_singer_songs(cur, pairs: list[tuple]) -> None:
"""pairs: [(singer_id, song_uuid), ...]"""
if not pairs:
return
sql = """
INSERT INTO crawler_qqmusic_singer_songs (id, singer_id, song_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, song_id) DO NOTHING
"""
rows = [(str(uuid.uuid4()), singer_id, song_uuid) for singer_id, song_uuid in pairs]
cur.executemany(sql, rows)
def upsert_qq_singer_albums(cur, pairs: list[tuple]) -> None:
"""pairs: [(singer_id, album_id), ...]"""
if not pairs:
return
sql = """
INSERT INTO crawler_qqmusic_singer_albums (id, singer_id, album_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, album_id) DO NOTHING
"""
rows = [(str(uuid.uuid4()), singer_id, album_id) for singer_id, album_id in pairs]
cur.executemany(sql, rows)
# ─── Kugou ───────────────────────────────────────────────────────────────────
def upsert_kugou_singers(cur, singers: list[dict]) -> None:
if not singers:
return
sql = """
INSERT INTO crawler_kugou_singers
(id, name, avatar, sex, area, "index", intro, home_url,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s::kugou_singer_sex, %s::kugou_singer_area, %s::kugou_singer_index, %s, %s,
NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
s['id'], s['name'], s.get('avatar', ''),
s.get('sex') or 'U', s.get('area') or '其他', s.get('index') or '#',
s.get('intro'), s.get('home_url', ''),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in singers]
cur.executemany(sql, rows)
def upsert_kugou_albums(cur, albums: list[dict]) -> None:
if not albums:
return
sql = """
INSERT INTO crawler_kugou_albums
(id, cover, title, intro, type, company_id, company, is_owner, published_at,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
a['id'], a.get('cover', ''), a.get('title', ''), a.get('intro'),
a.get('type', ''), a.get('company_id', 0) or 0,
a.get('company', ''), a.get('is_owner', 0) or 0, a.get('published_at'),
a.get('provider_name'), a.get('crawler_source_data'),
) for a in albums]
cur.executemany(sql, rows)
def upsert_kugou_songs(cur, songs: list[dict]) -> None:
if not songs:
return
sql = """
INSERT INTO crawler_kugou_songs
(id, platform_song_id, hash, album_audio_id, album_id, cover, title, name, duration,
lyric, composer_name, lyricist_name, url, lyric_url,
platform_index_url, published_at, singers, status, created_at, updated_at,
version, audio_md5, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s::jsonb, 0, NOW(), NOW(), %s, %s, %s, %s::json)
ON CONFLICT (platform_song_id) DO NOTHING
"""
rows = [(
s['song_uuid'], s['platform_song_id'], s.get('hash', ''),
s.get('album_audio_id', 0) or 0, s.get('album_id'),
s.get('cover', ''), s.get('title', ''), s.get('name', ''), s.get('duration', 0) or 0,
s.get('lyric'), s.get('composer_name'), s.get('lyricist_name'),
s.get('url', ''), s.get('lyric_url'),
s.get('platform_index_url'), s.get('published_at'), s.get('singers_json', '[]'),
s.get('version'),
s.get('audio_md5'),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in songs]
cur.executemany(sql, rows)
def upsert_kugou_singer_songs(cur, pairs: list[tuple]) -> None:
if not pairs:
return
sql = """
INSERT INTO crawler_kugou_singer_songs (id, singer_id, song_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, song_id) DO NOTHING
"""
cur.executemany(sql, [(str(uuid.uuid4()), s, sg) for s, sg in pairs])
def upsert_kugou_singer_albums(cur, pairs: list[tuple]) -> None:
if not pairs:
return
sql = """
INSERT INTO crawler_kugou_singer_albums (id, singer_id, album_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, album_id) DO NOTHING
"""
cur.executemany(sql, [(str(uuid.uuid4()), s, a) for s, a in pairs])
# ─── Netease ─────────────────────────────────────────────────────────────────
def upsert_netease_singers(cur, singers: list[dict]) -> None:
if not singers:
return
sql = """
INSERT INTO crawler_netease_singers
(id, name, avatar, sex, area, "index", intro, home_url,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s::netease_singer_sex, %s::netease_singer_area, %s::netease_singer_index, %s, %s,
NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
s['id'], s['name'], s.get('avatar', ''),
s.get('sex') or 'U', s.get('area') or '其他', s.get('index') or '#',
s.get('intro'), s.get('home_url'),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in singers]
cur.executemany(sql, rows)
def upsert_netease_albums(cur, albums: list[dict]) -> None:
if not albums:
return
sql = """
INSERT INTO crawler_netease_albums
(id, cover, title, intro, type, company_id, company, is_owner, published_at,
created_at, updated_at, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW(), %s, %s::json)
ON CONFLICT (id) DO NOTHING
"""
rows = [(
a['id'], a.get('cover', ''), a.get('title', ''), a.get('intro'),
a.get('type', ''), a.get('company_id', 0) or 0,
a.get('company', ''), a.get('is_owner', 0) or 0, a.get('published_at'),
a.get('provider_name'), a.get('crawler_source_data'),
) for a in albums]
cur.executemany(sql, rows)
def upsert_netease_songs(cur, songs: list[dict]) -> None:
if not songs:
return
sql = """
INSERT INTO crawler_netease_songs
(id, platform_song_id, album_id, cover, title, name, duration,
lyric, composer_name, lyricist_name, url, lyric_url,
platform_index_url, published_at, album, singers, status, created_at, updated_at,
version, audio_md5, provider_name, crawler_source_data)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s::json, %s::jsonb, 0, NOW(), NOW(), %s, %s, %s, %s::json)
ON CONFLICT (platform_song_id) DO NOTHING
"""
rows = [(
s['song_uuid'], s['platform_song_id'], s.get('album_id'),
s.get('cover', ''), s.get('title', ''), s.get('name', ''), s.get('duration', 0) or 0,
s.get('lyric'), s.get('composer_name'), s.get('lyricist_name'),
s.get('url', ''), s.get('lyric_url'),
s.get('platform_index_url'), s.get('published_at'), s.get('album_json'), s.get('singers_json', '[]'),
s.get('version'),
s.get('audio_md5'),
s.get('provider_name'), s.get('crawler_source_data'),
) for s in songs]
cur.executemany(sql, rows)
def upsert_netease_singer_songs(cur, pairs: list[tuple]) -> None:
if not pairs:
return
sql = """
INSERT INTO crawler_netease_singer_songs (id, singer_id, song_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, song_id) DO NOTHING
"""
cur.executemany(sql, [(str(uuid.uuid4()), s, sg) for s, sg in pairs])
def upsert_netease_singer_albums(cur, pairs: list[tuple]) -> None:
if not pairs:
return
sql = """
INSERT INTO crawler_netease_singer_albums (id, singer_id, album_id)
VALUES (%s, %s, %s)
ON CONFLICT (singer_id, album_id) DO NOTHING
"""
cur.executemany(sql, [(str(uuid.uuid4()), s, a) for s, a in pairs])