<commit_before>import ws, {playlists, addSong, removeSong, playSong,sendMessage } from '../../../api/socket';

// ------------------------------------
// Constants
// ------------------------------------
export const PLAYLIST_ADD = 'PLAYLIST_ADD';
export const PLAYLIST_REMOVE = 'PLAYLIST_REMOVE';
export const PLAYLIST_PLAY = 'PLAYLIST_PLAY';
export const PLAYLIST_GET = 'PLAYLIST_GET';
export const PLAYLIST_MESSAGE='PLAYLIST_MESSAGE';
const soundcloud_key = '<KEY>';

let SC: any = window['SC'];
SC.initialize({
  client_id: soundcloud_key
});

function parseQueryString(url): any {
    let queryIndex = url.indexOf('?'),
        queryObject = {},
        pairs;
    if (!url) {
        return null;
    }
    let queryString = url;
    if (queryIndex != -1) {
        queryString = url.substr(queryIndex + 1);
    }
    pairs = queryString.split('&');
    for (let pair of pairs) {
        if (pair === '') {
            continue;
        }
        let parts = pair.split(/=(.+)?/),
            key = parts[0],
            value = parts[1] && parts[1].replace(/\+/g, ' ');
        let existing = queryObject[key];
        if (existing) {
            if (Array.isArray(existing)) {
                existing.push(value);
            } else {
                queryObject[key] = [existing, value];
            }
        } else {
            queryObject[key] = value;
        }
    }
    return queryObject;
}

// ------------------------------------
// Actions
// ------------------------------------
export function get(data) {
    return {
        type: PLAYLIST_GET,
        payload: data
    }
}


export function add(data) {
    return {
        type: PLAYLIST_ADD,
        payload: data
    }
}
export function remove(data) {
    return {
        type: PLAYLIST_REMOVE,
        payload: data
    }
}
export function play(data) {
    return {
        type: PLAYLIST_PLAY,
        payload: data
    }
}
export function onMessage(user,data) {
    return {
        type: PLAYLIST_MESSAGE,
        payload: data
    }
}
export function initAsync() {
    return (dispatch, getState) => {
        playlists().then((data) => {
            dispatch(get(data));
        });
        ws.on('song.add', (data) => {
            dispatch(add(data));
        }).on('song.remove', (data) => {
            dispatch(remove(data));
        }).on('song.play', data => {
            dispatch(play(data));
        }).on('send.message',(user,data)=>{
            dispatch(onMessage(user,data));
        });
    }
}

