-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor youtube fetchTitle for track model
- Loading branch information
Hugo Vieilledent
committed
Jul 13, 2018
1 parent
da696c9
commit f0e46f1
Showing
2 changed files
with
42 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Ember from 'ember'; | ||
import config from 'radio4000/config/environment'; | ||
const {debug} = Ember; | ||
|
||
const fetchYoutubeTrack = async (ytid, fields) => { | ||
if (!ytid) { | ||
throw Error('You need to provide a youtube video to fetchYoutubeTrack') | ||
} | ||
if (!fields) { | ||
fields = `items(id,snippet,contentDetails,statistics)&part=snippet,contentDetails,statistics`; | ||
} | ||
|
||
let rootUrl = `https://www.googleapis.com/youtube/v3/videos?key=${config.youtubeApiKey}` | ||
let url = rootUrl + `&id=${ytid}&fields=${fields}` | ||
let response = await fetch(url); | ||
let data = await response.json(); | ||
if (!data.items.length) { | ||
debug('Could not find title for track'); | ||
return; | ||
} | ||
return data.items[0] | ||
} | ||
|
||
const fetchTitle = async function (ytid) { | ||
let data = await fetchYoutubeTrack(ytid, 'items(id,snippet(title))&part=snippet'); | ||
return data.snippet.title | ||
} | ||
|
||
|
||
const fetchTrackAvailability = async function (ytid) { | ||
// let data = await fetchYoutubeTrack(ytid, 'items(id,status)&part=status') | ||
let data = await fetchYoutubeTrack(ytid, 'items(id,snippet(title))&part=snippet'); | ||
return Boolean(data.snippet.title) | ||
} | ||
|
||
export {fetchTitle, fetchTrackAvailability}; |