@@ -2,6 +2,7 @@ import fs from 'node:fs/promises';
22import path from 'node:path' ;
33import { fileURLToPath } from 'node:url' ;
44import { createHash } from 'node:crypto' ;
5+ import { createApifyClient , openPublicationStore } from '../lib/apify-client.js' ;
56import {
67 normalizePublication ,
78 mergePublications ,
5152 console . log ( 'Uma sincronização já está em andamento.' ) ;
5253 process . exit ( 0 ) ;
5354}
54- async function api ( endpoint , { method = 'GET' , body } = { } ) {
55- const response = await fetch ( `https://api.apify.com/v2/${ endpoint } ` , {
56- method,
57- headers : { Authorization : `Bearer ${ token } ` , 'Content-Type' : 'application/json' } ,
58- body : body === undefined ? undefined : JSON . stringify ( body ) ,
59- signal : AbortSignal . timeout ( 30000 ) ,
60- } ) ;
61- if ( ! response . ok ) throw new Error ( `Apify respondeu HTTP ${ response . status } .` ) ;
62- if ( response . status === 204 ) return null ;
63- return response . json ( ) ;
64- }
55+ const api = createApifyClient ( token ) ;
6556const mediaCache = new Map ( ) ;
6657async function archiveImage ( url ) {
6758 if ( ! mediaUrl ( url ) ) return null ;
@@ -108,6 +99,7 @@ async function archiveImage(url) {
10899}
109100try {
110101 const config = JSON . parse ( await fs . readFile ( path . join ( dataDir , 'apify-config.json' ) , 'utf8' ) ) ;
102+ const storeId = await openPublicationStore ( api , config ) ;
111103 const incoming = [ ] ,
112104 sources = [ ] ;
113105 for ( let index = 0 ; index < config . tasks . length ; index ++ ) {
@@ -123,9 +115,9 @@ try {
123115 // Persist the run ID remotely before waiting. Retries resume it instead of starting another extraction.
124116 const record = `WEEK-${ weekKey } -${ source . id } ` ;
125117 try {
126- run = await api ( `key-value-stores/${ config . storeId } /records/${ record } ` ) ;
118+ run = await api ( `key-value-stores/${ storeId } /records/${ record } ` ) ;
127119 } catch ( error ) {
128- if ( ! error . message . includes ( ' 404' ) ) throw error ;
120+ if ( error . status !== 404 ) throw error ;
129121 }
130122 if ( ! run ) {
131123 const runs = ( await api ( `actor-tasks/${ task . id } /runs?limit=100&desc=1` ) ) . data . items ;
@@ -146,7 +138,7 @@ try {
146138 } ,
147139 } )
148140 ) . data ;
149- await api ( `key-value-stores/${ config . storeId } /records/${ record } ` , {
141+ await api ( `key-value-stores/${ storeId } /records/${ record } ` , {
150142 method : 'PUT' ,
151143 body : { id : run . id } ,
152144 } ) ;
@@ -242,9 +234,9 @@ try {
242234 const temporary = `${ storePath } .tmp` ;
243235 await fs . writeFile ( temporary , JSON . stringify ( store , null , 2 ) ) ;
244236 await fs . rename ( temporary , storePath ) ;
245- if ( config . storeId ) {
237+ if ( storeId ) {
246238 try {
247- await api ( `key-value-stores/${ config . storeId } /records/PUBLICATIONS` , {
239+ await api ( `key-value-stores/${ storeId } /records/PUBLICATIONS` , {
248240 method : 'PUT' ,
249241 body : store ,
250242 } ) ;
0 commit comments