This repository was archived by the owner on May 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
190 lines (187 loc) · 5.79 KB
/
api.js
File metadata and controls
190 lines (187 loc) · 5.79 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* api.js
*
* Module for communicating with Fandom's API.
*/
/**
* Importing modules.
*/
// eslint-disable-next-line
import 'babel-polyfill';
import FormData from 'form-data';
import cookie from 'tough-cookie';
import cookieJarSupport from 'axios-cookiejar-support';
import http from 'axios';
import querystring from 'querystring';
/**
* Setup HTTP agent.
*/
cookieJarSupport(http);
/**
*
*/
export default class UploaderApi {
/**
* Class constructor.
* @param {Wiki} wikiFrom Wiki from which to list images
* @param {Wiki} wikiTo Wiki to which the images should be uploaded
*/
constructor(wikiFrom, wikiTo) {
this._from = wikiFrom;
this._to = wikiTo;
http.defaults.headers.common['User-Agent'] =
'Fandom image transfer client';
http.defaults.jar = new cookie.CookieJar();
http.defaults.withCredentials = true;
}
/**
* Logs in to Fandom.
* @param {string} username Fandom username
* @param {string} password Fandom password
* @returns {Promise} Promise that resolves when the user logs in
*/
login(username, password) {
return http.post(
`https://services.${this._to.domain}/auth/token`,
querystring.stringify({
password,
username
})
);
}
/**
* Gets the current user's edit token.
* @returns {Promise} Promise that resolves with the user's edit token
*/
token() {
return http.get(this._to.apiUrl, {
params: {
action: 'query',
format: 'json',
intoken: 'edit',
meta: 'tokens',
prop: 'info',
titles: '#',
type: 'csrf'
},
transformResponse: [
function(d) {
const data = JSON.parse(d);
return data.query.pages ?
data.query.pages[-1].edittoken :
data.query.tokens.csrftoken;
}
]
});
}
/**
* Lists all images on a certain wiki, along with their URLs.
* @returns {Array<Image>} List of found image objects
*/
async listAll() {
const images = [];
let gapfrom = null;
// eslint-disable-next-line no-constant-condition
while (true) {
const response = await http.get(this._from.apiUrl, {
params: {
action: 'query',
format: 'json',
gapfrom,
gaplimit: 'max',
gapnamespace: 6,
generator: 'allpages',
iiprop: 'url',
prop: 'imageinfo'
}
});
const {pages} = response.data.query,
cont = response.data['query-continue'];
for (const page in pages) {
const p = pages[page];
if (p.imageinfo) {
images.push({
title: p.title,
url: p.imageinfo[0].url
});
}
}
if (cont) {
({gapfrom} = cont.allpages.gapfrom);
} else {
break;
}
}
return images;
}
/**
* Finds URLs to specified images on a certain wiki.
* @param {Array<string>} imageList Images whose URLs should be found
* @returns {Array<Image>} List of found image objects
*/
async listSome(imageList) {
const images = imageList.slice(0),
result = [];
while (images.length) {
const batch = images.splice(0, 50),
response = await http.get(this._from.apiUrl, {
params: {
'action': 'query',
'format': 'json',
'iiprop': 'url',
'prop': 'imageinfo',
'titles': batch.join('|'),
// Weird redirection bug.
/* eslint-disable-next-line sort-keys */
'*': '0'
}
}), {pages} = response.data.query;
for (const page in pages) {
const p = pages[page];
if (p.imageinfo) {
result.push({
title: p.title,
url: p.imageinfo[0].url
});
}
}
}
return result;
}
/**
* Uploads an image via the MediaWiki API.
* @param {object} image Object representing an image returned from list()
* @param {string} image.title Filename of the image to upload
* @param {string} image.url URL of the image to upload
* @param {string} comment Comment used when uploading the file
* @returns {Promise} Promise that resolves when the upload finishes
*/
async upload(image, {
comment, ignoreWarnings, text, watchlist
}) {
const token = (await this.token()).data;
const form = new FormData();
form.append('action', 'upload');
form.append('filename', image.title);
form.append('format', 'json');
form.append('token', token);
if (comment) {
form.append('comment', comment);
}
if (ignoreWarnings) {
form.append('ignorewarnings', '1');
}
if (text) {
form.append('text', text);
}
if (watchlist) {
form.append('watchlist', watchlist);
}
form.append('file', (await http.get(`${image.url}&format=original`, {
responseType: 'stream'
})).data);
return http.post(this._to.apiUrl, form, {
headers: form.getHeaders()
});
}
}