diff --git a/cdbscreens.js b/cdbscreens.js index 15a94a6..72df3e3 100644 --- a/cdbscreens.js +++ b/cdbscreens.js @@ -30,12 +30,22 @@ async function cdbscreens() { const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/'; + await cdblib.login(); console.log('checking existing screenshots...'); const $ = await cdblib.fetch(uri); const exist = []; - $('.screenshot_list img') - .each((i, e) => exist.push($(e) - .attr('alt'))); + $('.screenshot_list a') + .each((i, e) => { + const href = $(e) + .attr('href'); + if(/screenshots\/new/.test(href)) + return; + exist.push({ + id: $('img', e) + .attr('alt'), + key: href.match(/screenshots\/(\d+)\/edit/)[1] + }); + }); let dirty; @@ -43,21 +53,20 @@ async function cdbscreens() { let spos = 0; const deletes = []; while(epos < exist.length && spos < screens.length) { - if(exist[epos] === screens[spos].id) + if(exist[epos].id === screens[spos].id) spos++; else - deletes.push(epos); + deletes.push(exist[epos].key); epos++; } while(epos < exist.length) { - deletes.push(epos); + deletes.push(exist[epos].key); epos++; } while(spos < screens.length) { dirty = true; const scr = screens[spos]; console.log(`uploading ${scr.name || scr.id}...`); - await cdblib.login(); const newuri = uri + 'screenshots/new/'; const $$ = await cdblib.fetch(newuri); const fields = cdblib.getfields($$); @@ -74,36 +83,21 @@ async function cdbscreens() { await cdblib.fetch(newuri, 'post', fields, { multipart: true }); spos++ } - - if(deletes.length) { - await cdblib.login(); - const $$ = await cdblib.fetch(uri); - const imgids = []; - $$('.screenshot_list a') - .each((i, e) => { - const href = $$(e) - .attr('href'); - if(/screenshots\/new/.test(href)) - return; - imgids.push(href.match(/screenshots\/(\d+)\/edit/)[1]) - }); - await Promise.all(deletes - .map(i => imgids[i]) - .map(async id => { - dirty = true; - console.log(`deleting screenshot ${id}...`) - const edituri = uri + 'screenshots/' + id + '/edit/'; - const $$$ = await cdblib.fetch(edituri); - const fields = cdblib.getfields($$$); - fields.delete = 'y'; - delete fields.approved; - console.log(fields); - if(config.dryrun) { - console.log('dry run; not deleting'); - console.log(util.inspect(fields)); - } else - await cdblib.fetch(edituri, 'post', fields, { multipart: true }); - })); + for(let i = 0; i < deletes.length; i++) { + const key = deletes[i]; + dirty = true; + console.log(`deleting screenshot ${key}...`) + const edituri = uri + 'screenshots/' + key + '/edit/'; + const $$ = await cdblib.fetch(edituri); + const fields = cdblib.getfields($$); + fields.delete = 'y'; + delete fields.approved; + console.log(fields); + if(config.dryrun) { + console.log('dry run; not deleting'); + console.log(util.inspect(fields)); + } else + await cdblib.fetch(edituri, 'post', fields, { multipart: true }); } console.log('screenshots synced');