Last active
May 17, 2022 10:39
-
-
Save dartmax/acd065c087b2292f42f58d01453f3f2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const yargs = require('yargs') | |
| const csv = require('csvtojson/v2') | |
| const contentful = require('contentful-management') | |
| const { config } = require('dotenv') | |
| const axois = require('axios') | |
| const { min } = require('lodash') | |
| // keep script alive | |
| setTimeout(() => {}, 99999999) | |
| config() | |
| const { argv } = yargs | |
| // const { | |
| // CONTENTFUL_ENVIRONMENT_ID, | |
| // CONTENTFUL_SPACE_ID, | |
| // CONTENTFUL_MANAGEMENT_TOKEN, | |
| // OPEN_CAGE_API_KEY | |
| // } = process.env; | |
| const wait = async (ms) => { | |
| return new Promise((resolve) => { | |
| setTimeout(resolve, ms); | |
| }); | |
| }; | |
| ;(async () => { | |
| try { | |
| // const client = contentful.createClient({ accessToken: CONTENTFUL_MANAGEMENT_TOKEN }) | |
| // const space = await client.getSpace(CONTENTFUL_SPACE_ID) | |
| // const env = await space.getEnvironment(CONTENTFUL_ENVIRONMENT_ID) | |
| const data = await csv().fromFile('Osmo_dealers_2021_new.csv') // add any csv* | |
| const length = data.length // length of amount all sttrings | |
| const sortedDealers = {}; | |
| // prepare items with all languages | |
| for (let i = 0; i < length; i += 1) { | |
| const item = data[i]; | |
| const postcode = item['PostalCode'].replace(/\s/g, ""); // save each postalCode and replace every space symbol changed to empty string (postalCode should be all different, or change to Name) | |
| const language = item['Language'].replace(/\s/g, ""); // save each language and replace every space symbol changed to empty string | |
| if (!sortedDealers[postcode]) { //if sortedDealers is empty | |
| sortedDealers[postcode] = {}; //write object | |
| } | |
| console.log('postcode', sortedDealers[postcode][language]) | |
| sortedDealers[postcode][language] = item; // write in each name like {} and language - data[i] | |
| } | |
| // process resorted dealers | |
| const postCodes = Object.keys(sortedDealers) || []; | |
| const postCodeLength = postCodes.length; | |
| console.log(`Processing ${postCodeLength} dealers`) | |
| for (let i = 0; i < postCodeLength; i += 1) { //for each postCodes do some functionality... | |
| const postCode = postCodes[i]; //write each new postCodes object in new value | |
| const dealer = sortedDealers[postCode];// crete new dealer | |
| console.log(dealer) | |
| let geometry = null; | |
| const entryData = { | |
| fields: { | |
| title: {}, | |
| address: {}, | |
| mapUrl: {}, | |
| telephone: {}, | |
| filters: {} | |
| } | |
| };// init new object with some fields | |
| // process translations | |
| const languages = Object.keys(dealer) || []; | |
| console.log('languages', languages) | |
| const languageLength = languages.length; | |
| for (let l = 0; l < languageLength; l += 1) { | |
| const language = languages[l]; // each language | |
| const localisedData = dealer[language]; | |
| const languageKey = language.toLowerCase(); | |
| entryData.fields.title[languageKey] = localisedData['Name']; | |
| entryData.fields.telephone[languageKey] = localisedData['Telephone']; | |
| entryData.fields.filters[languageKey] = localisedData['Filter'].split('|'); | |
| entryData.fields.address[languageKey] = `${localisedData['Address']}\n${localisedData['PostalCode']} ${localisedData['City']}\n${localisedData['Country']}`; | |
| const addressForGeometry = `${localisedData['Address']} ${localisedData['PostalCode']} ${localisedData['City']} ${localisedData['Country']}`; | |
| const mapUrl = `https://www.google.be/maps/place/${encodeURIComponent(addressForGeometry.replace(/'/g, "%27"))}`; | |
| geometry = `https://api.opencagedata.com/geocode/v1/json?q=${encodeURIComponent(addressForGeometry.replace(/'/g, "%27"))}`; | |
| // geometry = response.data?.results?.[0]?.geometry; | |
| entryData.fields.mapUrl[languageKey] = localisedData['MapUrl'] || mapUrl; | |
| if (geometry) { | |
| entryData.fields.location = entryData.fields.location || {}; | |
| entryData.fields.location[languageKey] = { | |
| lat: geometry, | |
| // lon: geometry?.lng | |
| }; | |
| } | |
| } | |
| console.log(entryData.fields.title); | |
| if (entryData.fields.location) { | |
| // const entry = await env.createEntry('dealer', entryData); | |
| await wait(500); | |
| // await entry.publish(); | |
| console.log(`Item number ${i + 1}. created and published`); | |
| } else { | |
| console.log(`Item number skipped ${i + 1}. ${entryData?.fields?.title} no geometry`); | |
| } | |
| } | |
| console.log('Done') | |
| process.exit(0) | |
| } catch (err) { | |
| console.error(err) | |
| process.exit(1) | |
| } | |
| })() | |
| ________________________________CSV example________________________________ | |
| Name,Address,addressForGeometry,PostalCode,City,Country,Filter,Language | |
| GENKERPLANTENCENTRUM ,HENGELHOEFSTRAAT 118,HENGELHOEFSTRAAT 118,3600,GENK,België,Bodemverbetering|Meststof,NL | |
| LES SERRES DE LASSIGNY ,5 ZONE ARTISANALE,5 ZONE ARTISANALE,60310,LASSIGNY,France,Amendement de sol|Couvre-sols|Terreau,FR | |
| Apdeco Arabians,Derrevoortstraat 61,1760,Roosdaal,Belgium,0475/462639,https://goo.gl/maps/os2WzBP1bJaCXa3D7,Belgium,EN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment