// programme de scraping des données de Maine.edu // C mais avec bugs dans pinyinise var jsdom = require("jsdom"); const { JSDOM } = jsdom; const fs = require("fs"); var argv = require("optimist").argv; const sq = require("better-sqlite3"); const pinyinizer = require('pinyinizer'); const builder = require('xmlbuilder'); const opencc = require('node-opencc'); // données de stockage var fichier = argv._[0]; var fichierXml = "datas.xml"; var fichierJson = "datas.json"; var baseDeDonnées = "datas.db"; var fichierCSV = "datas.csv"; // données en mémoire // le fichier complet en mémoire var maineeduObj = { "maineedu": [] }; var phraseObj = { "phrase": { "topic": "", "hanzi": { "simplified": "", "traditional": "" }, "pinyin": "", "translations": [], "recordings": [] } } // fichier des locuteurs var locuteurs = []; // un locuteur var locuteur = { "nom": "", "langue": "" } var tab = "\t"; var endLine = "\n"; // on crée le fichier xml var feed = builder.create('maineedu', { version: '1.0', encoding: 'UTF-8', standalone: true }); // on crée le fichier csv var csv = fs.createWriteStream(fichierCSV); var html = fs.readFileSync(fichier, "UTF-8"); dom = new JSDOM(html); var $ = require('jquery')(dom.window); function reset(p) { phraseObj = { "phrase": { "topic": "", "hanzi": { "simplified": "", "traditional": "" }, "pinyin": "", "translations": [], "recordings": [] } } } // fonction de scraping des fichiers c*.html // on traite le topic var $header = $.find("#header H1"); // DEBUG console.log("Le header est: " + $($header).text()); var topic = $($header).text(); // on traite les "rows" $z = dom.window.$(".row ").each(function(index, element) { // le topic phraseObj.phrase.topic = topic; var ligneCSV = ""; var csv1 = ""; var csv2 = ""; //traitement des données textuelles : Hanzi, Pinyin et traduction en anglais //c'est la seule partie qui change entre les différents fichiers. // on retrouve le texte en chinois // DEBUG console.log("index: " + index + " texte en hanzi: " + $(element).find("p .chinese").text()); var hanzi = $(element).find("p .chinese").text(); var traditional = opencc.simplifiedToTraditional(hanzi); phraseObj.phrase.hanzi.simplified = hanzi; phraseObj.phrase.hanzi.traditional = traditional; csv1 = hanzi + tab + traditional + tab; // on retrouve le texte en anglais et pinyin // DEBUG console.log("texte en pinyin et traduction en anglais"); // l'anglais var anglais = $(element).find(".indented img:first").attr("title"); // DEBUG console.log(anglais); var translationObj = { "translation": {} }; translationObj.translation.langue = "en"; translationObj.translation.texte = anglais; phraseObj.phrase.translations.push(translationObj); // le pinyin que l'on transforme d'une version avec chiffres à une version avec accents var pinyin = $(element).find(".indented img:nth-child(2)").attr("title"); pinyin = pinyin.toLowerCase(); // on va tester ça ... try { phraseObj.phrase.pinyin = pinyinizer.pinyinize(pinyin); } catch(err) { console.log("Erreur: " + pinyin); phraseObj.phrase.pinyin = pinyin; } // modif pour court-circuiter pinyinise //phraseObj.phrase.pinyin = pinyin; csv1 = csv1 + phraseObj.phrase.pinyin + tab + anglais + tab; // DEBUG $(element).find(".indented img").each(function(index, letexte) { // console.log("---->" + $(this).attr("title")); // }); // // traitement des données audio en anglais et en chinois // // on retrouve les enregistrements var $t = $(element).find(".speech_samples:first"); // // prononctation en chinois // DEBUG console.log("--> Prononciation en chinois"); $t.find("tr:first td a:nth-child(2)").each(function(index, audio) { // DEBUG console.log("--> " + index + " " + $(this).text() + " " + $(this).attr("href")); var recordingObject = { "recording": {} }; recordingObject.recording.langue = "zh"; recordingObject.recording.locuteur = $(this).text(); recordingObject.recording.audio = $(this).attr("href"); phraseObj.phrase.recordings.push(recordingObject); // on crée la ligne csv à écrire dans le fichier csv2 = recordingObject.recording.locuteur + tab + recordingObject.recording.audio; ligneCSV = csv1 + csv2 + tab + topic + tab + "MaineEdu" + endLine; csv.write(ligneCSV); }); // prononciation en anglais // DEBUG console.log("--> Prononciation en anglais"); $t.find("tr:nth-child(2) td a:nth-child(2)").each(function(index, audio) { // DEBUG console.log("--> " + index + " " + $(this).text() + " " + $(this).attr("href")); var recordingObject = { "recording": {} }; recordingObject.recording.langue = "en"; recordingObject.recording.locuteur = $(this).text(); recordingObject.recording.audio = $(this).attr("href"); phraseObj.phrase.recordings.push(recordingObject); }); // DEBUG console.log(JSON.stringify(phraseObj)); maineeduObj.maineedu.push(phraseObj); var ele = feed.ele(phraseObj); reset(phraseObj); }); // on ferme le fichier CSV csv.end(); // on écrit le fichier Json fs.writeFileSync(fichierJson, JSON.stringify(maineeduObj), "UTF-8"); // DEBUG console.log(JSON.stringify(maineedu)); // on écrit le fichier xml fs.writeFileSync(fichierXml, feed.end({ pretty: true }));