Ce guide utilise le client de l’API présenté dans l’index des guides. Copiez-le d’abord dans votre projet.
Ce que vous allez construire
Des pipelines d’export vers :- Fichiers CSV : pour Excel ou Google Sheets
- Fichiers JSON : pour les pipelines de données
- BigQuery : pour l’analytics sur Google Cloud
- Snowflake : pour l’entrepôt de données d’entreprise
Récupérer toutes les données
Commencez par rassembler les données de tous les endpoints. Les concurrents et les domaines sources sont récupérés page par page jusqu’à une page vide. L’endpoint des concurrents utilise SELF et DIRECT par défaut : la liste exportée inclut donc la marque suivie lorsque son entrée SELF existe. Filtrez surrelationship si vous souhaitez uniquement les concurrents directs. Les données peuvent changer entre les requêtes : l’export ne constitue donc pas un instantané.
async function fetchAllData(client, brandId, period = 30) {
async function fetchPages(getPage, key) {
const items = [];
let offset = 0;
while (true) {
const page = await getPage(brandId, { period, limit: 100, offset });
const batch = page[key];
if (batch.length === 0) return items;
items.push(...batch);
offset += batch.length;
}
}
const [performance, competitors, sources] = await Promise.all([
client.getPerformance(brandId, { period }),
fetchPages(client.getCompetitors.bind(client), 'competitors'),
fetchPages(client.getSourceDomains.bind(client), 'sources'),
]);
return {
exportedAt: new Date().toISOString(),
brandId,
period,
performance: {
scores: performance.scores,
methodology: performance.methodology,
},
competitors: competitors.map(c => ({
id: c.id,
name: c.name,
relationship: c.relationship,
shareOfVoice: c.shareOfVoice,
totalMentions: c.totalMentions,
avgPosition: c.avgPosition,
avgSentiment: c.avgSentiment,
})),
sources: sources.map(s => ({
id: s.id,
domain: s.domain,
type: s.type,
isSelf: s.isSelf,
totalMentions: s.totalMentions,
rate: s.rate,
avgPosition: s.avgPosition,
})),
};
}
def fetch_all_data(client, brand_id: str, period: int = 30) -> dict:
"""Fetch all data for export."""
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
def fetch_pages(get_page, key):
items = []
offset = 0
while True:
page = get_page(brand_id, period=period, limit=100, offset=offset)
batch = page[key]
if not batch:
return items
items.extend(batch)
offset += len(batch)
with ThreadPoolExecutor(max_workers=3) as executor:
perf_future = executor.submit(client.get_performance, brand_id, period=period)
comp_future = executor.submit(fetch_pages, client.get_competitors, 'competitors')
src_future = executor.submit(fetch_pages, client.get_source_domains, 'sources')
performance = perf_future.result()
competitors = comp_future.result()
sources = src_future.result()
return {
'exported_at': datetime.now(timezone.utc).isoformat(),
'brand_id': brand_id,
'period': period,
'performance': {
'scores': performance['scores'],
'methodology': performance['methodology'],
},
'competitors': [
{
'id': c['id'],
'name': c['name'],
'relationship': c['relationship'],
'share_of_voice': c['shareOfVoice'],
'total_mentions': c['totalMentions'],
'avg_position': c['avgPosition'],
'avg_sentiment': c['avgSentiment'],
}
for c in competitors
],
'sources': [
{
'id': s['id'],
'domain': s['domain'],
'type': s['type'],
'is_self': s['isSelf'],
'total_mentions': s['totalMentions'],
'rate': s['rate'],
'avg_position': s['avgPosition'],
}
for s in sources
],
}
Exporter en CSV
Les exemples ajoutent une apostrophe devant les textes ressemblant à une formule avant l’encodage CSV, afin de réduire le risque d’exécution dans un tableur. Cela modifie le texte exporté : utilisez l’export JSON si vous avez besoin des valeurs originales exactes. Les fichiers de concurrents et de sources conservent leurs en-têtes même lorsqu’ils sont vides.const fs = require('fs');
function exportToCSV(data, outputDir = './exports') {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const timestamp = data.exportedAt.split('T')[0];
const csvCell = value => {
let text = value == null ? '' : String(value);
if (typeof value === 'string' && (/^[\s]*[=+@-]/u.test(text) || /^[\t\r\n]/.test(text))) {
text = `'${text}`;
}
return /[",\r\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
};
const csvRow = values => values.map(csvCell).join(',');
// Competitors CSV
const competitorHeaders = ['id', 'name', 'relationship', 'shareOfVoice', 'totalMentions', 'avgPosition', 'avgSentiment'];
const competitorRows = data.competitors.map(c => csvRow(competitorHeaders.map(h => c[h])));
fs.writeFileSync(
`${outputDir}/competitors_${timestamp}.csv`,
[competitorHeaders.join(','), ...competitorRows].join('\n')
);
// Sources CSV
const sourceHeaders = ['id', 'domain', 'type', 'isSelf', 'totalMentions', 'rate', 'avgPosition'];
const sourceRows = data.sources.map(s => csvRow(sourceHeaders.map(h => s[h])));
fs.writeFileSync(
`${outputDir}/sources_${timestamp}.csv`,
[sourceHeaders.join(','), ...sourceRows].join('\n')
);
// Performance CSV (single row)
const perfHeaders = ['mentionRate', 'sourceRate', 'coverage', 'shareOfVoice', 'sentiment'];
const perfValues = [
data.performance.scores.mentionRate,
data.performance.scores.sourceRate,
data.performance.scores.coverage,
data.performance.scores.shareOfVoice,
data.performance.scores.sentiment,
];
fs.writeFileSync(
`${outputDir}/performance_${timestamp}.csv`,
[csvRow(perfHeaders), csvRow(perfValues)].join('\n')
);
return {
files: [
`${outputDir}/competitors_${timestamp}.csv`,
`${outputDir}/sources_${timestamp}.csv`,
`${outputDir}/performance_${timestamp}.csv`,
],
};
}
import csv
import os
import re
def spreadsheet_safe(value):
if isinstance(value, str) and (
re.match(r'^\s*[=+@-]', value) or value.startswith(('\t', '\r', '\n'))
):
return "'" + value
return value
def export_to_csv(data: dict, output_dir: str = './exports') -> dict:
"""Export data to CSV files."""
os.makedirs(output_dir, exist_ok=True)
timestamp = data['exported_at'].split('T')[0]
files = []
# Competitors CSV
comp_file = f'{output_dir}/competitors_{timestamp}.csv'
with open(comp_file, 'w', newline='') as f:
fields = ['id', 'name', 'relationship', 'share_of_voice', 'total_mentions', 'avg_position', 'avg_sentiment']
writer = csv.DictWriter(f, fieldnames=fields)
writer.writeheader()
writer.writerows({key: spreadsheet_safe(row[key]) for key in fields} for row in data['competitors'])
files.append(comp_file)
# Sources CSV
src_file = f'{output_dir}/sources_{timestamp}.csv'
with open(src_file, 'w', newline='') as f:
fields = ['id', 'domain', 'type', 'is_self', 'total_mentions', 'rate', 'avg_position']
writer = csv.DictWriter(f, fieldnames=fields)
writer.writeheader()
writer.writerows({key: spreadsheet_safe(row[key]) for key in fields} for row in data['sources'])
files.append(src_file)
# Performance CSV
perf_file = f'{output_dir}/performance_{timestamp}.csv'
scores = data['performance']['scores']
with open(perf_file, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['mention_rate', 'source_rate', 'coverage', 'share_of_voice', 'sentiment'])
writer.writeheader()
writer.writerow({
'mention_rate': scores['mentionRate'],
'source_rate': scores['sourceRate'],
'coverage': scores['coverage'],
'share_of_voice': scores['shareOfVoice'],
'sentiment': scores['sentiment'],
})
files.append(perf_file)
return {'files': files}
Exporter en JSON
const fs = require('fs');
function exportToJSON(data, outputDir = './exports') {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const timestamp = data.exportedAt.split('T')[0];
const filepath = `${outputDir}/qwairy_export_${timestamp}.json`;
fs.writeFileSync(filepath, JSON.stringify(data, null, 2));
return { file: filepath, size: fs.statSync(filepath).size };
}
import json
import os
def export_to_json(data: dict, output_dir: str = './exports') -> dict:
"""Export data to JSON file."""
os.makedirs(output_dir, exist_ok=True)
timestamp = data['exported_at'].split('T')[0]
filepath = f'{output_dir}/qwairy_export_{timestamp}.json'
with open(filepath, 'w') as f:
json.dump(data, f, indent=2)
return {'file': filepath, 'size': os.path.getsize(filepath)}
Schémas de base de données
Créez ces tables avant d’exporter vers BigQuery ou Snowflake.-- Create dataset
CREATE SCHEMA IF NOT EXISTS qwairy;
-- Competitors table
CREATE TABLE IF NOT EXISTS qwairy.competitors (
id STRING NOT NULL,
name STRING NOT NULL,
relationship STRING,
share_of_voice FLOAT64,
total_mentions INT64,
avg_position FLOAT64,
avg_sentiment FLOAT64,
exported_at TIMESTAMP NOT NULL,
brand_id STRING NOT NULL
);
-- Sources table
CREATE TABLE IF NOT EXISTS qwairy.sources (
id STRING NOT NULL,
domain STRING NOT NULL,
type STRING,
is_self BOOL,
total_mentions INT64,
rate FLOAT64,
avg_position FLOAT64,
exported_at TIMESTAMP NOT NULL,
brand_id STRING NOT NULL
);
-- Performance table
CREATE TABLE IF NOT EXISTS qwairy.performance (
mentionRate FLOAT64,
sourceRate FLOAT64,
coverage FLOAT64,
shareOfVoice FLOAT64,
sentiment FLOAT64,
methodology STRING,
exported_at TIMESTAMP NOT NULL,
brand_id STRING NOT NULL
);
-- Create schema
CREATE SCHEMA IF NOT EXISTS QWAIRY;
-- Competitors table
CREATE TABLE IF NOT EXISTS QWAIRY.COMPETITORS (
ID VARCHAR(50) NOT NULL,
NAME VARCHAR(255) NOT NULL,
RELATIONSHIP VARCHAR(50),
SHARE_OF_VOICE FLOAT,
TOTAL_MENTIONS INTEGER,
AVG_POSITION FLOAT,
AVG_SENTIMENT FLOAT,
EXPORTED_AT TIMESTAMP_NTZ NOT NULL,
BRAND_ID VARCHAR(50) NOT NULL
);
-- Sources table
CREATE TABLE IF NOT EXISTS QWAIRY.SOURCES (
ID VARCHAR(50) NOT NULL,
DOMAIN VARCHAR(255) NOT NULL,
TYPE VARCHAR(50),
IS_SELF BOOLEAN,
TOTAL_MENTIONS INTEGER,
RATE FLOAT,
AVG_POSITION FLOAT,
EXPORTED_AT TIMESTAMP_NTZ NOT NULL,
BRAND_ID VARCHAR(50) NOT NULL
);
-- Performance table
CREATE TABLE IF NOT EXISTS QWAIRY.PERFORMANCE (
MENTION_RATE FLOAT,
SOURCE_RATE FLOAT,
COVERAGE FLOAT,
SHARE_OF_VOICE FLOAT,
SENTIMENT FLOAT,
METHODOLOGY VARCHAR(5000),
EXPORTED_AT TIMESTAMP_NTZ NOT NULL,
BRAND_ID VARCHAR(50) NOT NULL
);
Exporter vers BigQuery
Créez le jeu de données et les tables ci-dessus avant de lancer l’export. Les exemples envoient jusqu’à 500 lignes à la fois et visent 8 Mo de données pour laisser de la place aux métadonnées de la requête BigQuery. Une ligne plus volumineuse est envoyée seule et doit tenir dans la limite de 10 Mo par requête. En cas d’échec, vérifiez quelles lignes ont été écrites avant de réessayer.const { BigQuery } = require('@google-cloud/bigquery');
async function exportToBigQuery(data, datasetId = 'qwairy', projectId = process.env.GCP_PROJECT_ID) {
const bigquery = new BigQuery({ projectId });
const dataset = bigquery.dataset(datasetId);
const competitors = data.competitors.map(c => ({
id: c.id, name: c.name, relationship: c.relationship,
share_of_voice: c.shareOfVoice, total_mentions: c.totalMentions,
avg_position: c.avgPosition, avg_sentiment: c.avgSentiment,
exported_at: data.exportedAt, brand_id: data.brandId,
}));
const sources = data.sources.map(s => ({
id: s.id, domain: s.domain, type: s.type, is_self: s.isSelf,
total_mentions: s.totalMentions, rate: s.rate, avg_position: s.avgPosition,
exported_at: data.exportedAt, brand_id: data.brandId,
}));
const scores = data.performance.scores;
const performance = {
mentionRate: scores.mentionRate, sourceRate: scores.sourceRate,
coverage: scores.coverage, shareOfVoice: scores.shareOfVoice,
sentiment: scores.sentiment,
methodology: JSON.stringify(data.performance.methodology),
exported_at: data.exportedAt, brand_id: data.brandId,
};
for (const [name, rows] of Object.entries({ competitors, sources, performance: [performance] })) {
const table = dataset.table(name);
let batch = [];
let bytes = 0;
for (const row of rows) {
const rowBytes = Buffer.byteLength(JSON.stringify(row), 'utf8');
if (batch.length && (batch.length >= 500 || bytes + rowBytes > 8_000_000)) {
await table.insert(batch);
batch = [];
bytes = 0;
}
batch.push(row);
bytes += rowBytes;
}
if (batch.length) await table.insert(batch);
}
return { dataset: datasetId, tables: ['competitors', 'sources', 'performance'] };
}
from google.cloud import bigquery
def export_to_bigquery(data: dict, dataset_id: str = 'qwairy', project_id: str = None) -> dict:
"""Export data to BigQuery."""
import os
import json
project_id = project_id or os.environ.get('GCP_PROJECT_ID')
client = bigquery.Client(project=project_id)
project_id = client.project
def batches(rows):
batch = []
size = 0
for row in rows:
row_size = len(json.dumps(row).encode('utf-8'))
if batch and (len(batch) >= 500 or size + row_size > 8_000_000):
yield batch
batch = []
size = 0
batch.append(row)
size += row_size
if batch:
yield batch
competitors = [
{**c, 'exported_at': data['exported_at'], 'brand_id': data['brand_id']}
for c in data['competitors']
]
sources = [
{**s, 'exported_at': data['exported_at'], 'brand_id': data['brand_id']}
for s in data['sources']
]
scores = data['performance']['scores']
performance = {
'mentionRate': scores['mentionRate'],
'sourceRate': scores['sourceRate'],
'coverage': scores['coverage'],
'shareOfVoice': scores['shareOfVoice'],
'sentiment': scores['sentiment'],
'methodology': json.dumps(data['performance']['methodology']),
'exported_at': data['exported_at'],
'brand_id': data['brand_id'],
}
tables = {'competitors': competitors, 'sources': sources, 'performance': [performance]}
for name, rows in tables.items():
for batch in batches(rows):
errors = client.insert_rows_json(f'{project_id}.{dataset_id}.{name}', batch)
if errors:
raise RuntimeError(f'BigQuery insert failed for {name}: {errors}')
return {'dataset': dataset_id, 'tables': ['competitors', 'sources', 'performance']}
Exporter vers Snowflake
Ces exemples alimentent uniquement les tables de concurrents et de sources. Ils n’insèrent ni les scores de performance ni leur méthodologie dans la table de performance définie ci-dessus.const snowflake = require('snowflake-sdk');
async function exportToSnowflake(data, config) {
const connection = snowflake.createConnection({
account: config.account,
username: config.username,
password: config.password,
warehouse: config.warehouse,
database: config.database,
schema: config.schema || 'QWAIRY',
});
await new Promise((resolve, reject) => {
connection.connect((err) => err ? reject(err) : resolve());
});
const execute = (sql, binds = []) => new Promise((resolve, reject) => {
connection.execute({ sqlText: sql, binds, complete: (err, stmt, rows) => err ? reject(err) : resolve(rows) });
});
// Insert competitors
for (const c of data.competitors) {
await execute(
`INSERT INTO competitors (id, name, relationship, share_of_voice, total_mentions, avg_position, avg_sentiment, exported_at, brand_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[c.id, c.name, c.relationship, c.shareOfVoice, c.totalMentions, c.avgPosition, c.avgSentiment, data.exportedAt, data.brandId]
);
}
// Insert sources
for (const s of data.sources) {
await execute(
`INSERT INTO sources (id, domain, type, is_self, total_mentions, rate, avg_position, exported_at, brand_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[s.id, s.domain, s.type, s.isSelf, s.totalMentions, s.rate, s.avgPosition, data.exportedAt, data.brandId]
);
}
connection.destroy();
return { schema: config.schema || 'QWAIRY', tables: ['competitors', 'sources'] };
}
import snowflake.connector
def export_to_snowflake(data: dict, config: dict) -> dict:
"""Export data to Snowflake."""
conn = snowflake.connector.connect(
account=config['account'],
user=config['username'],
password=config['password'],
warehouse=config['warehouse'],
database=config['database'],
schema=config.get('schema', 'QWAIRY'),
)
cursor = conn.cursor()
# Insert competitors
for c in data['competitors']:
cursor.execute(
'''INSERT INTO competitors (id, name, relationship, share_of_voice, total_mentions, avg_position, avg_sentiment, exported_at, brand_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)''',
(c['id'], c['name'], c['relationship'], c['share_of_voice'], c['total_mentions'],
c['avg_position'], c['avg_sentiment'], data['exported_at'], data['brand_id'])
)
# Insert sources
for s in data['sources']:
cursor.execute(
'''INSERT INTO sources (id, domain, type, is_self, total_mentions, rate, avg_position, exported_at, brand_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)''',
(s['id'], s['domain'], s['type'], s['is_self'], s['total_mentions'],
s['rate'], s['avg_position'], data['exported_at'], data['brand_id'])
)
conn.commit()
cursor.close()
conn.close()
return {'schema': config.get('schema', 'QWAIRY'), 'tables': ['competitors', 'sources']}
Utilisation
const client = new QwairyClient(process.env.QWAIRY_API_TOKEN);
// Fetch data
const data = await fetchAllData(client, 'your-brand-id', 30);
// Export to files
const csvResult = exportToCSV(data);
console.log('CSV files:', csvResult.files);
const jsonResult = exportToJSON(data);
console.log('JSON file:', jsonResult.file, `(${jsonResult.size} bytes)`);
// Export to BigQuery (requires @google-cloud/bigquery)
// const bqResult = await exportToBigQuery(data);
// console.log('BigQuery tables:', bqResult.tables);
client = QwairyClient()
# Fetch data
data = fetch_all_data(client, 'your-brand-id', period=30)
# Export to files
csv_result = export_to_csv(data)
print(f"CSV files: {csv_result['files']}")
json_result = export_to_json(data)
print(f"JSON file: {json_result['file']} ({json_result['size']} bytes)")
# Export to BigQuery (requires google-cloud-bigquery)
# bq_result = export_to_bigquery(data)
# print(f"BigQuery tables: {bq_result['tables']}")
Planifier les exports
Automatisez des exports quotidiens ou hebdomadaires :| Plateforme | Configuration |
|---|---|
| Cron | 0 1 * * * (tous les jours à 1 h) |
| GitHub Actions | schedule: cron: '0 1 * * *' |
| AWS Lambda | Règle planifiée EventBridge |
| Google Cloud | Cloud Scheduler + Cloud Functions |
Étapes suivantes
- Créez un tableau de bord sur mesure pour un suivi à la demande
- Mettez en place des rapports hebdomadaires pour les parties prenantes
- Ajoutez l’analyse concurrentielle à vos exports

