import fetch from "node-fetch" ; import { open } from "sqlite" ; const sqlite3 = require ( "sqlite3" ) . verbose ( ) ; const fetchData = async ( URL ) => { const response = await fetch ( URL ) ; const data = await response . text ( ) ; return data ; } ; const fetchProposalDetailsFromURL = async ( URL ) => { let title = "" ; let contents = "" ; const db = await open ( { filename : "./wings.db" , driver : sqlite3 . Database , } ) ; await db . exec ( `
  CREATE TABLE IF NOT EXISTS proposals (
    url TEXT UNIQUE,
    title TEXT
  )` ) ; await db . exec ( `
  CREATE TABLE IF NOT EXISTS contents (
    url TEXT UNIQUE,
    contents TEXT
  )` ) ; const titleResult = await db . get ( `
  SELECT * from "proposals"
  WHERE url="${ URL }"
  ` ) ; const contentsResult = await db . get ( `
  SELECT * from "contents"
  WHERE url="${ URL }"
  ` ) ; title = titleResult && titleResult . title ; contents = contentsResult && contentsResult . contents ; let data = "" ; if ( ! title || ! contents ) { data = await fetchData ( URL ) ; } if ( ! title ) { const regex = /]:([^\n]+)/ ; const result = regex . exec ( data ) ; if ( result ) { title = result [ 1 ] . trim ( ) ; await db . exec ( `
        INSERT INTO "proposals"
        VALUES ("${ URL }","${ title }")
        ` ) ; } } if ( ! contents ) { contents = data ; await db . exec ( `
      INSERT INTO "contents"
      VALUES ("${ URL }","${ contents . replace ( /"/g , "'" ) }")
      ` ) ; } return { url : URL , title , contents } ; } ; const generateUrl = ( proposalId ) => { let URL = "https://raw.githubusercontent.com/celo-org/celo-proposals/master/CGPs/" ; const parsedProposalId = `000000${ proposalId }` . slice ( - 4 ) ; URL = `${ URL }${ parsedProposalId }.md` ; return URL ; } ; const fetchProposalDetailsFromID = async ( proposalId ) => { if ( proposalId <= 0 ) { return { title : "" , contents : "" , url : "" } ; } const URL = generateUrl ( proposalId ) ; const result = await fetchProposalDetailsFromURL ( URL ) ; return result ; } ; const getClaimObject = ( data , keyword ) => { const object = data . claims . filter ( ( claim ) => claim . type === keyword ) ; return object [ 0 ] ; } ; const fetchKeybaseData = async ( username ) => { try { let response = await fetch ( `https://keybase.io/_/api/1.0/user/lookup.json?usernames=${ username }&field=basics,proofs_summary,pictures` ) ; response = await response . json ( ) ; const basics = response . them [ 0 ] . basics ; const pictures = response . them [ 0 ] . pictures ; const proofsSummary = response . them [ 0 ] . proofs_summary ; const logoUrl = pictures . primary . url ; const socialLinks = { } ; proofsSummary . all . map ( ( proof ) => { socialLinks [ proof . proof_type ] = proof . service_url ; } ) ; return { username , socialLinks , logoUrl } ; } catch ( error ) { return undefined ; } } ; const fetchValidatorGroupDetails = async ( URL ) => { let returnData ; const db = await open ( { filename : "./wings.db" , driver : sqlite3 . Database , } ) ; await db . exec ( `
  CREATE TABLE IF NOT EXISTS validator_groups (
    url TEXT,
    data TEXT
  )
  ` ) ; const res = await db . get ( `
  SELECT * from "validator_groups"
  WHERE url="${ URL }"
  ` ) ; returnData = res && JSON . parse ( res . data ) ; if ( ! res ) { let data = await fetchData ( URL ) ; data = JSON . parse ( data ) ; const domain = getClaimObject ( data , "DOMAIN" ) . domain ; let keybaseObject = getClaimObject ( data , "KEYBASE" ) ; if ( keybaseObject ) { keybaseObject = await fetchKeybaseData ( keybaseObject . username ) ; } returnData = { domain , keybase : keybaseObject || null } ; await db . exec ( `
    INSERT into "validator_groups"
    VALUES ("${ URL }",'${ JSON . stringify ( returnData ) }')
    ` ) . catch ( err => console . log ( err ) ) ; } return returnData ; } ; export { fetchProposalDetailsFromID as fetchProposalTitleFromID , fetchValidatorGroupDetails , fetchProposalDetailsFromURL as fetchProposalTitleFromURL , } ;