137 lines
5.0 KiB
JavaScript
137 lines
5.0 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;
|
|
|
|
function makePostMessageFromObj(post_obj) {
|
|
return `
|
|
# Le tag OSM du jour : ${post_obj.key}=${post_obj.value} 🗺️🏷️
|
|
${post_obj.link}
|
|
|
|
${post_obj.description ? post_obj.description.trim() : ''}
|
|
${post_obj.long_desc ? post_obj.long_desc.trim() : ''}
|
|
#osm #openstreetmap #wiki #rtfw
|
|
`
|
|
}
|
|
|
|
// 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('params', params)
|
|
|
|
let message = ''
|
|
let download_description_src = ''
|
|
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`
|
|
}
|
|
const firstParagraph = articleContent.split('<p>')[1].split('</p>')[0];
|
|
|
|
// 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);
|
|
|
|
})
|
|
.catch(error => {
|
|
console.error(error);
|
|
});
|
|
|
|
|
|
export function sendMessageWikiOSMOfTheDay(message, download_description_src) {
|
|
|
|
|
|
let configPost = {
|
|
author: 'curator',
|
|
// document.querySelector('.d_image img').attributes['src']
|
|
// image: '/home/tykayn/www/multi-account-post-schedule-mastodon/assets/osm_post_' + randomIntFromInterval(1 , 5) + '.jpg',
|
|
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')
|
|
sendPostMastodon(configPost)
|
|
}
|
|
}
|
|
|
|
sendMessageWikiOSMOfTheDay(message, download_description_src) |