import requests import random import re import time import sys import io # ========================= # UTF-8 FIX # ========================= sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="ignore") sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") # ========================= # CONFIG # ========================= WP_URL = "https://blog.omlete.ru/wp-json/wp/v2/posts" WP_USER = "Omlete" WP_APP_PASSWORD = "9pac UZk8 kLVC vmvu DrUy 2kSO" WP_CATEGORY_ID = 17 OLLAMA_URL = "http://localhost:11434/api/generate" MODEL = "llama3.1:8b" # 👈 ВАЖНО: лучше чем qwen7b VK_TOKEN = "vk1.a.xxxxxxxxxxxxxxxxxxxxxxxxx" VK_GROUP_ID = 236199048 VK_API_VERSION = "5.199" EMOJIS = ["🔥", "😈", "❤️", "⚡", "😉", "💥", "👀", "💬"] # ========================= # CLEAN # ========================= def clean(text): if not text: return "" text = re.sub(r"```.*?```", "", text, flags=re.DOTALL) text = text.replace("**", "").replace("*", "").replace("#", "") text = re.sub(r"\n\s*\n", "\n", text) return text.strip() # ========================= # EMOJI # ========================= def add_emojis(text): lines = text.split("\n") out = [] for line in lines: if "

" in line or "

" in line: line = line.replace(">", f"> {random.choice(EMOJIS)} ", 1) if "

" in line and random.random() > 0.8: line = line.replace("

", f" {random.choice(EMOJIS)}

") out.append(line) return "\n".join(out) # ========================= # OLLAMA SAFE CALL # ========================= def generate_article(topic): prompt = f""" Ты SEO-копирайтер. Тема: {topic} Требования: - HTML (h1 h2 h3 p ul li) - ~1000 слов - без markdown - живой стиль """ for i in range(3): try: r = requests.post( OLLAMA_URL, json={ "model": MODEL, "prompt": prompt, "stream": False }, timeout=300 ) data = r.json() text = data.get("response", "") if text: return add_emojis(clean(text)) except Exception as e: print(f"OLLAMA retry {i+1}: {e}") time.sleep(3) return None # ========================= # WORDPRESS SLUG # ========================= def slugify(text): text = text.lower() text = re.sub(r"[^a-zа-я0-9\s-]", "", text) text = text.strip() return text.replace(" ", "-")[:60] or "post" # ========================= # WORDPRESS POST # ========================= def post_wp(title, content): try: r = requests.post( WP_URL, json={ "title": title, "content": content, "status": "publish", "categories": [WP_CATEGORY_ID], "slug": slugify(title) }, auth=(WP_USER, WP_APP_PASSWORD), timeout=30 ) data = r.json() print("WP STATUS:", r.status_code) if "id" not in data: print("WP ERROR:", data) return None return data.get("link") or f"{WP_URL}/{data['id']}" except Exception as e: print("WP ERROR:", e) return None # ========================= # VK POST # ========================= def post_vk(title, link, content): preview = re.sub(r"<.*?>", "", content or "")[:250] message = f""" 📰 OMLETE.RU 📌 {title} {preview}... 👉 Читать полностью: {link} """ try: r = requests.post( "https://api.vk.com/method/wall.post", data={ "owner_id": -VK_GROUP_ID, "from_group": 1, "message": message, "access_token": VK_TOKEN, "v": VK_API_VERSION }, timeout=20 ) data = r.json() print("VK RESPONSE:", data) if "error" in data: print("VK ERROR:", data["error"]) return None post_id = data.get("response") if not post_id: return None return f"https://vk.com/wall-{VK_GROUP_ID}_{post_id}" except Exception as e: print("VK ERROR:", e) return None # ========================= # MAIN # ========================= if __name__ == "__main__": topic = input("Введите тему: ").strip() if not topic: print("NO TOPIC") exit() print("Генерация статьи...") article = generate_article(topic) if not article: print("AI FAILED") exit() print("Публикация WP...") wp_link = post_wp(topic, article) if not wp_link: print("WP FAILED") exit() print("Публикация VK...") vk_link = post_vk(topic, wp_link, article) print("\nDONE") print("WP:", wp_link) print("VK:", vk_link) time.sleep(2)