Wanikani Open Framework - Apiv2 module

Apiv2 module for Wanikani Open Framework

目前为 2018-02-17 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/38581/252065/Wanikani%20Open%20Framework%20-%20Apiv2%20module.js

  1. // ==UserScript==
  2. // @name Wanikani Open Framework - Apiv2 module
  3. // @namespace rfindley
  4. // @description Apiv2 module for Wanikani Open Framework
  5. // @version 1.0.0
  6. // @copyright 2018+, Robin Findley
  7. // @license MIT; http://opensource.org/licenses/MIT
  8. // ==/UserScript==
  9.  
  10. (function(global) {
  11.  
  12. //########################################################################
  13. //------------------------------
  14. // Published interface.
  15. //------------------------------
  16. global.wkof.Apiv2 = {
  17. clear_cache: clear_cache, // Clear the user cache
  18. fetch_endpoint: fetch_endpoint, // Fetch a complete API endpoint, including pagination
  19. get_endpoint: get_endpoint, // Scripts can signal which API endpoints they need
  20. is_valid_apikey_format: is_valid_apikey_format, // Check if string is a valid API key
  21. };
  22. //########################################################################
  23.  
  24. function promise(){var a,b,c=new Promise(function(d,e){a=d;b=e;});c.resolve=a;c.reject=b;return c;}
  25.  
  26. var available_endpoints = [
  27. 'assignments','level_progressions','resets','review_statistics',
  28. 'reviews','study_materials','subjects','summary','user'
  29. ];
  30. var using_apikey_override = false;
  31. var skip_username_check = false;
  32.  
  33. //------------------------------
  34. // Retrieve the username from the page.
  35. //------------------------------
  36. function get_username() {
  37. try {
  38. return ($('.account a[href^="/users/"]').attr('href') || '').match(/[^\/]+$/)[0];
  39. } catch(e) {
  40. return undefined;
  41. }
  42. }
  43.  
  44. //------------------------------
  45. // Check if a string is a valid apikey format.
  46. //------------------------------
  47. function is_valid_apikey_format(str) {
  48. return ((typeof str === 'string') &&
  49. (str.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) !== null));
  50. }
  51.  
  52. //------------------------------
  53. // Clear any datapoint cache not belonging to the current user.
  54. //------------------------------
  55. function clear_cache() {
  56. var clear_promises = [];
  57. var dir = wkof.file_cache.dir;
  58. for (var idx in available_endpoints) {
  59. var filename = 'Apiv2.'+available_endpoints[idx];
  60. if (filename === 'Apiv2.subjects' || !dir[filename]) continue;
  61. clear_promises.push(filename);
  62. }
  63. clear_promises = clear_promises.map(delete_file);
  64.  
  65. if (clear_promises.length > 0) {
  66. console.log('Clearing user cache...');
  67. return Promise.all(clear_promises);
  68. } else {
  69. return Promise.resolve();
  70. }
  71.  
  72. function delete_file(filename){
  73. return wkof.file_cache.delete(filename);
  74. }
  75. }
  76.  
  77. wkof.set_state('wkof.Apiv2.key', 'not_ready');
  78.  
  79. //------------------------------
  80. // Get the API key (either from localStorage, or from the Account page).
  81. //------------------------------
  82. function get_apikey() {
  83. // If we already have the apikey, just return it.
  84. if (is_valid_apikey_format(wkof.Apiv2.key))
  85. return Promise.resolve(wkof.Apiv2.key);
  86.  
  87. // If we don't have the apikey, but override was requested, return error.
  88. if (using_apikey_override)
  89. return Promise.reject('Invalid api2_key_override in localStorage!');
  90.  
  91. // Fetch the apikey from the account page.
  92. console.log('Fetching API key...');
  93. wkof.set_state('wkof.Apiv2.key', 'fetching');
  94. return wkof.load_file('https://www.wanikani.com/settings/account')
  95. .then(parse_page);
  96.  
  97. function parse_page(page){
  98. var page = $(page);
  99. var apikey = page.find('#user_api_key_v2').val();
  100. if (!wkof.Apiv2.is_valid_apikey_format(apikey))
  101. return Promise.reject('No API key (version 2) found on account page!');
  102.  
  103. // Store the api key.
  104. wkof.Apiv2.key = apikey;
  105. localStorage.setItem('apiv2_key', apikey);
  106. wkof.set_state('wkof.Apiv2.key', 'ready');
  107. return apikey;
  108. };
  109. }
  110.  
  111. //------------------------------
  112. // Fetch a URL asynchronously, and pass the result as resolved Promise data.
  113. //------------------------------
  114. function fetch_endpoint(endpoint, options) {
  115. var retry_cnt, endpoint_data, url, headers;
  116. var progress_data = {name:'wk_api_'+endpoint, label:'Wanikani '+endpoint, value:0, max:100};
  117. var bad_key_cnt = 0;
  118.  
  119. // Parse options.
  120. if (!options) options = {};
  121. var filters = options.filters;
  122. if (!filters) filters = {};
  123. var progress_callback = options.progress_callback;
  124.  
  125. // Get timestamp of last fetch from options (if specified)
  126. var last_update = options.last_update;
  127.  
  128. // If no prior fetch, set last_update to ancient date.
  129. if (typeof last_update !== 'string') {
  130. if (filters.updated_after === undefined)
  131. last_update = '1999-01-01T01:00:00.000000Z';
  132. else
  133. last_update = filters.updated_after;
  134. }
  135.  
  136. // Set up URL and headers
  137. url = "https://www.wanikani.com/api/v2/" + endpoint;
  138.  
  139. // Add user-specified data filters to the URL
  140. filters.updated_after = last_update;
  141. var arr = [];
  142. for (var name in filters) {
  143. var value = filters[name];
  144. if (Array.isArray(value)) value = value.join(',');
  145. arr.push(name+'='+value);
  146. }
  147. url += '?'+arr.join('&');
  148.  
  149. // Get API key and fetch the data.
  150. var fetch_promise = promise();
  151. get_apikey()
  152. .then(setup_and_fetch);
  153.  
  154. return fetch_promise;
  155.  
  156. //============
  157. function setup_and_fetch() {
  158. wkof.Progress.update(progress_data);
  159. headers = {
  160. // 'Wanikani-Revision': '20170710', // Placeholder?
  161. 'Authorization': 'Bearer '+wkof.Apiv2.key,
  162. };
  163. headers['If-Modified-Since'] = new Date(last_update).toUTCString(last_update);
  164.  
  165. retry_cnt = 0;
  166. fetch();
  167. }
  168.  
  169. //============
  170. function fetch() {
  171. retry_cnt++;
  172. var request = new XMLHttpRequest();
  173. request.onreadystatechange = received;
  174. request.open('GET', url, true);
  175. for (var key in headers)
  176. request.setRequestHeader(key, headers[key]);
  177. request.send();
  178. }
  179.  
  180. //============
  181. function received(event) {
  182. // ReadyState of 4 means transaction is complete.
  183. if (this.readyState !== 4) return;
  184.  
  185. // Check for rate-limit error. Delay and retry if necessary.
  186. if (this.status === 429 && retry_cnt < 40) {
  187. var delay = Math.min((retry_cnt * 250), 2000);
  188. setTimeout(fetch, delay);
  189. return;
  190. }
  191.  
  192. // Check for bad API key.
  193. if (this.status === 401) return bad_apikey();
  194.  
  195. // Check of 'no updates'.
  196. if (this.status >= 300) return fetch_promise.reject({status:this.status, url:url});
  197.  
  198. // Process the response data.
  199. var json = JSON.parse(event.target.response);
  200.  
  201. // Data may be a single object, or collection of objects.
  202. // Collections are paginated, so we may need more fetches.
  203. if (json.object === 'collection') {
  204. // It's a multi-page endpoint.
  205. var first_new, so_far, total;
  206. if (endpoint_data === undefined) {
  207. // First page of results.
  208. first_new = 0;
  209. so_far = json.data.length;
  210. } else {
  211. // Nth page of results.
  212. first_new = endpoint_data.data.length;
  213. so_far = first_new + json.data.length;
  214. json.data = endpoint_data.data.concat(json.data);
  215. }
  216. endpoint_data = json;
  217. total = json.total_count;
  218.  
  219. // Call the 'progress' callback.
  220. if (typeof progress_callback === 'function')
  221. progress_callback(endpoint, first_new, so_far, total);
  222. progress_data.value = so_far;
  223. progress_data.max = total;
  224. wkof.Progress.update(progress_data);
  225.  
  226. // If there are more pages, fetch the next one.
  227. if (json.pages.next_url !== null) {
  228. retry_cnt = 0;
  229. url = json.pages.next_url;
  230. fetch();
  231. return;
  232. }
  233.  
  234. // This was the last page. Return the data.
  235. fetch_promise.resolve(endpoint_data);
  236.  
  237. } else {
  238. // Single-page result. Report single-page progress, and return data.
  239. if (typeof progress_callback === 'function')
  240. progress_callback(endpoint, 0, 1, 1);
  241. progress_data.value = 1;
  242. progress_data.max = 1;
  243. wkof.Progress.update(progress_data);
  244. fetch_promise.resolve(json);
  245. }
  246. }
  247.  
  248. //============
  249. function bad_apikey(){
  250. // If we are using an override key, abort and return error.
  251. if (using_apikey_override) {
  252. fetch_promise.reject('Wanikani doesn\'t recognize the apiv2_key_override key ("'+wkof.Apiv2.key+'")');
  253. return;
  254. }
  255.  
  256. // If bad key received too many times, abort and return error.
  257. bad_key_cnt++;
  258. if (bad_key_cnt > 1) {
  259. fetch_promise.reject('Aborting fetch: Bad key reported multiple times!');
  260. return;
  261. }
  262.  
  263. // We received a bad key. Report on the console, then try fetching the key (and data) again.
  264. console.log('Seems we have a bad API key. Erasing stored info.');
  265. localStorage.removeItem('apiv2_key');
  266. wkof.Apiv2.key = undefined;
  267. get_apikey()
  268. .then(populate_user_cache)
  269. .then(setup_and_fetch);
  270. }
  271. }
  272.  
  273.  
  274. var min_update_interval = 60;
  275. var ep_cache = {};
  276.  
  277. //------------------------------
  278. // Get endpoint data from cache with updates from API.
  279. //------------------------------
  280. function get_endpoint(ep_name, options) {
  281. if (!options) options = {};
  282.  
  283. // We cache data for 'min_update_interval' seconds.
  284. // If within that interval, we return the cached data.
  285. // User can override cache via "options.force_update = true"
  286. var ep_info = ep_cache[ep_name];
  287. if (ep_info) {
  288. // If still awaiting prior fetch return pending promise.
  289. // Also, not force_update, return non-expired cache (i.e. resolved promise)
  290. if (options.force_update !== true || ep_info.timer === undefined)
  291. return ep_info.promise;
  292. // User is requesting force_update, and we have unexpired cache.
  293. // Clear the expiration timer since we will re-fetch anyway.
  294. clearTimeout(ep_info.timer);
  295. }
  296.  
  297. // Create a promise to fetch data. The resolved promise will also serve as cache.
  298. var get_promise = promise();
  299. ep_cache[ep_name] = {promise: get_promise};
  300.  
  301. // Make sure the requested endpoint is valid.
  302. var merged_data;
  303. if (available_endpoints.indexOf(ep_name) < 0) {
  304. get_promise.reject(new Error('Invalid endpoint name "'+ep_name+'"'));
  305. return get_promise;
  306. }
  307.  
  308. // Perform the fetch, and process the data.
  309. wkof.file_cache.load('Apiv2.'+ep_name)
  310. .then(fetch, fetch);
  311. return get_promise;
  312.  
  313. //============
  314. function fetch(cache_data) {
  315. if (typeof cache_data === 'string') cache_data = {last_update:null};
  316. merged_data = cache_data;
  317. var fetch_options = Object.assign({}, options);
  318. fetch_options.last_update = cache_data.last_update;
  319. fetch_endpoint(ep_name, fetch_options)
  320. .then(process_api_data, handle_error);
  321. }
  322.  
  323. //============
  324. function process_api_data(fetched_data) {
  325. // Mark the data with the last_update timestamp reported by the server.
  326. if (fetched_data.data_updated_at !== null) merged_data.last_update = fetched_data.data_updated_at;
  327.  
  328. // Process data according to whether it is paginated or not.
  329. if (fetched_data.object === 'collection') {
  330. if (merged_data.data === undefined) merged_data.data = {};
  331. for (var idx = 0; idx < fetched_data.data.length; idx++) {
  332. var item = fetched_data.data[idx];
  333. merged_data.data[item.id] = item;
  334. }
  335. } else {
  336. merged_data.data = fetched_data.data;
  337. }
  338.  
  339. // If it's the 'user' endpoint, we insert the apikey before caching.
  340. if (ep_name === 'user') merged_data.data.apikey = wkof.Apiv2.key;
  341.  
  342. // Save data to cache and finish up.
  343. wkof.file_cache.save('Apiv2.'+ep_name, merged_data)
  344. .then(finish);
  345. }
  346.  
  347. //============
  348. function finish() {
  349. // Return the data, then set up a cache expiration timer.
  350. get_promise.resolve(merged_data.data);
  351. ep_cache[ep_name].timer = setTimeout(expire_cache, min_update_interval*1000);
  352. }
  353.  
  354. //============
  355. function expire_cache() {
  356. // Delete the data from cache.
  357. delete ep_cache[ep_name];
  358. }
  359.  
  360. //============
  361. function handle_error(error) {
  362. if (typeof error === 'string')
  363. get_promise.reject(error);
  364. if (error.status >= 300 && error.status <= 399)
  365. finish();
  366. else
  367. get_promise.reject('Error '+error.status+' fetching "'+error.url+'"');
  368. }
  369. }
  370.  
  371. //########################################################################
  372. //------------------------------
  373. // Make sure user cache matches the current (or override) user.
  374. //------------------------------
  375. function validate_user_cache() {
  376. var user = get_username();
  377. if (!user) {
  378. // Username unavailable if not logged in, or if on Lessons or Reviews pages.
  379. // If not logged in, stop running the framework.
  380. if (location.pathname.match(/^(\/|\/login)$/) !== null)
  381. return Promise.reject('Couldn\'t extract username from user menu! Not logged in?');
  382. skip_username_check = true;
  383. }
  384.  
  385. var apikey = localStorage.getItem('apiv2_key_override');
  386. if (apikey !== null) {
  387. // It looks like we're trying to override the apikey (e.g. for debug)
  388. using_apikey_override = true;
  389. if (!is_valid_apikey_format(apikey)) {
  390. return Promise.reject('Invalid api2_key_override in localStorage!');
  391. }
  392. console.log('Using apiv2_key_override key ('+apikey+')');
  393. } else {
  394. // Use regular apikey (versus override apikey)
  395. apikey = localStorage.getItem('apiv2_key');
  396. if (!is_valid_apikey_format(apikey)) apikey = undefined;
  397. }
  398.  
  399. wkof.Apiv2.key = apikey;
  400.  
  401. // Make sure cache is still valid
  402. return wkof.file_cache.load('Apiv2.user')
  403. .then(process_user_info)
  404. .catch(retry);
  405.  
  406. //============
  407. function process_user_info(user_info) {
  408. // If cache matches, we're done.
  409. if (user_info.data.apikey === wkof.Apiv2.key) {
  410. // We don't check username when using override key.
  411. if (using_apikey_override || skip_username_check || (user_info.data.username === user)) {
  412. wkof.Apiv2.user = user_info.data.username;
  413. wkof.user = user_info.data;
  414. return;
  415. }
  416. }
  417. // Cache doesn't match.
  418. if (!using_apikey_override) {
  419. // Fetch the key from the accounts page.
  420. wkof.Apiv2.key = undefined;
  421. throw 'fetch key';
  422. } else {
  423. // We're using override. No need to fetch key, just populate cache.
  424. return clear_cache().then(populate_user_cache);
  425. }
  426. }
  427.  
  428. //============
  429. function retry() {
  430. // Either empty cache, or user mismatch. Fetch key, then populate cache.
  431. return get_apikey().then(clear_cache).then(populate_user_cache);
  432. }
  433. }
  434.  
  435. //------------------------------
  436. // Populate the user info into cache.
  437. //------------------------------
  438. function populate_user_cache() {
  439. console.log('Fetching user info...');
  440. return fetch_endpoint('user')
  441. .then(function(user_info){
  442. // Store the apikey in the cache.
  443. user_info.data.apikey = wkof.Apiv2.key;
  444. wkof.Apiv2.user = user_info.data.username;
  445. wkof.user = user_info.data
  446. console.log('Caching user info...');
  447. return wkof.file_cache.save('Apiv2.user', user_info);
  448. })
  449. }
  450.  
  451. //------------------------------
  452. // Do initialization once document is loaded.
  453. //------------------------------
  454. function notify_ready() {
  455. // Notify listeners that we are ready.
  456. // Delay guarantees include() callbacks are called before ready() callbacks.
  457. setTimeout(function(){wkof.set_state('wkof.Apiv2', 'ready');},0);
  458. }
  459.  
  460. //------------------------------
  461. // Do initialization once document is loaded.
  462. //------------------------------
  463. wkof.include('Progress');
  464. wkof.ready('document,Progress').then(startup);
  465. function startup() {
  466. validate_user_cache()
  467. .then(notify_ready);
  468. }
  469.  
  470. })(window);
  471.