Zorla getirme kararı nasıl uygulanır ?

Aydin

New member
import os

import requests

from requests.auth import HTTPBasicAuth

import time

import shutil

import random

import re

# WordPress API bilgileri

WORDPRESS_URL = "https://parofix.com/wp-json/wp/v2/posts"

MEDIA_URL = "https://parofix.com/wp-json/wp/v2/media"

TAGS_URL = "https://parofix.com/wp-json/wp/v2/tags"

USERNAME = "admin"

PASSWORD = "uEiA EvhM mxu8 wHOT e79e 14rK"

CATEGORY_MAPPING = {

"4": [4], "1": [1], "5": [5], "2": [2], "14": [14], "16": [16], "21": [21], "crypto": [91, 108]

}

auth = HTTPBasicAuth(USERNAME, PASSWORD)

LINK_FILE_PATH = r"C:UsersKullanıcıDesktop20siteprojesi5.parofixwordpresslink.txt"

ETIKET_FILE_PATH = r"C:UsersKullanıcıDesktop20siteprojesi5.parofixwordpressetiket.txt"

TAMAM_DIR = r"C:UsersKullanıcıDesktop20siteprojesi5.parofixwordpresstamam"

RAPOR_FILE_PATH = r"C:UsersKullanıcıDesktop20siteprojesi5.parofixwordpressrapor.txt"

# Yasaklı etiket kelimeleri

YASAKLI_KELIMELER = {"ve", "ile", "veya", "ama", "fakat", "ancak"}

def seo_description(text):

"""İlk 160 karakterden SEO description oluşturur"""

desc = re.sub(r"s+", " ", text.strip())

return desc[:160]

def otomatik_etiketler(title):

"""Başlıktan 4-25 harf arası kelimeler seçer"""

kelimeler = [k.lower() for k in re.findall(r"w+", title) if 4 <= len(k) <= 25 and k.lower() not in YASAKLI_KELIMELER]

return list(set(kelimeler))[:2]

def upload_image(image_path, title):

"""Resmi başlığa göre yeniden adlandırıp yükler, alt text ve caption ekler"""

ext = os.path.splitext(image_path)[1]

seo_filename = re.sub(r"[^a-zA-Z0-9-]", "-", title.lower()) + ext

new_path = os.path.join(os.path.dirname(image_path), seo_filename)

os.rename(image_path, new_path)

alt_caption = " ".join(title.split()[:3])

with open(new_path, 'rb') as img:

files = {'file': (seo_filename, img, 'image/jpeg')}

data = {

'title': title,

'caption': alt_caption,

'alt_text': alt_caption

}

r = requests.post(MEDIA_URL, files=files, data=data, auth=auth)

if r.status_code == 201:

return r.json()['id'], r.json()['source_url']

else:

print(f"Resim yüklenemedi: {r.status_code} - {r.text}")

return None, None

def load_links():

links = {}

with open(LINK_FILE_PATH, 'r', encoding="utf-8") as f:

for line in f:

if "|" in line:

keyword, url = line.strip().split("|")

links[keyword] = url

return links

def replace_keywords_with_links(content, links):

count = 0

for keyword, url in links.items():

if count < 3:

content = content.replace(keyword, f'<a href="{url}">{keyword}</a>', 1)

count += 1

return content

def get_tag_ids(tags):

ids = []

for tag in tags:

res = requests.get(TAGS_URL, params={'search': tag}, auth=auth)

if res.status_code == 200 and res.json():

ids.append(res.json()[0]['id'])

else:

create_res = requests.post(TAGS_URL, json={"name": tag}, auth=auth)

if create_res.status_code == 201:

ids.append(create_res.json()['id'])

return ids

def create_post(title, content, category_ids, image_paths=None, status="draft"):

links = load_links()

content = replace_keywords_with_links(content, links)

# SEO description

excerpt = seo_description(content)

# Etiketler

tags = otomatik_etiketler(title)

tag_ids = get_tag_ids(tags)

post_data = {

"title": title,

"content": content,

"status": status,

"categories": category_ids,

"tags": tag_ids,

"excerpt": excerpt

}

if image_paths:

uploaded_images = []

for i, img in enumerate(image_paths):

media_id, media_url = upload_image(img, title)

if media_id:

if i == 0:

post_data["featured_media"] = media_id

else:

# Rastgele paragraf arasına yerleştir

paragraphs = content.split("n")

insert_index = random.randint(1, len(paragraphs) - 1)

paragraphs.insert(insert_index, f'<img src="{media_url}" alt="{title}"/>')

content = "n".join(paragraphs)

uploaded_images.append(media_id)

post_data["content"] = content

r = requests.post(WORDPRESS_URL, json=post_data, auth=auth)

if r.status_code == 201:

post_id = r.json()['id']

with open(RAPOR_FILE_PATH, 'a', encoding="utf-8") as rf:

rf.write(f"{title} | {WORDPRESS_URL}/{post_id} | {' '.join(['#'+t for t in tags])}n")

return True

else:

print(f"Post oluşturulamadı: {r.status_code} - {r.text}")

return False

def process_category_folders(base_dir, delay):

while True:

categories = list(CATEGORY_MAPPING.items())

random.shuffle(categories)

processed = False

for category, ids in categories:

category_folder = os.path.join(base_dir, category)

if os.path.isdir(category_folder):

for folder in os.listdir(category_folder):

folder_path = os.path.join(category_folder, folder)

if os.path.isdir(folder_path):

txt_file = None

image_files = []

for file in os.listdir(folder_path):

if file.endswith(".txt"):

txt_file = os.path.join(folder_path, file)

elif file.lower().endswith((".jpg", ".jpeg", ".png")):

image_files.append(os.path.join(folder_path, file))

if txt_file:

title = os.path.splitext(os.path.basename(txt_file))[0]

with open(txt_file, "r", encoding="utf-8") as f:

content = f.read()

success = create_post(title, content, ids, image_paths=image_files, status="publish")

if success:

shutil.move(folder_path, os.path.join(TAMAM_DIR, folder))

processed = True

time.sleep(delay)

break

if not processed:

print("Makale yok, 300 saniye bekleniyor...")

time.sleep(300)

def main():

BASE_DIR = r"C:UsersKullanıcıDesktop20siteprojesi5.parofixmakaleler"

delay = int(input("Kaç saniye arayla makale atmak istiyorsunuz? "))

process_category_folders(BASE_DIR, delay)

if __name__ == "__main__":

main()