up post picture wiki osm

This commit is contained in:
Tykayn 2022-12-14 18:18:34 +01:00 committed by tykayn
parent 93caf1ae48
commit 75e460e0f0
5 changed files with 82 additions and 9 deletions

View File

@ -0,0 +1,37 @@
import fetch from "node-fetch"
import rp from "request-promise";
import $ from "cheerio";
import fs from "fs";
import path from 'path';
let url = 'https://wiki.openstreetmap.org/wiki/FR:Key:building:material';
let pictureDescrFinder = '.d_image img'
// rp(url).then(function (html) {
// getSourceOfDescriptorPageContent(html)
// })
export function getSourceOfDescriptorPageContent(HTMLcontent) {
const pictureDescriptionOfTagSrc = $(pictureDescrFinder, HTMLcontent)
let selectedPicture = 'osm_default.jpg';
// image trouvée
if (pictureDescriptionOfTagSrc[0]) {
let sourcesSet = pictureDescriptionOfTagSrc[0].attribs.srcset
let selectedPicture = pictureDescriptionOfTagSrc[0].attribs.src;
sourcesSet = sourcesSet.split(',')
console.log(sourcesSet.length)
// prendre l'image la plus grande du src set
if (sourcesSet.length > 1) {
selectedPicture = sourcesSet[sourcesSet.length - 1]
selectedPicture = selectedPicture.trim().split(' ')[0]
}
console.log(sourcesSet)
console.log(selectedPicture)
return selectedPicture;
} else {
console.log("pas d'image de description dans le HTML")
}
}

View File

@ -33,7 +33,7 @@ function postLink() {
let postObject = {};
let idOfPost = 1091;
// idOfPost = null;
idOfPost = null;
if (idOfPost) {
postObject = list_posts_json.filter(elem => elem.id === idOfPost)[0]
console.log(postObject)

View File

@ -48,7 +48,7 @@ export function tokenForAuthorIsPresentInDotEnv(author) {
*/
export default function sendPostMastodon(config) {
console.log('send post', config.postObject.post_guid , config.postObject.guid )
// console.log('send post', config.postObject.post_guid , config.postObject.guid )
// override defaults with input argument
config = {
...defaultConfigMasto,
@ -241,7 +241,11 @@ function clearLink(linkString){
*/
export function downloadImage(url, filepath) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
const options = {
headers: { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52" }
};
https.get( url, options, (res) => {
if (res.statusCode === 200) {
res.pipe(fs.createWriteStream(filepath))
.on('error', reject)

View File

@ -7,7 +7,8 @@ import rp from "request-promise";
import $ from "cheerio";
import fs from "fs";
import path from 'path';
import sendPostMastodon, {randomIntFromInterval} from './utils.mjs'
import sendPostMastodon, {downloadImage, randomIntFromInterval, slugify} from './utils.mjs'
import {getSourceOfDescriptorPageContent} from "./osm_get_description_picture.mjs";
const __dirname = path.resolve();
@ -156,8 +157,8 @@ export default function getElementCartographique() {
const descriptionStrophe = '';
const filteredHtml = $('.mw-parser-output', html).find('p')
const pictureDescriptionOfTagSrc = $('.d_image img', html).attributes['src']
console.log(pictureDescriptionOfTagSrc)
// trouver l'image de description
configPost.download_description_src = getSourceOfDescriptorPageContent(html)
console.log("filteredHtml", filteredHtml.length)
configPost.long_desc = $(filteredHtml).text().substring(0,250)
@ -167,7 +168,7 @@ export default function getElementCartographique() {
// let imgSelector = ".description a.image img"
console.log("✅ cette page existe bien en Français sur le wiki OSM")
foundExistingWikiPageInFrench = true;
sendMessageWikiTagOfTheDay(makePostMessageFromObj(configPost))
sendMessageWikiTagOfTheDay(makePostMessageFromObj(configPost), configPost.download_description_src)
return;
}, (err) => {
@ -281,7 +282,7 @@ const res = getElementCartographique()
// console.log("res", res)
function sendMessageWikiTagOfTheDay(message) {
function sendMessageWikiTagOfTheDay(message, download_description_src) {
let configPost = {
@ -290,5 +291,36 @@ function sendMessageWikiTagOfTheDay(message) {
image: 'osm_post_' + randomIntFromInterval(1 , 5) + '.jpg',
message,
}
sendPostMastodon(configPost)
if(download_description_src) {
let filePathImage = `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)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB