Skip to content

Commit 782a338

Browse files
committed
feat(audio): add fn to fetch audio tracks and pick one track
1 parent ff63798 commit 782a338

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

src/audio.ts

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import axios from 'axios';
2+
import { pick, chain } from 'lodash';
3+
import config from './config';
4+
import qs from 'qs';
5+
import { format } from 'date-fns';
6+
7+
interface SCUser {
8+
avatar_url: string;
9+
id: number;
10+
kind: 'user';
11+
permalink_url: string;
12+
uri: string;
13+
username: string;
14+
permalink: string;
15+
last_modified: Date;
16+
}
17+
18+
export interface Track {
19+
comment_count: number;
20+
release: number;
21+
original_content_size: number;
22+
track_type: string;
23+
original_format: string;
24+
streamable: boolean;
25+
download_url: string;
26+
id: number;
27+
state: 'processing' | 'failed' | 'finished';
28+
last_modified: Date;
29+
favoritings_count: number;
30+
kind: 'track';
31+
purchase_url: string;
32+
release_year: number;
33+
sharing: 'all' | 'private' | 'public';
34+
attachments_uri: string;
35+
license:
36+
| 'no-rights-reserved'
37+
| 'all-rights-reserved'
38+
| 'cc-by'
39+
| 'cc-by-nc'
40+
| 'cc-by-nd'
41+
| 'cc-by-sa'
42+
| 'cc-by-nc-nd'
43+
| 'cc-by-nc-sa';
44+
user_id: number;
45+
user_favorite: boolean;
46+
waveform_url: string;
47+
permalink: string;
48+
permalink_url: string;
49+
playback_count: number;
50+
downloadable: boolean;
51+
created_at: Date;
52+
description: string;
53+
title: string;
54+
duration: number;
55+
artwork_url: string;
56+
video_url: string;
57+
tag_list: string;
58+
release_month: number;
59+
genre: string;
60+
release_day: number;
61+
reposts_count: number;
62+
label_name: string;
63+
commentable: boolean;
64+
bpm: number;
65+
policy: string;
66+
key_signature: string;
67+
isrc: string;
68+
uri: string;
69+
download_count: number;
70+
likes_count: number;
71+
purchase_title: string;
72+
embeddable_by: 'all' | 'me' | 'none';
73+
monetization_model: string;
74+
user: SCUser;
75+
user_playback_count: number;
76+
stream_url: string;
77+
label_id: number;
78+
}
79+
80+
interface SCUserWebProfile {
81+
kind: 'web-profile';
82+
id: number;
83+
service: 'personal' | 'youtube' | 'twitter' | 'instagram' | 'facebook';
84+
title: string;
85+
url: string;
86+
username: string;
87+
created_at: string;
88+
}
89+
90+
export const getTracksFromSoundcloud = async () => {
91+
try {
92+
console.log('fetching tracks');
93+
const response = await axios.get<Track[]>(
94+
`https://api.soundcloud.com/tracks`,
95+
{
96+
params: {
97+
client_id: config.SOUNDCLOUD_CLIENT_ID,
98+
tags: config.SOUNDCLOUD_SEARCH_TAGS.join(','),
99+
license: config.SOUNDCLOUD_SEARCH_LICENSE,
100+
created_at: {
101+
from: encodeURIComponent(
102+
format(
103+
new Date(Date.now() - 60 * 60 * 24 * 1000),
104+
'YYYY-MM-DD HH:mm:ss'
105+
)
106+
),
107+
to: encodeURIComponent(format(new Date(), 'YYYY-MM-DD HH:mm:ss')),
108+
},
109+
},
110+
paramsSerializer: params => {
111+
let stringified = qs.stringify(params, {
112+
arrayFormat: 'brackets',
113+
encode: false,
114+
});
115+
console.log(stringified);
116+
return stringified;
117+
},
118+
}
119+
);
120+
121+
console.log(`fetched and got ${response.data.length} responses`);
122+
const pickedSong = response.data
123+
.sort(e => (e.playback_count || 0) + (e.likes_count || 0))
124+
.filter(e => e.downloadable)[0];
125+
126+
console.log('picked song: ' + pickedSong.id);
127+
return pick(pickedSong, [
128+
'download_url',
129+
'user',
130+
'description',
131+
'title',
132+
'purchase_title',
133+
'purchase_url',
134+
'tag_list',
135+
'permalink_url',
136+
'id',
137+
'duration',
138+
]);
139+
} catch (e) {
140+
return Promise.reject(e);
141+
}
142+
};
143+
144+
const pickUsername = (obj: SCUserWebProfile) =>
145+
pick(obj, ['username', 'service']);
146+
147+
export const getWebProfileForArtist = async (id: number) => {
148+
try {
149+
const { data: webProfiles } = await axios.get<SCUserWebProfile[]>(
150+
`https://api.soundcloud.com/users/${id}/web-profiles`
151+
);
152+
153+
if (webProfiles.length <= 0) {
154+
return;
155+
}
156+
157+
const profilesHash = chain(webProfiles)
158+
.groupBy('service')
159+
.mapValues(0)
160+
.value();
161+
162+
if (profilesHash.twitter) {
163+
return pickUsername(profilesHash.twitter);
164+
} else if (profilesHash.instagram) {
165+
return pick(profilesHash.instagram);
166+
} else {
167+
return;
168+
}
169+
} catch (e) {
170+
return Promise.reject(e);
171+
}
172+
};

0 commit comments

Comments
 (0)