Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Add postprocessor and generator for contacts GH-23
Browse files Browse the repository at this point in the history
#contacts
  • Loading branch information
mrbrianevans committed Feb 2, 2022
1 parent 106589f commit 396a7c3
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 19 deletions.
11 changes: 11 additions & 0 deletions demo-files/contacts/contacts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Family Name,Given Name,Group Membership,Organization 1 - Name,Organization 1 - Type,Phone 1 - Type,Phone 1 - Value,Name
Turner,Matthew,* myContacts,,,Mobile,04841 794049,Matthew Turner
Lewis,Brian,* myContacts,,,Mobile,02794 678454,Brian Lewis
Taylor,Michael,* myContacts,,,Mobile,05380 178879,Michael Taylor
Smith,Alan,* myContacts,,,Mobile,04623 971739,Alan Smith
John Robinson,David,* myContacts,,,Mobile,03749 234774,David John Robinson
Morris,Andrew,* myContacts,,,Mobile,08458 206820,Andrew Morris
Hill,Brian,* myContacts,,,Mobile,04874 833649,Brian Hill
Ward,Paul,* myContacts,,,Mobile,09730 103706,Paul Ward
Taylor,John,* myContacts,,,Mobile,06671 694507,John Taylor
Scott,Mark,* myContacts,,,Mobile,08304 579657,Mark Scott
12 changes: 11 additions & 1 deletion lib-testing/generateDemoFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import { generateInstagramComments } from '../lib/vendors/instagram/Comments'
import { generateInstagramLikes } from '../lib/vendors/instagram/Likes'
import { generateInstagramAccountHistory } from '../lib/vendors/instagram/AccountHistory'
import { generateInstagramInformationAboutYou } from '../lib/vendors/instagram/InformationAboutYou'
import { generateContactsFile } from 'social-media-export-analyser-lib/vendors/google/Contacts'

const demoFilePath = (filename) => {
const filePath = path.resolve('..', 'demo-files', filename)
fs.mkdirSync(path.dirname(filePath), { recursive: true })
return filePath
}

