forked from nukeop/nuclear
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle downloads via ipc, report on progress
- Loading branch information
Showing
10 changed files
with
139 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
import _ from 'lodash'; | ||
export const ADD_TO_DOWNLOADS = 'ADD_TO_DOWNLOADS'; | ||
export const DOWNLOAD_STARTED = 'DOWNLOAD_STARTED'; | ||
export const DOWNLOAD_PROGRESS = 'DOWNLOAD_PROGRESS'; | ||
export const DOWNLOAD_FINISHED = 'DOWNLOAD_FINISHED'; | ||
export const DOWNLOAD_ERROR = 'DOWNLOAD_ERROR'; | ||
|
||
function addTrackToDownloads(track) { | ||
export function addToDownloads(musicSources, track) { | ||
return { | ||
type: ADD_TO_DOWNLOADS, | ||
payload: { item: { | ||
status: 'Started', | ||
status: 'Waiting', | ||
completion: 0, | ||
track | ||
} } | ||
}; | ||
} | ||
|
||
export function addToDownloads(musicSources, track) { | ||
return dispatch => { | ||
Promise.all(_.map(musicSources, m => m.search({ artist: track.artist, track: track.name }))) | ||
.then(results => Promise.all(results)) | ||
.then(streams => { | ||
dispatch( | ||
addTrackToDownloads( | ||
Object.assign( | ||
{}, | ||
track, | ||
{ streams } | ||
) | ||
) | ||
); | ||
}); | ||
export function onDownloadStarted(uuid) { | ||
return { | ||
type: DOWNLOAD_STARTED, | ||
payload: { | ||
uuid | ||
} | ||
}; | ||
} | ||
|
||
export function onDownloadProgress(uuid, progress) { | ||
return { | ||
type: DOWNLOAD_PROGRESS, | ||
payload: { | ||
uuid, | ||
progress | ||
} | ||
}; | ||
} | ||
|
||
export function onDownloadFinished(uuid) { | ||
return { | ||
type: DOWNLOAD_FINISHED, | ||
payload: { | ||
uuid | ||
} | ||
}; | ||
} |
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
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
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,9 @@ | ||
import globals from '../globals'; | ||
|
||
export function prepareUrl (url) { | ||
return `${url}&key=${globals.ytApiKey}`; | ||
} | ||
|
||
export function trackSearch (track) { | ||
return fetch(prepareUrl('https://www.googleapis.com/youtube/v3/search?part=id,snippet&type=video&maxResults=50&q=' + encodeURIComponent(track))); | ||
} |
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 |
---|---|---|
@@ -1,19 +1,55 @@ | ||
import electronDl from 'electron-dl'; | ||
import 'isomorphic-fetch'; | ||
import electronDl, { download } from 'electron-dl'; | ||
import logger from 'electron-timber'; | ||
import ytdl from 'ytdl-core'; | ||
import _ from 'lodash'; | ||
import { BrowserWindow, ipcMain } from 'electron'; | ||
import { ipcMain } from 'electron'; | ||
|
||
import { trackSearch } from '../app/rest/youtube-search'; | ||
|
||
export const registerDownloadsEvents = () => { | ||
let rendererWindow = null; | ||
electronDl(); | ||
|
||
export const registerDownloadsEvents = window => { | ||
ipcMain.once('started', event => { | ||
rendererWindow = event.sender; | ||
}); | ||
|
||
ipcMain.on('start-download', (event, data) => { | ||
const streamUrl = _.get(data, 'streams[0].stream'); | ||
electronDl(); | ||
logger.log(`Downloading from ${streamUrl}`); | ||
const win = BrowserWindow.getFocusedWindow(); | ||
electronDl.download(win, streamUrl) | ||
.then(result => { | ||
logger.log(result); | ||
const query = `${_.get(data, 'artist.name')} ${_.get(data, 'name')}`; | ||
const filename = `${_.get(data, 'artist.name')} - ${_.get(data, 'name')}`; | ||
|
||
// In order to get a valid stream URL, we need to generate it right before downloading | ||
trackSearch(query) | ||
.then(results => results.json()) | ||
.then(data => { | ||
const trackId = _.get(_.head(data.items), 'id.videoId'); | ||
return ytdl.getInfo(`http://www.youtube.com/watch?v=${trackId}`); | ||
}) | ||
.then(videoInfo => { | ||
const formatInfo = _.head(videoInfo.formats.filter(e => e.itag === '140')); | ||
const streamUrl = formatInfo.url; | ||
|
||
return download(window, streamUrl, { | ||
filename: filename, | ||
onStarted: () => { | ||
rendererWindow.send('download-started', data.uuid); | ||
}, | ||
onProgress: progress => { | ||
rendererWindow.send('download-progress', { | ||
uuid: data.uuid, | ||
progress | ||
}); | ||
} | ||
}); | ||
}) | ||
.then(() => { | ||
rendererWindow.send('download-finished', data.uuid); | ||
}) | ||
.catch(logger.error); | ||
.catch(error => { | ||
logger.error(error); | ||
rendererWindow.send('download-error', { uui: data.uuid, error }); | ||
}); | ||
|
||
}); | ||
}; |
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