Skip to content

Commit

Permalink
Initial upload to Google Drive
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanliang committed Jan 27, 2021
1 parent 06e8a3e commit 9b4fd8b
Show file tree
Hide file tree
Showing 546 changed files with 182,534 additions and 111 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

# DownloadBot

(Currently) 🤖 A Telegram Bot that can control your Aria2 server, control server files and also upload to OneDrive.
[![Go Version](https://img.shields.io/github/go-mod/go-version/gaowanliang/DownloadBot.svg?style=flat-square&label=Go&color=00ADD8)](https://github.com/gaowanliang/DownloadBot/blob/master/go.mod)
[![Release Version](https://img.shields.io/github/v/release/gaowanliang/DownloadBot.svg?style=flat-square&label=Release&color=1784ff)](https://github.com/gaowanliang/DownloadBot/releases/latest)
[![GitHub license](https://img.shields.io/github/license/gaowanliang/DownloadBot.svg?style=flat-square&label=License&color=2ecc71)](https://github.com/gaowanliang/DownloadBot/blob/master/LICENSE)
[![GitHub Star](https://img.shields.io/github/stars/gaowanliang/DownloadBot.svg?style=flat-square&label=Star&color=f39c12)](https://github.com/gaowanliang/DownloadBot/stargazers)
[![GitHub Fork](https://img.shields.io/github/forks/gaowanliang/DownloadBot.svg?style=flat-square&label=Fork&color=8e44ad)](https://github.com/gaowanliang/DownloadBot/network/members)


(Currently) 🤖 A Telegram Bot that can control your Aria2 server, control server files and also upload to OneDrive.
## Project significance
This project is mainly to use small hard disk server for offline downloading, for large BitTorrent files to be downloaded in sections according to the size of the hard disk, each time downloading a part, then uploading the network disk, delete and then download the other parts, until all the files are downloaded.

Expand Down Expand Up @@ -53,7 +59,7 @@ At the same time, communication via the bot protocol facilitates use on machines
- [x] Upload a file
- [x] Upload the file to OneDrive when the download is complete
- [ ] Resume from break point
- [ ] Upload the file to Google Drive when the download is complete
- [x] Upload the file to Google Drive when the download is complete
- [ ] Upload the file to Mega when the download is complete
- [ ] Upload the file to 189Cloud when the download is complete
- [ ] (When communicating via Telegram) Upload the file to Telegram when the download is complete
Expand Down
8 changes: 7 additions & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# DownloadBot

[![Go Version](https://img.shields.io/github/go-mod/go-version/gaowanliang/DownloadBot.svg?style=flat-square&label=Go&color=00ADD8)](https://github.com/gaowanliang/DownloadBot/blob/master/go.mod)
[![Release Version](https://img.shields.io/github/v/release/gaowanliang/DownloadBot.svg?style=flat-square&label=Release&color=1784ff)](https://github.com/gaowanliang/DownloadBot/releases/latest)
[![GitHub license](https://img.shields.io/github/license/gaowanliang/DownloadBot.svg?style=flat-square&label=License&color=2ecc71)](https://github.com/gaowanliang/DownloadBot/blob/master/LICENSE)
[![GitHub Star](https://img.shields.io/github/stars/gaowanliang/DownloadBot.svg?style=flat-square&label=Star&color=f39c12)](https://github.com/gaowanliang/DownloadBot/stargazers)
[![GitHub Fork](https://img.shields.io/github/forks/gaowanliang/DownloadBot.svg?style=flat-square&label=Fork&color=8e44ad)](https://github.com/gaowanliang/DownloadBot/network/members)

(目前) 🤖 一个可以控制你的Aria2服务器、控制服务器文件,同时可以上传到OneDrive的Telegram Bot。

## 意义
Expand Down Expand Up @@ -54,7 +60,7 @@
- [x] 上传文件
- [x] 下载完成后,向 OneDrive 上传文件
- [ ] 断点续传
- [ ] 下载完成后,向 Google Drive 上传文件
- [x] 下载完成后,向 Google Drive 上传文件
- [ ] 下载完成后,向 Mega 上传文件
- [ ] 下载完成后,向 天翼网盘 上传文件
- [ ] (当使用Telegram进行通信时)下载完成后,向 Telegram 上传文件
Expand Down
8 changes: 7 additions & 1 deletion README_zh-TW.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# DownloadBot

[![Go Version](https://img.shields.io/github/go-mod/go-version/gaowanliang/DownloadBot.svg?style=flat-square&label=Go&color=00ADD8)](https://github.com/gaowanliang/DownloadBot/blob/master/go.mod)
[![Release Version](https://img.shields.io/github/v/release/gaowanliang/DownloadBot.svg?style=flat-square&label=Release&color=1784ff)](https://github.com/gaowanliang/DownloadBot/releases/latest)
[![GitHub license](https://img.shields.io/github/license/gaowanliang/DownloadBot.svg?style=flat-square&label=License&color=2ecc71)](https://github.com/gaowanliang/DownloadBot/blob/master/LICENSE)
[![GitHub Star](https://img.shields.io/github/stars/gaowanliang/DownloadBot.svg?style=flat-square&label=Star&color=f39c12)](https://github.com/gaowanliang/DownloadBot/stargazers)
[![GitHub Fork](https://img.shields.io/github/forks/gaowanliang/DownloadBot.svg?style=flat-square&label=Fork&color=8e44ad)](https://github.com/gaowanliang/DownloadBot/network/members)

(目前) 🤖 一個可以控制你的Aria2伺服器、控制伺服器檔,同時可以上傳到OneDrive的Telegram Bot。

## 意義
Expand Down Expand Up @@ -53,7 +59,7 @@
- [x] 上傳文件
- [x] 下載完成後,向 OneDrive 上傳檔
- [ ] 中斷點續傳
- [ ] 下載完成後,向 Google Drive 上傳檔
- [x] 下載完成後,向 Google Drive 上傳檔
- [ ] 下載完成後,向 Mega 上傳檔
- [ ] 下載完成後,向 天翼網盤 上傳文件
- [ ] (當使用Telegram進行通信時)下載完成後,向 Telegram 上傳檔
Expand Down
80 changes: 29 additions & 51 deletions Telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,27 +393,26 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
mail := getNewOneDriveInfo(chatMsg)
text = locText("oneDriveOAuthFileCreateSuccess") + mail
}
case "googledDrive":
case "googleDrive":
switch b[1] {
case "new":
text = fmt.Sprintf(
`%s https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=ad5e65fd-856d-4356-aefc-537a9700c137&response_type=code&redirect_uri=http://localhost/onedrive-login&response_mode=query&scope=offline_access%%20User.Read%%20Files.ReadWrite.All`,
locText("oneDriveGetAccess"),
`%s %s`,
locText("googleDriveGetAccess"), getGoogleDriveAuthCodeURL(),
)
case "create":
mail := getNewOneDriveInfo(chatMsg)
text = locText("oneDriveOAuthFileCreateSuccess") + mail
mail := getNewGoogleDriveInfo(chatMsg)
text = locText("googleDriveOAuthFileCreateSuccess") + mail
}
case "odInfo":
uploadDFToOneDrive("./info/onedrive/" + b[1])
case "gdInfo":
uploadDFToGoogleDrive("./info/googleDrive/" + b[1])
default:
switch b[1] {
case "1": // 选择OneDrive
_, err := os.Stat("./info/onedrive")
if err != nil {
err = os.MkdirAll("./info/onedrive", os.ModePerm)
dropErr(err)
}
createDriveInfoFolder("./info/onedrive")

dir, _ := ioutil.ReadDir("./info/onedrive")
if len(dir) == 0 {
text = locText("noOneDriveInfo")
Expand All @@ -423,19 +422,9 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
Keyboards = append(Keyboards, inlineKeyBoardRow)
} else {
text = locText("accountsAreCurrentlyLogin")
files := make([]string, 0)
rd, err := ioutil.ReadDir("./info/onedrive/")
dropErr(err)
index := 1
for _, fi := range rd {
if !fi.IsDir() {
if strings.HasSuffix(strings.ToLower(fi.Name()), ".json") {
files = append(files, fi.Name())
text += fmt.Sprintf("%d.%s\n", index, fi.Name())
index++
}
}
}
files, _text, index := getAuthInfoJson("./info/onedrive/")
text += _text

inlineKeyBoardRow := make([]tgBotApi.InlineKeyboardButton, 0)
index = 1
for _, name := range files {
Expand All @@ -453,36 +442,22 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("createNewAcc"), "onedrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("cancel"), "upload~cancel"+":9"))
Keyboards = append(Keyboards, inlineKeyBoardRow)
text += locText("selectAccount")
}
text += locText("selectAccount")

case "2": // 选择Google drive
_, err := os.Stat("./info/googleDrive")
if err != nil {
err = os.MkdirAll("./info/googleDrive", os.ModePerm)
dropErr(err)
}
createDriveInfoFolder("./info/googleDrive")
dir, _ := ioutil.ReadDir("./info/googleDrive")
if len(dir) == 0 {
text = "没有Google Drive上传配置,是否需要新建配置"
text = locText("noGoogleDriveInfo")
inlineKeyBoardRow := make([]tgBotApi.InlineKeyboardButton, 0)
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("yes"), "googleDrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("no"), "googleDrive~cancel"+":9"))
Keyboards = append(Keyboards, inlineKeyBoardRow)
} else {
text = locText("accountsAreCurrentlyLogin")
files := make([]string, 0)
rd, err := ioutil.ReadDir("./info/googleDrive/")
dropErr(err)
index := 1
for _, fi := range rd {
if !fi.IsDir() {
if strings.HasSuffix(strings.ToLower(fi.Name()), ".json") {
files = append(files, fi.Name())
text += fmt.Sprintf("%d.%s\n", index, fi.Name())
index++
}
}
}
files, _text, index := getAuthInfoJson("./info/googleDrive/")
text += _text
inlineKeyBoardRow := make([]tgBotApi.InlineKeyboardButton, 0)
index = 1
for _, name := range files {
Expand All @@ -497,11 +472,11 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
Keyboards = append(Keyboards, inlineKeyBoardRow)
}
inlineKeyBoardRow = make([]tgBotApi.InlineKeyboardButton, 0)
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("createNewAcc"), "onedrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("createNewAcc"), "googleDrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("cancel"), "upload~cancel"+":9"))
Keyboards = append(Keyboards, inlineKeyBoardRow)
text += locText("selectAccount")
}
text += locText("selectAccount")
case "Upload":
//CopyFiles(copyFiles)
bot.Send(tgBotApi.NewDeleteMessage(myID, MessageID))
Expand Down Expand Up @@ -892,11 +867,8 @@ func tgBot(BotKey string, wg *sync.WaitGroup) {
FileControlChan <- "upload"
default:
if strings.Contains(update.Message.Text, "localhost/onedrive-login") {
_, err := os.Stat("info/onedrive")
if err != nil {
err = os.MkdirAll("info/onedrive", os.ModePerm)
dropErr(err)
}
//如果是OneDrive auth code 链接
createDriveInfoFolder("./info/onedrive")
var re = regexp.MustCompile(`(?m)code=(.*?)&`)
judgeLegal := re.FindStringSubmatch(update.Message.Text)
//log.Println(judgeLegal)
Expand All @@ -913,9 +885,15 @@ func tgBot(BotKey string, wg *sync.WaitGroup) {
go uploadFiles(update.Message.MessageID, update.Message.Text, bot)
FileControlChan <- "onedrive~create"
} else {
msg.Text = "您输入的OneDrive OAuth2链接有误,请重新输入"
msg.Text = locText("errorOneDriveAuthURL")
}

} else if strings.Contains(update.Message.Text, "4/1AY") && len(update.Message.Text) == 62 {
//如果是Google Drive auth code
createDriveInfoFolder("./info/googleDrive")
FileControlChan <- "close"
go uploadFiles(update.Message.MessageID, update.Message.Text, bot)
FileControlChan <- "googleDrive~create"
} else if !download(update.Message.Text) {
msg.Text = locText("unknownLink")
}
Expand Down
66 changes: 66 additions & 0 deletions drive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"googledrive"
"io/ioutil"
"onedrive"
"os"
"strings"
)

func createDriveInfoFolder(path string) {
_, err := os.Stat(path)
if err != nil {
err = os.MkdirAll(path, os.ModePerm)
dropErr(err)
}
}
func getAuthInfoJson(path string) ([]string, string, int) {
text := ""
files := make([]string, 0)
rd, err := ioutil.ReadDir(path)
dropErr(err)
index := 1
for _, fi := range rd {
if !fi.IsDir() {
if strings.HasSuffix(strings.ToLower(fi.Name()), ".json") {
files = append(files, fi.Name())
text += fmt.Sprintf("%d.%s\n", index, fi.Name())
index++
}
}
}
return files, text, index
}

func getNewOneDriveInfo(url string) string {
return onedrive.ApplyForNewPass(url)
}

func uploadDFToOneDrive(infoPath string) {
FileControlChan <- "close"
//log.Println(strings.ReplaceAll(info.DownloadFolder, "\\", "/"))
go onedrive.Upload(strings.ReplaceAll(infoPath, "\\", "/"), strings.ReplaceAll(info.DownloadFolder, "\\", "/"), 3, func() func(text string) {
return sendAutoUpdateMessage()
}, func(text string) string {
return locText(text)
})
}

func getGoogleDriveAuthCodeURL() string {
return googledrive.GetURL()
}
func getNewGoogleDriveInfo(code string) string {
return googledrive.CreateNewInfo(code)
}

func uploadDFToGoogleDrive(infoPath string) {
FileControlChan <- "close"
//log.Println(strings.ReplaceAll(info.DownloadFolder, "\\", "/"))
go googledrive.Upload(strings.ReplaceAll(infoPath, "\\", "/"), strings.ReplaceAll(info.DownloadFolder, "\\", "/"), func() func(text string) {
return sendAutoUpdateMessage()
}, func(text string) string {
return locText(text)
})
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ require (
golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0 // indirect
golang.org/x/text v0.3.3
onedrive v0.0.0
googledrive v0.0.0
)

replace onedrive => ./src/onedrive
replace googledrive => ./src/googledrive
Loading

0 comments on commit 9b4fd8b

Please sign in to comment.