-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
148 lines (125 loc) · 3.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict'
const { google } = require('googleapis')
const client = require('./oath')
const download = require('image-downloader');
const nhentai_api = require('./nhentai_api')
//USE TO REMOVE FOLDER WITH FILES INSIDE
var rimraf = require("rimraf");
//
const fs = require('fs');
//
const drive = google.drive({
version: 'v3',
auth: client.oAuth2Client,
});
async function runSample(query) {
const params = { pageSize: 10 };
params.q = query;
const res = await drive.files.list(params);
console.log(res.data);
return res.data;
}
async function createDriveFolder(folderName) {
return new Promise((resolve, reject) => {
var fileMetadata = {
'name': folderName,
'mimeType': 'application/vnd.google-apps.folder'
};
drive.files.create({
resource: fileMetadata,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
// console.error(err);
reject(err)
} else {
console.log('Folder Id: ', file.data.id);
resolve(file.data.id)
}
});
})
}
async function downloadFileFromUrl(url, dest) {
try {
const { filename, image } = await download.image({
url: url,
dest: dest,
})
return filename // => /path/to/dest/image.jpg
} catch (e) {
console.error(e)
}
}
async function uploadFileToDrive(path, folderName, folderContainsId) {
// const fileSize = fs.statSync(path).size;
return await drive.files.create(
{
resource: {
'name': path.split(folderName + '/')[1],
parents: [folderContainsId]
},
media: {
mimeType: 'image/jpeg',
body: fs.createReadStream(path),
},
},
// {
// // Use the `onUploadProgress` event from Axios to track the
// // number of bytes uploaded to this point.
// onUploadProgress: evt => {
// const progress = (evt.bytesRead / fileSize) * 100;
// readline.clearLine();
// readline.cursorTo(0);
// process.stdout.write(`${Math.round(progress)}% complete \n`);
// },
// }
);
}
async function uploadFile(urls, folderName = 'tmp_images') {
folderName = folderName.replace('.', ' ')
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName)
}
let folderId = await createDriveFolder(folderName)
console.log('UPLOADING ...')
return Promise.all(urls.map(async url => {
let path = await downloadFileFromUrl(url, './' + folderName)
await uploadFileToDrive(path, folderName, folderId)
})).then(() => {
console.log(folderName + " is uploaded")
//REMOVE TEMP FOLDER FOR IMAGES
rimraf.sync(folderName)
})
}
if (module === require.main) {
}
async function Save(urls, callback) {
//let urls = ['https://nhentai.net/g/267195/','https://nhentai.net/g/267191/']
const scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.file'];
await client.authenticate(scopes, false)
urls.map((url) => {
nhentai_api.GetImageUrls(url)
.then((data) => {
if (data.status === 200) {
// sampleClient
// .authenticate(scopes, false)
uploadFile(data.imageUrls.slice(0,3), data.title)
.catch(async (err) => {
// //INVALID TOKEN OR EXPIRED TOKEN
if (err.code === 401 || err.code === 400){
// console.log('Token updated,please run again')
await client.authenticate(scopes)
Save(urls, callback)
}else
callback(err)
})
.then(() => callback());
}
})
})
}
module.exports = {
Save,
client: client.oAuth2Client,
};