const demoFilePath = (filename) => path.resolve('..', 'demo-files', filename)
export const generateDemoFiles = async () => {
// instagram media.json (list of posts)
const instagramMedia = generateSampleMediaData({ photos: 5 })
Expand Down Expand Up @@ -81,6 +87,10 @@ export const generateDemoFiles = async () => {
//twitter
const twitterTweets = generateTwitterTweetFile({ qty: 15 })
fs.writeFileSync(demoFilePath('twitter/tweet.js'), twitterTweets)

//contacts
const contactsCsv = generateContactsFile()
fs.writeFileSync(demoFilePath('contacts/contacts.csv'), contactsCsv)
}

generateDemoFiles()
7 changes: 4 additions & 3 deletions lib-testing/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
"composite": true,
"rootDir": "./",
"sourceMap": true,
"allowJs": false
},
"exclude": ["node_modules"],
"composite": true,
"rootDir": "./",
"references": [
{
"path": "../lib"
Expand Down
23 changes: 22 additions & 1 deletion lib/common/RandomUtils/RandomNumberUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// inclusive of max and min
export const RandInt = (min: number, max: number) => {
const RandIntMinMax = (min: number, max: number) => {
return Math.round(Math.random() * (max - min) + min)
}

Expand All @@ -9,3 +9,24 @@ export const RandHex = (digits: number): string => {
const int = RandInt(min, max)
return int.toString(16)
}

const RandIntLen = (length: number) => {
return RandIntMinMax(10 ** (length - 1), 10 ** length - 1)
}

/**
* Return a random integer between a range of minimum and maximum.
* @param min - minimum value the integer can take
* @param max - maximum value the integer can take
*/
export function RandInt(min: number, max: number)
/**
* Generate an integer with a certain number of digits, specified by length.
* @param length - number of digits in generated integer
*/
export function RandInt(length: number)
// random integer, either between min and max, or of size len digits
export function RandInt(minOrLen: number, max?: number): number {
if (typeof max === 'number') return RandIntMinMax(minOrLen, max)
else return RandIntLen(minOrLen)
}
7 changes: 7 additions & 0 deletions lib/common/RandomUtils/RandomPhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RandInt } from './RandomNumberUtils'

export function generateRandomPhoneNumber() {
const firstPart = '0' + RandInt(4).toString()
const secondPart = RandInt(6).toString()
return firstPart + ' ' + secondPart
}
4 changes: 3 additions & 1 deletion lib/postProcessing/postProcessors/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PostProcess } from '../../typedefs/PostProcess'
import { processContacts } from '../../vendors/google/Contacts'

export const ContactsCsvPostProcess: PostProcess = {
name: 'Contacts',
Expand All @@ -11,5 +12,6 @@ export const ContactsCsvPostProcess: PostProcess = {
maxDepth: 1
}
},
component: 'VaadinGrid'
component: 'VaadinGrid',
postProcessingFunction: processContacts
}
26 changes: 13 additions & 13 deletions lib/preProcessing/preProcessors/csv.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { PreProcessor } from '../../typedefs/PreProcess'
import * as Papa from 'papaparse'
export const csvPreProcessor: PreProcessor<
{ [key: string]: string | number | boolean }[]
> = ({ filename, fileType, fileContent }) => {
const data = readCsvStringToObject(fileContent)

export type CsvObject = { [key: string]: string | number | boolean }[]
export const csvPreProcessor: PreProcessor<CsvObject> = ({
filename,
fileType,
fileContent
}) => {
const { data, meta } = Papa.parse(fileContent, {
header: true,
skipEmptyLines: true,
dynamicTyping: true
})
const { length } = data
return {
data,
title: filename,
metadata: { length }
metadata: { length, ...meta }
}
}

const readCsvStringToObject = (csvString) => {
return Papa.parse(csvString, {
header: true,
skipEmptyLines: true,
dynamicTyping: true
}).data
}
146 changes: 146 additions & 0 deletions lib/vendors/google/Contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { PostProcessor } from '../../typedefs/PostProcess'
import * as Papa from 'papaparse'
import { getRandomFullName } from '../../common/RandomUtils/RandomContent/RandomNames'
import { generateRandomPhoneNumber } from '../../common/RandomUtils/RandomPhoneNumber'
export type CsvContacts = CsvContactsChild[]
export interface CsvContactsChild {
Name: string
'Given Name': string
'Additional Name'?: string
'Family Name': string
'Yomi Name'?: string
'Given Name Yomi'?: string
'Additional Name Yomi'?: string
'Family Name Yomi'?: string
'Name Prefix'?: string
'Name Suffix'?: string
Initials?: string
Nickname?: string
'Short Name'?: string
'Maiden Name'?: string
Birthday?: string
Gender?: string
Location?: string
'Billing Information'?: string
'Directory Server'?: string
Mileage?: string
Occupation?: string
Hobby?: string
Sensitivity?: string
Priority?: string
Subject?: string
Notes?: string
Language?: string
Photo?: string
'Group Membership': string
'E-mail 1 - Type'?: string
'E-mail 1 - Value'?: string
'E-mail 2 - Type'?: string
'E-mail 2 - Value'?: string
'E-mail 3 - Type'?: string
'E-mail 3 - Value'?: string
'IM 1 - Type'?: string
'IM 1 - Service'?: string
'IM 1 - Value'?: string
'Phone 1 - Type': string
'Phone 1 - Value': string
'Phone 2 - Type'?: string
'Phone 2 - Value'?: string
'Address 1 - Type'?: string
'Address 1 - Formatted'?: string
'Address 1 - Street'?: string
'Address 1 - City'?: string
'Address 1 - PO Box'?: string
'Address 1 - Region'?: string
'Address 1 - Postal Code'?: string
'Address 1 - Country'?: string
'Address 1 - Extended Address'?: string
'Organization 1 - Type': string
'Organization 1 - Name': string
'Organization 1 - Yomi Name'?: string
'Organization 1 - Title'?: string
'Organization 1 - Department'?: string
'Organization 1 - Symbol'?: string
'Organization 1 - Location'?: string
'Organization 1 - Job Description'?: string
'Website 1 - Type'?: string
'Website 1 - Value'?: string
'Custom Field 1 - Type'?: string
'Custom Field 1 - Value'?: string
}
export interface Contact {
fullName: string
profilePictureUrl?: string
firstName: string
lastName: string
phoneNumbers: { phoneNumber: string; label: string }[]
emailAddresses: { emailAddress: string; label: string }[]
organisation?: string
address?: {
streetAddress: string
postCode: string
}
otherDetails: Record<string, any>
}

export const processContacts: PostProcessor<CsvContacts, Contact[]> = ({
preProcessedOutput: { data, metadata }
}) => {
const { fields } = metadata
console.log(data)
const contacts: Contact[] = []
for (const contact of data) {
contacts.push({
address: {
postCode: contact['Address 1 - Postal Code'],
streetAddress: contact['Address 1 - Street']
},
emailAddresses: [
{
emailAddress: contact['E-mail 1 - Value'],
label: contact['E-mail 1 - Type']
}
],
firstName: contact['Given Name'],
fullName: contact.Name,
lastName: contact['Family Name'],
organisation: contact['Organization 1 - Name'],
otherDetails: contact,
phoneNumbers: [
{
phoneNumber: contact['Phone 1 - Value'],
label: contact['Phone 1 - Type']
}
],
profilePictureUrl: contact.Photo
})
}
return {
data: contacts,
title: 'Contacts',
metadata
}
}

function generateContacts(qty = 10): CsvContacts {
const contacts = new Set<CsvContacts[number]>()
for (let i = 0; i < qty; i++) {
const Name = getRandomFullName()
contacts.add({
'Family Name': Name.split(' ').slice(1).join(' '),
'Given Name': Name.split(' ').slice(0, 1).join(' '),
'Group Membership': '* myContacts',
'Organization 1 - Name': '',
'Organization 1 - Type': '', // can be 'unknown'
'Phone 1 - Type': 'Mobile',
'Phone 1 - Value': generateRandomPhoneNumber(),
Name
})
}
return Array.from(contacts)
}

export function generateContactsFile(qty = 10): string {
const contacts = generateContacts(qty)
return Papa.unparse(contacts)
}

0 comments on commit 396a7c3

Please sign in to comment.