Skip to content

Commit

Permalink
Added sync and small improves
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicDataInfo committed Jul 3, 2020
1 parent 4ddfac4 commit 9ccc25f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GIT_REMOTE=https://github.com/group/repo.git
1 change: 0 additions & 1 deletion database/offers/test-offer copy 2.json

This file was deleted.

1 change: 0 additions & 1 deletion database/offers/test-offer copy.json

This file was deleted.

1 change: 0 additions & 1 deletion database/offers/test-offer.json

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"url": "https://kysune.me"
},
"dependencies": {
"dotenv": "^8.2.0",
"graphql-request": "1.8.2",
"simple-git": "^2.11.0"
}
Expand Down
45 changes: 34 additions & 11 deletions update.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config({ path: `${__dirname}/.env` });
const Fs = require('fs');
const SimpleGit = require('simple-git');
const { GraphQLClient } = require('graphql-request');
Expand All @@ -10,6 +11,7 @@ class Main {
this.language = 'en';
this.country = 'US';
this.namespaces = []; // You can add here non-store namespaces e.g. ue (unreal engine market offers)
this.perPage = 1000;

this.ql = new GraphQLClient('https://graphql.epicgames.com/graphql', {
headers: {
Expand Down Expand Up @@ -51,8 +53,18 @@ class Main {
baseDir: __dirname,
binary: 'git',
});
const add = git.add([`${__dirname}/database/.`]);
console.dir(add);
await git.addConfig('hub.protocol', 'https');
await git.checkoutBranch('master');
await git.add([`${__dirname}/database/.`]);
const status = await git.status();
const changesCount = status.created.length + status.modified.length + status.deleted.length + status.renamed.length;
if (changesCount === 0) return;
const commitMessage = `Update - ${Math.floor(Date.now() / 1000)}`;
await git.commit(commitMessage);
await git.removeRemote('origin');
await git.addRemote('origin', process.env.GIT_REMOTE);
await git.push(['-u', 'origin', 'master']);
console.log(`Changes has commited to repo with message ${commitMessage}`);
}

saveOffer (offer) {
Expand All @@ -61,18 +73,26 @@ class Main {
});
}

sleep (time) {
return new Promise((resolve) => {
const sto = setTimeout(() => {
clearTimeout(sto);
resolve();
}, time);
});
}

async fetchAllOffers (query, params, resultSelector) {
const elements = [];
let paging = {};
do {
const result = await this.fetchOffers(query, params, resultSelector, paging.start, paging.count);
const result = await this.fetchOffers(query, params, resultSelector, paging.start, paging.count || this.perPage);
paging = result.paging;
paging.start += paging.count;
for (let i = 0; i < result.elements.length; ++i) {
const element = result.elements[i];
this.saveOffer(element);
}
} while (paging.start - 1000 < paging.total - paging.count);
} while (paging.start - this.perPage < paging.total - paging.count);
}

async fetchOffers (query, params, resultSelector, start = 0, count = 1000) {
Expand All @@ -84,12 +104,15 @@ class Main {
});
result = resultSelector(result);
return result;
} catch (err) {
console.dir(err);
if(!err.response.data) {
console.dir(err);
if (err.response && err.response.errors) console.log(JSON.stringify(err.response.errors, null, 2));
}else data = err.response.data;
} catch (error) {
if (error.response) {
console.log(JSON.stringify(error.response, null, 2));
console.log('Next attempt in 5s...');
await this.sleep(5000);
return this.fetchOffers(...arguments);
} else {
throw new Error(error);
}
}
}
}
Expand Down

0 comments on commit 9ccc25f

Please sign in to comment.