135 lines
4.8 KiB
JavaScript
135 lines
4.8 KiB
JavaScript
/**
|
|
* Envoi d'une page du wiki au hasard parmi les pages en Français répertoriées dans le json all_wiki_osm.json
|
|
* Cette liste d'environ 3000 pages du wiki contient toutes les pages francophones, pas seulement les tags.
|
|
* Utiliser l'argument --force pour réellement envoyer le post avec le compte Curator.
|
|
* nécessite d'avoir le fichier .env rempli.
|
|
*/
|
|
import fs from 'fs';
|
|
import path, {dirname} from 'path';
|
|
import axios from 'axios';
|
|
import {fileURLToPath} from 'url';
|
|
import {getSourceOfDescriptorPageContent} from "./osm_get_description_picture.mjs";
|
|
import sendPostMastodon, {downloadImage, randomIntFromInterval} from "./libs/utils.mjs";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Charger le fichier JSON
|
|
const wikiArticles = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'assets', 'documents', 'all_wiki_osm.json'), 'utf8'));
|
|
|
|
// Sélectionner un article au hasard
|
|
const randomIndex = Math.floor(Math.random() * wikiArticles['elements'].length);
|
|
console.log('wikiArticles[\'elements\']', wikiArticles['elements'].length)
|
|
const selectedArticle = wikiArticles['elements'][randomIndex];
|
|
|
|
console.log('selectedArticle', selectedArticle)
|
|
// Récupérer le titre et la description de l'article
|
|
const title = selectedArticle.title;
|
|
const pageId = selectedArticle.pageid;
|
|
|
|
// Récupérer le contenu de l'article via l'API de MediaWiki
|
|
const wikiApiUrl = `https://wiki.openstreetmap.org/w/api.php`;
|
|
const params = {
|
|
action: 'parse',
|
|
pageid: pageId,
|
|
format: 'json',
|
|
prop: 'text',
|
|
section: 0
|
|
};
|
|
console.log('url de l article: ', `https://wiki.openstreetmap.org/wiki/` + title)
|
|
|
|
let message = ''
|
|
let download_description_src = ''
|
|
|
|
export function sendMessageWikiOSMOfTheDay(message, download_description_src) {
|
|
|
|
|
|
console.log('envoi du message ............')
|
|
let configPost = {
|
|
author: 'curator',
|
|
image: `${process.cwd()}/assets/blog_posts_medias/assets/osm_post_${randomIntFromInterval(1, 5)}.jpg`,
|
|
message,
|
|
}
|
|
if (download_description_src) {
|
|
let filePathImage = `${process.cwd()}/assets/blog_posts_medias/osm_wiki_description_page.jpg`
|
|
if (download_description_src) {
|
|
console.log("firstPictureSource found", download_description_src)
|
|
|
|
// check if picture already exist
|
|
console.log('on récupère l image de description : ', filePathImage)
|
|
downloadImage(download_description_src, filePathImage)
|
|
.then((res) => {
|
|
// suite du poste avec upload d'image
|
|
|
|
console.log('média téléchargé, on envoie le post')
|
|
configPost.image = filePathImage;
|
|
|
|
|
|
sendPostMastodon(configPost)
|
|
|
|
},
|
|
(err) => {
|
|
console.log('pas dimage trouvée pour l URL ', download_description_src, err)
|
|
sendPostMastodon(configPost)
|
|
}
|
|
)
|
|
.catch((err) => {
|
|
console.log('erreur avec cette URL ', download_description_src, err)
|
|
sendPostMastodon(configPost)
|
|
})
|
|
}
|
|
|
|
} else {
|
|
console.log('::::::::::::: no image description')
|
|
console.log('configPost.image', configPost.image)
|
|
sendPostMastodon(configPost)
|
|
}
|
|
}
|
|
|
|
axios.get(wikiApiUrl, {params})
|
|
.then(response => {
|
|
// console.log('wikiApiUrl', wikiApiUrl)
|
|
let keys = Object.keys(response.data)
|
|
// console.log('keys', keys)
|
|
const articleContent = response.data.parse.text['*'];
|
|
|
|
let resultpicture = getSourceOfDescriptorPageContent(articleContent)
|
|
|
|
// console.log('resultpicture', resultpicture)
|
|
if (resultpicture === "pas d'image de description dans le HTML") {
|
|
download_description_src = `${process.cwd()}/assets/blog_posts_medias/osm_wiki_description_page.jpg`
|
|
}
|
|
let firstParagraph;
|
|
if (articleContent.includes('<p>')) {
|
|
firstParagraph = articleContent.split('<p>')[1].split('</p>')[0];
|
|
} else {
|
|
firstParagraph = articleContent.substring(0, 200);
|
|
}
|
|
|
|
// Sanitizer le texte
|
|
const sanitizedText = firstParagraph.replace(/<\/?[^>]+(>|$)/g, '');
|
|
const sanitizedTextWithoutEntities = sanitizedText.replace(/&#(\d+);/g, (match, code) => {
|
|
return String.fromCharCode(code);
|
|
});
|
|
|
|
console.log('image', download_description_src)
|
|
console.log('-----------------')
|
|
// Générer le message
|
|
message = `Le page #OSM du jour : ${title}.
|
|
https://wiki.openstreetmap.org/wiki/${title}
|
|
|
|
${sanitizedTextWithoutEntities}
|
|
|
|
#openstreetmap #wiki #RTFW
|
|
`;
|
|
|
|
console.log(message);
|
|
|
|
sendMessageWikiOSMOfTheDay(message, download_description_src)
|
|
|
|
|
|
})
|
|
.catch(error => {
|
|
console.error(error);
|
|
});
|
|
|