- Jul 27, 2020
- 150
- 79
Hello dear ones,
I have been working on programming a Python script for the last few days. I started from 0 and now unfortunately I can't get any further.
Task of the script should be:
It should scan posts on a wordpress page, and for those that don't have a featured image, automatically fetch one from pixabay and post it.
I exported it as an exe and ran it, but unfortunately it does nothing.
Can you maybe look over it, if there is something wrong with the script? Or am I doing something fundamentally wrong, the thing must be loaded somehow on wordpress or something like that?
I've spent the last days and nights looking for an answer, but unfortunately I can't get any further ...
And once I'm here among you professionals: I would need a (probably) somewhat more elaborate script (for a fee, of course). Who likes, can please send me a PM.
Thank you!
I have been working on programming a Python script for the last few days. I started from 0 and now unfortunately I can't get any further.
Task of the script should be:
It should scan posts on a wordpress page, and for those that don't have a featured image, automatically fetch one from pixabay and post it.
I exported it as an exe and ran it, but unfortunately it does nothing.
Can you maybe look over it, if there is something wrong with the script? Or am I doing something fundamentally wrong, the thing must be loaded somehow on wordpress or something like that?
I've spent the last days and nights looking for an answer, but unfortunately I can't get any further ...
import requests
import json
# WordPress API-Endpunkt und Zugangsdaten
url = "https://XYZ/wp-json/wp/v2/"
username = "XYZ"
password = "XYZ"
# API-Endpunkt von Pixabay und API-Schlüssel
pixabay_url = "https://pixabay.com/api/"
pixabay_key = "370460-3037d382659cdd939413354b5"
# Funktion zum Abrufen der Beiträge aus WordPress
def get_posts():
response = requests.get(url + "posts", auth=(username, password))
return json.loads(response.text)
# Funktion zum Festlegen des Beitragsbilds in WordPress
def set_post_thumbnail(post_id, media_id):
data = {
"featured_media": media_id
}
response = requests.post(url + "posts/" + str(post_id), json=data, auth=(username, password))
return json.loads(response.text)
# Funktion zum Herunterladen von Bildern von Pixabay
def download_image(image_url):
response = requests.get(image_url)
return response.content
# Loop durch alle Beiträge in WordPress
for post in get_posts():
# Überprüfen, ob ein Beitragsbild vorhanden ist
if not post["featured_media"]:
# Schlagwort des Beitrags abrufen
search_term = post["tags"][0]["name"]
# Pixabay-API-Aufruf mit Schlagwort des Beitrags
params = {
"key": pixabay_key,
"q": search_term,
"image_type": "photo",
"per_page": 200,
}
response = requests.get(pixabay_url, params=params)
data = json.loads(response.text)
# Loop durch alle gefundenen Bilder
for image in data["hits"]:
# Überprüfen, ob das Bild bereits in einem anderen Beitrag verwendet wurde
existing_posts = get_posts()
used_images = []
for existing_post in existing_posts:
if existing_post["featured_media"]:
existing_image = requests.get(url + "media/" + str(existing_post["featured_media"]), auth=(username, password))
existing_image = json.loads(existing_image.text)
used_images.append(existing_image["source_url"])
if image["webformatURL"] not in used_images:
# Herunterladen des Bildes von Pixabay
image_content = download_image(image["webformatURL"])
# Hochladen des Bildes in WordPress
headers = {
"Content-Disposition": "attachment; filename=\"" + image["tags"] + ".jpg\""
}
files = {
"file": ("file.jpg", image_content)
}
response = requests.post(url + "media", headers=headers, files=files, auth=(username, password))
media = json.loads(response.text)
# Festlegen des Beitragsbilds
set_post_thumbnail(post["id"], media["id"])
# Hinzufügen der Quellenangabe am Ende des Beitrags
post_content = requests.get(url + "posts/" + str(post["id"]), auth=(username, password))
post_content = json.loads(post_content.text)
source_text = "<br><br>Bildquelle: <a href='" + image["pageURL"] + "'>" + image["pageURL"] + "</a>"
updated_content = {
"content": {
"rendered": post_content["content"]["rendered"] + source_text
}
}
And once I'm here among you professionals: I would need a (probably) somewhat more elaborate script (for a fee, of course). Who likes, can please send me a PM.
Thank you!