-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sam bacha
authored
Mar 19, 2021
1 parent
08928a3
commit c62fcf6
Showing
5 changed files
with
91 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
node_modules | ||
*.tar.gz | ||
*.zip | ||
*.log | ||
dist/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,98 @@ | ||
import * as fs from "fs" | ||
import { YearnInfo, YearnList, schema as yearn_schema } from "@yfi/tokenlist"; | ||
import { TokenInfo, TokenList, schema as token_schema } from "@uniswap/token-lists"; | ||
// @file graphql scrapper | ||
import * as fs from 'fs'; | ||
import { YearnInfo, YearnList, schema as yearn_schema } from 'yearn-tokenlist'; | ||
import { | ||
TokenInfo, | ||
TokenList, | ||
schema as token_schema, | ||
} from '@uniswap/token-lists'; | ||
const cliProgress = require('cli-progress'); | ||
const Ajv = require("ajv") | ||
const Ajv = require('ajv'); | ||
const ogs = require('open-graph-scraper'); | ||
|
||
// Progress bar | ||
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic); | ||
const progressBar = new cliProgress.SingleBar( | ||
{}, | ||
cliProgress.Presets.shades_classic, | ||
); | ||
|
||
const main = async () => { | ||
|
||
// Get all token list files | ||
let folders = fs.readdirSync('../index/').filter(folder => !folder.includes('.')) | ||
let files: string[] = folders.reduce((files: string[], folder:string) => { | ||
const currentFiles: string[] = fs.readdirSync('../index/' + folder) | ||
currentFiles.forEach(file => { | ||
files.push('../index/' + folder + '/' + file) | ||
}) | ||
return files | ||
}, []) | ||
let folders = fs | ||
.readdirSync('../index/') | ||
.filter((folder) => !folder.includes('.')); | ||
let files: string[] = folders.reduce((files: string[], folder: string) => { | ||
const currentFiles: string[] = fs.readdirSync('../index/' + folder); | ||
currentFiles.forEach((file) => { | ||
files.push('../index/' + folder + '/' + file); | ||
}); | ||
return files; | ||
}, []); | ||
|
||
// Iterate over all lists | ||
for (let i=0; i < files.length; i++) { | ||
const f: string = files[i] | ||
console.log('Images for ' + f) | ||
const list_path = f | ||
for (let i = 0; i < files.length; i++) { | ||
const f: string = files[i]; | ||
console.log('Images for ' + f); | ||
const list_path = f; | ||
|
||
// Load list | ||
let list = f.includes('erc20') ? require('../' + list_path) as TokenList : require('../' + list_path) as YearnList | ||
progressBar.start(list.tokens.length, 0) | ||
for (let j = 0; j < list.tokens.length; j++ ) { | ||
const t: YearnInfo | TokenInfo = list.tokens[j] | ||
let list = f.includes('erc20') | ||
? (require('../' + list_path) as TokenList) | ||
: (require('../' + list_path) as YearnList); | ||
progressBar.start(list.tokens.length, 0); | ||
for (let j = 0; j < list.tokens.length; j++) { | ||
const t: YearnInfo | TokenInfo = list.tokens[j]; | ||
//@ts-ignore | ||
if (t.extensions.link) { | ||
try{ | ||
try { | ||
//@ts-ignore | ||
const graph = await ogs({'url': t.extensions.link, 'timeout' : 5000}) | ||
const image = graph.result ? (await graph.result).ogImage : null | ||
const graph = await ogs({ url: t.extensions.link, timeout: 5000 }); | ||
const image = graph.result ? (await graph.result).ogImage : null; | ||
//@ts-ignore | ||
list.tokens[j].extensions.ogImage = image ? (image.url ? image.url : null) : null | ||
list.tokens[j].extensions.ogImage = image | ||
? image.url | ||
? image.url | ||
: null | ||
: null; | ||
} catch (e) { | ||
//@ts-ignore | ||
list.tokens[j].extensions.ogImage = null | ||
list.tokens[j].extensions.ogImage = null; | ||
} | ||
} else { | ||
//@ts-ignore | ||
list.tokens[j].extensions.ogImage = null | ||
list.tokens[j].extensions.ogImage = null; | ||
} | ||
progressBar.update(j+1) | ||
progressBar.update(j + 1); | ||
} | ||
progressBar.stop() | ||
progressBar.stop(); | ||
|
||
// Validate list against schema | ||
const ajv = new Ajv() | ||
const validateList = ajv.compile(f.includes('erc20') ? token_schema : yearn_schema) | ||
|
||
const ajv = new Ajv(); | ||
const validateList = ajv.compile( | ||
f.includes('erc20') ? token_schema : yearn_schema, | ||
); | ||
|
||
if (!validateList(list)) { | ||
console.log("New list has invalid schema: ") | ||
console.log(validateList.errors) | ||
console.log('New list has invalid schema: '); | ||
console.log(validateList.errors); | ||
//throw Error("^^^") | ||
} | ||
|
||
fs.writeFile( | ||
list_path, | ||
JSON.stringify(list), | ||
{ flag: "w+" }, | ||
{ flag: 'w+' }, | ||
function (err) { | ||
if (err) throw err | ||
} | ||
) | ||
if (err) throw err; | ||
}, | ||
); | ||
} | ||
|
||
} | ||
}; | ||
|
||
main() | ||
.then(() => { | ||
console.log("Finished") | ||
console.log('Finished'); | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
|
||
console.error(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import * as dotenv from 'dotenv' | ||
import * as path from 'path' | ||
// @file utils | ||
import * as dotenv from 'dotenv'; | ||
import * as path from 'path'; | ||
|
||
export const getEnvConfig = () => { | ||
const envFile = path.resolve(__dirname, '../config/creds.env') | ||
const envLoad = dotenv.config({ path: envFile }) | ||
const envFile = path.resolve(__dirname, '../config/creds.env'); | ||
const envLoad = dotenv.config({ path: envFile }); | ||
|
||
if (envLoad.error) { | ||
throw new Error(envLoad.error.message) | ||
throw new Error(envLoad.error.message); | ||
} | ||
|
||
return envLoad.parsed || {} | ||
} | ||
return envLoad.parsed || {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters