Skip to content

Commit

Permalink
feat(audio): add fn to fetch audio tracks and pick one track
Browse files Browse the repository at this point in the history
  • Loading branch information
Buzzertech committed Jun 14, 2019
1 parent ff63798 commit 782a338
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions src/audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import axios from 'axios';
import { pick, chain } from 'lodash';
import config from './config';
import qs from 'qs';
import { format } from 'date-fns';

interface SCUser {
avatar_url: string;
id: number;
kind: 'user';
permalink_url: string;
uri: string;
username: string;
permalink: string;
last_modified: Date;
}

export interface Track {
comment_count: number;
release: number;
original_content_size: number;
track_type: string;
original_format: string;
streamable: boolean;
download_url: string;
id: number;
state: 'processing' | 'failed' | 'finished';
last_modified: Date;
favoritings_count: number;
kind: 'track';
purchase_url: string;
release_year: number;
sharing: 'all' | 'private' | 'public';
attachments_uri: string;
license:
| 'no-rights-reserved'
| 'all-rights-reserved'
| 'cc-by'
| 'cc-by-nc'
| 'cc-by-nd'
| 'cc-by-sa'
| 'cc-by-nc-nd'
| 'cc-by-nc-sa';
user_id: number;
user_favorite: boolean;
waveform_url: string;
permalink: string;
permalink_url: string;
playback_count: number;
downloadable: boolean;
created_at: Date;
description: string;
title: string;
duration: number;
artwork_url: string;
video_url: string;
tag_list: string;
release_month: number;
genre: string;
release_day: number;
reposts_count: number;
label_name: string;
commentable: boolean;
bpm: number;
policy: string;
key_signature: string;
isrc: string;
uri: string;
download_count: number;
likes_count: number;
purchase_title: string;
embeddable_by: 'all' | 'me' | 'none';
monetization_model: string;
user: SCUser;
user_playback_count: number;
stream_url: string;
label_id: number;
}

interface SCUserWebProfile {
kind: 'web-profile';
id: number;
service: 'personal' | 'youtube' | 'twitter' | 'instagram' | 'facebook';
title: string;
url: string;
username: string;
created_at: string;
}

export const getTracksFromSoundcloud = async () => {
try {
console.log('fetching tracks');
const response = await axios.get<Track[]>(
`https://api.soundcloud.com/tracks`,
{
params: {
client_id: config.SOUNDCLOUD_CLIENT_ID,
tags: config.SOUNDCLOUD_SEARCH_TAGS.join(','),
license: config.SOUNDCLOUD_SEARCH_LICENSE,
created_at: {
from: encodeURIComponent(
format(
new Date(Date.now() - 60 * 60 * 24 * 1000),
'YYYY-MM-DD HH:mm:ss'
)
),
to: encodeURIComponent(format(new Date(), 'YYYY-MM-DD HH:mm:ss')),
},
},
paramsSerializer: params => {
let stringified = qs.stringify(params, {
arrayFormat: 'brackets',
encode: false,
});
console.log(stringified);
return stringified;
},
}
);

console.log(`fetched and got ${response.data.length} responses`);
const pickedSong = response.data
.sort(e => (e.playback_count || 0) + (e.likes_count || 0))
.filter(e => e.downloadable)[0];

console.log('picked song: ' + pickedSong.id);
return pick(pickedSong, [
'download_url',
'user',
'description',
'title',
'purchase_title',
'purchase_url',
'tag_list',
'permalink_url',
'id',
'duration',
]);
} catch (e) {
return Promise.reject(e);
}
};

const pickUsername = (obj: SCUserWebProfile) =>
pick(obj, ['username', 'service']);

export const getWebProfileForArtist = async (id: number) => {
try {
const { data: webProfiles } = await axios.get<SCUserWebProfile[]>(
`https://api.soundcloud.com/users/${id}/web-profiles`
);

if (webProfiles.length <= 0) {
return;
}

const profilesHash = chain(webProfiles)
.groupBy('service')
.mapValues(0)
.value();

if (profilesHash.twitter) {
return pickUsername(profilesHash.twitter);
} else if (profilesHash.instagram) {
return pick(profilesHash.instagram);
} else {
return;
}
} catch (e) {
return Promise.reject(e);
}
};

0 comments on commit 782a338

Please sign in to comment.