import/export to gist using markdown preview

This commit is contained in:
Listen 1 2017-12-26 20:52:14 +08:00
parent ec98406914
commit fa600e40d0
3 changed files with 51 additions and 23 deletions

View File

@ -517,42 +517,42 @@
items[key] = value;
}
}
var content = JSON.stringify(items);
var gistFiles = gist.json2gist(items);
$scope.gistBackupLoading = true;
gist.backupMySettings2Gist(content).then(function(){
gist.backupMySettings2Gist(gistFiles).then(function(){
Notification.clearAll();
Notification.success("成功备份歌单到Gist");
Notification.success("成功导出歌单到Gist");
$scope.gistBackupLoading = false;
},function(err){
Notification.clearAll();
Notification.warning("备份歌单失败,检查后重试");
Notification.warning("导出歌单失败,检查后重试");
$scope.gistBackupLoading = false;
});
Notification({message: "正在备份歌单到Gist...", delay: null});
Notification({message: "正在导出歌单到Gist...", delay: null});
}
$scope.gistRestoreLoading = false;
$scope.importMySettingsFromGist = function(){
$scope.gistRestoreLoading = true;
gist.importMySettingsFromGist().then(function(raw){
var data = JSON.parse(raw);
var data = gist.gist2json(raw);
for ( var key in data) {
var value = data[key];
localStorage.setObject(key, value);
}
Notification.clearAll();
Notification.success("恢复我的歌单成功");
Notification.success("导入歌单成功");
$scope.gistRestoreLoading = false;
},function(err){
Notification.clearAll();
if(err==404){
Notification.warning("未找到备份歌单,请先备份");
}else{
Notification.warning("恢复歌单失败,检查后重试");
Notification.warning("导入歌单失败,检查后重试");
}
$scope.gistRestoreLoading = false;
})
Notification({message: "正在从Gist恢复我的歌单...", delay: null});
Notification({message: "正在从Gist导入歌单...", delay: null});
}

View File

@ -35,7 +35,37 @@ ngGithub.provider('gist', {
return localStorage.getObject("gistid");
}
function backup(filecontent) {
function json2gist(jsonObject) {
var result = {};
result['listen1_backup.json'] = {content: JSON.stringify(jsonObject)};
//var markdown = '# My Listen1 Playlists\n';
var playlistIds = jsonObject['playerlists'];
for(var i=0; i<playlistIds.length; i++) {
var playlistId = playlistIds[i];
var playlist = jsonObject[playlistId];
var cover = ' <img src="' + playlist.info.cover_img_url + '" width="140" height="140"><br/>';
var title = playlist.info.title;
var tableHeader = '\n| 音乐标题 | 歌手 | 专辑 |\n';
tableHeader += '| --- | --- | --- |\n';
var tableBody = '';
for(var j=0; j<playlist.tracks.length; j++) {
var track = playlist.tracks[j];
tableBody += '| ' + track.title + ' | ' + track.artist + ' | ' + track.album + ' | \n';
}
var content = '<details>\n <summary>' + cover + ' ' + title + '</summary><p>\n' + tableHeader + tableBody +'</p></details>';
var filename = 'listen1_'+playlistId + '.md';
result[filename] = {content: content};
}
return result;
}
function gist2json(gistFiles) {
var jsonString = gistFiles['listen1_backup.json'].content;
return JSON.parse(jsonString);
}
function backup(files) {
var deferred = $q.defer();
var gistId = _getGistId();
var method = '';
@ -55,11 +85,7 @@ ngGithub.provider('gist', {
data:{
"description": "updated by Listen1(http://listen1.github.io/listen1/) at " + new Date().toLocaleString(),
"public": false,
"files": {
"listen1_backup.json": {
"content": filecontent
}
}
"files": files
}
}).then(function(res) {
var newGistId = res.data.id;
@ -78,8 +104,8 @@ ngGithub.provider('gist', {
url:apiUrl + '/' + _getGistId(),
}).then(function(res) {
try{
var backupcontent = res.data.files["listen1_backup.json"].content;
deferred.resolve(backupcontent);
var files = res.data.files
deferred.resolve(files);
}catch(e){
deferred.reject(404);
}
@ -89,6 +115,8 @@ ngGithub.provider('gist', {
return deferred.promise;
}
var gistApi = {
"gist2json": gist2json,
"json2gist": json2gist,
"backupMySettings2Gist": backup,
"importMySettingsFromGist": restore,
};

View File

@ -279,21 +279,21 @@
<div ng-show="isDoubanLogin"> 豆瓣:<button class="btn btn-primary confirm-button" ng-click="showDialog(2)">已登录</button></div>
<div ng-hide="isDoubanLogin"> 豆瓣:<button class="btn btn-primary confirm-button" ng-click="showDialog(2)">登录</button> </div>
</div> -->
<div class="settings-title"><span>数据备份<span></div>
<div class="settings-title"><span>歌单导出<span></div>
<div class="settings-content">
<p>重装插件或清除缓存数据会导致我的歌单数据丢失,强烈建议在这些操作前,备份我的歌单。</p>
<div>
<button class="btn btn-primary confirm-button" ng-click="backupMySettings()">下载备份文件</button>
<button class="btn btn-primary confirm-button" ng-disabled="gistBackupLoading" ng-show="github.getStatus() == 2" ng-click="backupMySettings2Gist()">备份到Gist</button>
<button class="btn btn-primary confirm-button" ng-click="backupMySettings()">导出歌单到本地文件</button>
<button class="btn btn-primary confirm-button" ng-disabled="gistBackupLoading" ng-show="github.getStatus() == 2" ng-click="backupMySettings2Gist()">导出歌单到Github Gist</button>
</div>
</div>
<div class="settings-title"><span>数据恢复<span></div>
<div class="settings-title"><span>歌单导入<span></div>
<div class="settings-content">
<p>选择备份文件,恢复我的歌单。注意:恢复我的歌单会覆盖现有的歌单。</p>
<label class="btn btn-warning" for="my-file-selector">
<input id="my-file-selector" type="file" style="display:none;" ng-model="myuploadfiles" custom-on-change="importMySettings">上传备份文件
<input id="my-file-selector" type="file" style="display:none;" ng-model="myuploadfiles" custom-on-change="importMySettings">从本地文件导入歌单
</label>
<button class="btn btn-warning confirm-button" ng-disabled="gistRestoreLoading" ng-show="github.getStatus() == 2" ng-click="importMySettingsFromGist()">从Gist恢复</button>
<button class="btn btn-warning confirm-button" ng-disabled="gistRestoreLoading" ng-show="github.getStatus() == 2" ng-click="importMySettingsFromGist()">从Github Gist导入歌单</button>
</div>
<div class="settings-title"><span>连接到 Github.com<span></div>