export function addAsync(url) {
    return (dispatch, getState) => {
        let user = getState().session.user;
        if (url.indexOf('http://music.163.com') === 0) {
             let surl = url.replace('#/', '');                           
             let refer=encodeURIComponent(`http://music.163.com`);
  
             let queryParams = parseQueryString(surl);
             if (queryParams.id) {
                 if (surl.indexOf('song') > -1) {
                    let api = encodeURIComponent( `http://music.163.com/api/song/detail/?id=${queryParams.id}&ids=[${queryParams.id}]`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let data = res.songs[0];
                            if (data.mp3Url != null) {
                                let m = {
                                    id: 'wangyi-' + data.id,
                                    name: data.name,
                                    artists: data.artists.map((item => item.name)),
                                    album: data.album.name,
                                    image: data.album.picUrl,
                                    resourceUrl: data.mp3Url,
                                    orderer: user
                                };
                                addSong(m);
                            }else {
                                alert('不支持播放');
                            }
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                }
                if (surl.indexOf('playlist') > -1) {
                    //http://music.163.com/api/playlist/detail?id=493682409
                    let api = encodeURIComponent( `http://music.163.com/api/playlist/detail/?id=${queryParams.id}`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&refer=${refer}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let tracks = res.result.tracks.filter((track) => track.mp3Url != null);
                            if (confirm(tracks.length + ' songs added?')) {
                                let items = tracks.map((data) => {
                                    return {
                                        id: 'wangyi-' + data.id,
                                        name: data.name,
                                        artists: data.artists.map((item => item.name)),
                                        album: data.album.name,
                                        image: data.album.picUrl,
                                        resourceUrl: data.mp3Url,
                                        orderer: user
                                    }
                                })
                                addSong(items);
                            }
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                }
                if(surl.indexOf('album')>-1){
                    let api=encodeURIComponent(`http://music.163.com/api/album/${queryParams.id}`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&refer=${refer}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let songs = res.album.songs.filter((song) => song.mp3Url != null);
                            if (confirm(songs.length + ' songs added?')) {
                                let items = songs.map((data) => {
                                    return {
                                        id: 'wangyi-' + data.id,
                                        name: data.name,
                                        artists: data.artists.map((item => item.name)),
                                        album: data.album.name,
                                        image: data.album.picUrl,
                                        resourceUrl: data.mp3Url,
                                        orderer: user
                                    }
                                })
                                addSong(items);
                            }
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                    
                }
             }

        }
        if (url.indexOf('http://www.xiami.com/song') === 0) {
            let location = url.split('?')[0];
            let id = location.replace('http://www.xiami.com/song/', '');
            let req = `http://www.xiami.com/song/playlist/id/${id}/object_name/default/object_id/0/cat/json?callback=?`;
            return $.ajax({<commit_msg>updated,<commit_after>import ws, {playlists, addSong, removeSong, playSong,sendMessage } from '../../../api/socket';

// ------------------------------------
// Constants
// ------------------------------------
export const PLAYLIST_ADD = 'PLAYLIST_ADD';
export const PLAYLIST_REMOVE = 'PLAYLIST_REMOVE';
export const PLAYLIST_PLAY = 'PLAYLIST_PLAY';
export const PLAYLIST_GET = 'PLAYLIST_GET';
export const PLAYLIST_MESSAGE='PLAYLIST_MESSAGE';
const soundcloud_key = '<KEY>';

let SC: any = window['SC'];
SC.initialize({
  client_id: soundcloud_key
});

function parseQueryString(url): any {
    let queryIndex = url.indexOf('?'),
        queryObject = {},
        pairs;
    if (!url) {
        return null;
    }
    let queryString = url;
    if (queryIndex != -1) {
        queryString = url.substr(queryIndex + 1);
    }
    pairs = queryString.split('&');
    for (let pair of pairs) {
        if (pair === '') {
            continue;
        }
        let parts = pair.split(/=(.+)?/),
            key = parts[0],
            value = parts[1] && parts[1].replace(/\+/g, ' ');
        let existing = queryObject[key];
        if (existing) {
            if (Array.isArray(existing)) {
                existing.push(value);
            } else {
                queryObject[key] = [existing, value];
            }
        } else {
            queryObject[key] = value;
        }
    }
    return queryObject;
}

// ------------------------------------
// Actions
// ------------------------------------
export function get(data) {
    return {
        type: PLAYLIST_GET,
        payload: data
    }
}


export function add(data) {
    return {
        type: PLAYLIST_ADD,
        payload: data
    }
}
export function remove(data) {
    return {
        type: PLAYLIST_REMOVE,
        payload: data
    }
}
export function play(data) {
    return {
        type: PLAYLIST_PLAY,
        payload: data
    }
}
export function onMessage(user,data) {
    return {
        type: PLAYLIST_MESSAGE,
        payload: data
    }
}
export function initAsync() {
    return (dispatch, getState) => {
        playlists().then((data) => {
            dispatch(get(data));
        });
        ws.on('song.add', (data) => {
            dispatch(add(data));
        }).on('song.remove', (data) => {
            dispatch(remove(data));
        }).on('song.play', data => {
            dispatch(play(data));
        }).on('send.message',(user,data)=>{
            dispatch(onMessage(user,data));
        });
    }
}

export function addAsync(url) {
    return (dispatch, getState) => {
        let user = getState().session.user;
        if (url.indexOf('http://music.163.com') === 0) {
             let surl = url.replace('#/', '');                           
             let refer=encodeURIComponent(`http://music.163.com`);
  
             let queryParams = parseQueryString(surl);
             if (queryParams.id) {
                 if (surl.indexOf('song') > -1) {
                    let api = encodeURIComponent( `http://music.163.com/api/song/detail/?id=${queryParams.id}&ids=[${queryParams.id}]`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let data = res.songs[0];
                            if (data.mp3Url != null) {
                                let m = {
                                    id: 'wangyi-' + data.id,
                                    name: data.name,
                                    artists: data.artists.map((item => item.name)),
                                    album: data.album.name,
                                    image: data.album.picUrl,
                                    resourceUrl: data.mp3Url,
                                    orderer: user
                                };
                                addSong(m);
                            }else {
                                alert('不支持播放');
                            }
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                }
                if (surl.indexOf('playlist') > -1) {
                    //http://music.163.com/api/playlist/detail?id=493682409
                    let api = encodeURIComponent( `http://music.163.com/api/playlist/detail/?id=${queryParams.id}`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&refer=${refer}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let tracks = res.result.tracks.filter((track) => track.mp3Url != null);
                            if (confirm(tracks.length + ' songs added?')) {
                                let items = tracks.map((data) => {
                                    return {
                                        id: 'wangyi-' + data.id,
                                        name: data.name,
                                        artists: data.artists.map((item => item.name)),
                                        album: data.album.name,
                                        image: data.album.picUrl,
                                        resourceUrl: data.mp3Url,
                                        orderer: user
                                    }
                                })
                                addSong(items);
                            }
                        }else{                            
                            alert('解析失败')
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                }
                if(surl.indexOf('album')>-1){
                    let api=encodeURIComponent(`http://music.163.com/api/album/${queryParams.id}`);
                    return $.ajax({
                        url: `http://myproxy.applinzi.com/get.php?url=${api}&refer=${refer}&callback=?`,
                        dataType: 'jsonp'
                    }).done((res) => {
                        if (res.code === 200) {
                            let songs = res.album.songs.filter((song) => song.mp3Url != null);
                            if (confirm(songs.length + ' songs added?')) {
                                let items = songs.map((data) => {
                                    return {
                                        id: 'wangyi-' + data.id,
                                        name: data.name,
                                        artists: data.artists.map((item => item.name)),
                                        album: data.album.name,
                                        image: data.album.picUrl,
                                        resourceUrl: data.mp3Url,
                                        orderer: user
                                    }
                                })
                                addSong(items);
                            }
                        }else{                            
                            alert('解析失败')
                        }
                    }).fail(() => {
                        alert('解析失败')
                    });
                    
                }
             }

        }
        if (url.indexOf('http://www.xiami.com/song') === 0) {
            let location = url.split('?')[0];
            let id = location.replace('http://www.xiami.com/song/', '');
            let req = `http://www.xiami.com/song/playlist/id/${id}/object_name/default/object_id/0/cat/json?callback=?`;
            return $.ajax({
{'fraction_non_alphanumeric': 0.14772067831581662, 'fraction_numerical': 0.007092655877232575, 'mean_word_length': 0.950207468879668, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6711076', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3783, 'n_words': 665}