translate tg.js, add percentage in task content, add one more button to copy

This commit is contained in:
liaojack8 2020-07-04 01:37:34 +08:00
parent e9e37d1d03
commit 45c995526e
2 changed files with 51 additions and 43 deletions

View File

@ -12,7 +12,7 @@ const { AUTH, RETRY_LIMIT, PARALLEL_LIMIT, TIMEOUT_BASE, TIMEOUT_MAX, LOG_DELAY,
const { db } = require('../db')
const { make_table, make_tg_table, make_html, summary } = require('./summary')
const FILE_EXCEED_MSG = '您的团队盘文件数已超限(40万),停止复制'
const FILE_EXCEED_MSG = '您的小組雲端硬碟文件數量已超過限制(40萬),停止複製'
const FOLDER_TYPE = 'application/vnd.google-apps.folder'
const { https_proxy } = process.env
const axins = axios.create(https_proxy ? { httpsAgent: new HttpsProxyAgent(https_proxy) } : {})

View File

@ -9,7 +9,7 @@ const { AUTH, DEFAULT_TARGET } = require('../config')
const { tg_token } = AUTH
const gen_link = (fid, text) => `<a href="https://drive.google.com/drive/folders/${fid}">${text || fid}</a>`
if (!tg_token) throw new Error('请先在auth.js里设置tg_token')
if (!tg_token) throw new Error('請先在auth.js中設定tg_token')
const { https_proxy } = process.env
const axins = axios.create(https_proxy ? { httpsAgent: new HttpsProxyAgent(https_proxy) } : {})
@ -25,16 +25,17 @@ async function get_folder_name (fid) {
module.exports = { send_count, send_help, sm, extract_fid, reply_cb_query, send_choice, send_task_info, send_all_tasks, tg_copy, extract_from_text }
function send_help (chat_id) {
const text = `<pre>[使用帮助]
命令 说明
const text = `<pre>[使用說明]
***不支持單檔分享***
命令 說明
/help | 使
/help | 使
/count shareID [-u] | sourceID, sourceIDgoogle driveID -u线
/count sourceID [-u] | sourceID, sourceIDID -u
/copy sourceID targetID [-u] | sourceIDtargetIDtargetIDconfig.js -u线taskID
/copy sourceID targetID() [-u] | sourceIDtargetIDtargetIDconfig.jsDEFAULT_TARGET -utaskID
/task taskID | all
/task taskID() | taskID all ()
</pre>`
return sm({ chat_id, text, parse_mode: 'HTML' })
}
@ -42,12 +43,15 @@ function send_help (chat_id) {
function send_choice ({ fid, chat_id }) {
return sm({
chat_id,
text: `识别出分享ID ${fid},请选择动`,
text: `辨識到分享ID ${fid},請選擇動`,
reply_markup: {
inline_keyboard: [
[
{ text: '文件统计', callback_data: `count ${fid}` },
{ text: '开始复制', callback_data: `copy ${fid}` }
{ text: '文件統計', callback_data: `count ${fid}` }
],
[
{ text: '開始複製(預設)', callback_data: `copy ${fid}` },
{ text: '開始複製(discord)', callback_data: `disCopy ${fid}` }
]
]
}
@ -56,7 +60,7 @@ function send_choice ({ fid, chat_id }) {
async function send_all_tasks (chat_id) {
let records = db.prepare('select id, status, ctime from task').all()
if (!records.length) return sm({ chat_id, text: '数据库中没有任务记录' })
if (!records.length) return sm({ chat_id, text: '資料庫中沒有任務記錄' })
const tb = new Table({ style: { head: [], border: [] } })
const headers = ['ID', 'status', 'ctime']
records = records.map(v => {
@ -69,13 +73,13 @@ async function send_all_tasks (chat_id) {
return axins.post(url, {
chat_id,
parse_mode: 'HTML',
text: `所有拷贝任务\n<pre>${text}</pre>`
text: `所有拷貝任務\n<pre>${text}</pre>`
}).catch(err => {
// const description = err.response && err.response.data && err.response.data.description
// if (description && description.includes('message is too long')) {
if (true) {
const text = [headers].concat(records).map(v => v.join('\t')).join('\n')
return sm({ chat_id, parse_mode: 'HTML', text: `所有拷贝任务\n<pre>${text}</pre>` })
return sm({ chat_id, parse_mode: 'HTML', text: `所有拷貝任務\n<pre>${text}</pre>` })
}
console.error(err)
})
@ -91,24 +95,28 @@ async function get_task_info (task_id) {
const { file_count, folder_count, total_size } = summary ? JSON.parse(summary) : {}
const copied_files = copied ? copied.trim().split('\n').length : 0
const copied_folders = folder_mapping ? (folder_mapping.length - 1) : 0
let text = '任务编号' + task_id + '\n'
let text = '任務ID' + task_id + '\n'
const folder_name = await get_folder_name(source)
text += '源文件夹' + gen_link(source, folder_name) + '\n'
text += '源資料夾' + gen_link(source, folder_name) + '\n'
text += '目的位置:' + gen_link(target) + '\n'
text += '新文件夹:' + (new_folder ? gen_link(new_folder) : '暂未创建') + '\n'
text += '任务状态:' + status + '\n'
text += '创建时间:' + dayjs(ctime).format('YYYY-MM-DD HH:mm:ss') + '\n'
text += '完成时间:' + (ftime ? dayjs(ftime).format('YYYY-MM-DD HH:mm:ss') : '未完成') + '\n'
text += '目录进度:' + copied_folders + '/' + (folder_count === undefined ? '未知数量' : folder_count) + '\n'
text += '文件进度:' + copied_files + '/' + (file_count === undefined ? '未知数量' : file_count) + '\n'
text += '合计大小:' + (total_size || '未知大小')
text += '新資料夾:' + (new_folder ? gen_link(new_folder) : '尚未創建') + '\n'
text += '任務狀態:' + status + '\n'
text += '創建時間:' + dayjs(ctime).format('YYYY-MM-DD HH:mm:ss') + '\n'
text += '完成時間:' + (ftime ? dayjs(ftime).format('YYYY-MM-DD HH:mm:ss') : '未完成') + '\n'
var pct = copied_folders/(folder_count === undefined ? '未知數量' : folder_count)*100
pct = pct.toFixed(2);
text += '目錄進度:' + copied_folders + '/' + (folder_count === undefined ? '未知數量' : folder_count) + ' - ' + pct + '%\n'
pct = copied_files/(file_count === undefined ? '未知數量' : file_count)*100
pct = pct.toFixed(2);
text += '文件進度:' + copied_files + '/' + (file_count === undefined ? '未知數量' : file_count) + ' - ' + pct + '%\n'
text += '合計大小:' + (total_size || '未知大小')
const total_count = (folder_count || 0) + (file_count || 0)
return { text, status, total_count }
}
async function send_task_info ({ task_id, chat_id }) {
const { text, status, total_count } = await get_task_info(task_id)
if (!text) return sm({ chat_id, text: '数据库不存在此任务ID' + task_id })
if (!text) return sm({ chat_id, text: '資料庫查無此任務ID' + task_id })
const url = `https://api.telegram.org/bot${tg_token}/sendMessage`
let message_id
try {
@ -130,17 +138,17 @@ async function send_task_info ({ task_id, chat_id }) {
async function tg_copy ({ fid, target, chat_id, update }) { // return task_id
target = target || DEFAULT_TARGET
if (!target) {
sm({ chat_id, text: '请输入目的地ID或先在config.js里设置默认复制目的地ID(DEFAULT_TARGET)' })
sm({ chat_id, text: '請輸入目的地ID或先在config.js中設定預設複製的目的地ID(DEFAULT_TARGET)' })
return
}
let record = db.prepare('select id, status from task where source=? and target=?').get(fid, target)
if (record) {
if (record.status === 'copying') {
sm({ chat_id, text: '已有相同源ID和目的ID的任务正在进行,查询进度可输入 /task ' + record.id })
sm({ chat_id, text: '已有相同源ID和目的ID的任務正在進行,查詢進度可輸入 /task ' + record.id })
return
} else if (record.status === 'finished') {
sm({ chat_id, text: `检测到已存在的任务 ${record.id},开始继续拷贝` })
sm({ chat_id, text: `檢測到已存在的任務 ${record.id},開始繼續拷貝` })
}
}
@ -156,18 +164,18 @@ async function tg_copy ({ fid, target, chat_id, update }) { // return task_id
const copied_files = copied ? copied.trim().split('\n').length : 0
const copied_folders = mapping ? (mapping.trim().split('\n').length - 1) : 0
let text = ` ${task_id} 复制完成\n`
let text = ` ${task_id} 完成\n`
const name = await get_folder_name(source)
text += '源文件夹' + gen_link(source, name) + '\n'
text += '目录完成数' + copied_folders + '/' + folder_count + '\n'
text += '文件完成' + copied_files + '/' + file_count + '\n'
text += '源資料夾' + gen_link(source, name) + '\n'
text += '目錄完成數' + copied_folders + '/' + folder_count + '\n'
text += '文件完成' + copied_files + '/' + file_count + '\n'
sm({ chat_id, text, parse_mode: 'HTML' })
})
.catch(err => {
if (!record) record = {}
console.error('复制失败', fid, '-->', target)
console.error('複製失敗', fid, '-->', target)
console.error(err)
sm({ chat_id, text: '复制失败,失败消息:' + err.message })
sm({ chat_id, text: '複製失敗,失敗訊息:' + err.message })
})
while (!record) {
@ -187,21 +195,21 @@ function reply_cb_query ({ id, data }) {
const url = `https://api.telegram.org/bot${tg_token}/answerCallbackQuery`
return axins.post(url, {
callback_query_id: id,
text: '开始执行 ' + data
text: '開始執行 ' + data
})
}
async function send_count ({ fid, chat_id, update }) {
const table = await gen_count_body({ fid, update, type: 'tg', service_account: true })
if (!table) return sm({ chat_id, parse_mode: 'HTML', text: gen_link(fid) + ' 信息获取失败' })
if (!table) return sm({ chat_id, parse_mode: 'HTML', text: gen_link(fid) + ' 資訊獲取失敗' })
const url = `https://api.telegram.org/bot${tg_token}/sendMessage`
const gd_link = `https://drive.google.com/drive/folders/${fid}`
const name = await get_folder_name(fid)
return axins.post(url, {
chat_id,
parse_mode: 'HTML',
text: `<pre>源文件夹名称${name}
链接${gd_link}
text: `<pre>源資料夾名稱${name}
連結${gd_link}
${table}</pre>`
}).catch(async err => {
// const description = err.response && err.response.data && err.response.data.description
@ -213,12 +221,12 @@ ${table}</pre>`
return sm({
chat_id,
parse_mode: 'HTML',
text: `链接<a href="https://drive.google.com/drive/folders/${fid}">${fid}</a>\n<pre>
表格太长超出telegram消息限制只显示概要
录名称${name}
文件总数${file_count}
录总数${folder_count}
大小${total_size}
text: `連結<a href="https://drive.google.com/drive/folders/${fid}">${fid}</a>\n<pre>
表格太長超出telegram訊息限制僅顯示概要
錄名稱${name}
文件總數${file_count}
錄總數${folder_count}
大小${total_size}
</pre>`
})
}