import pymysql
import requests
from requests.auth import HTTPBasicAuth

# DLE veritabanı bilgileri
dle_db_host = 'localhost'
dle_db_user = 'dle_db_user'
dle_db_password = 'dle_db_password'
dle_db_name = 'dle_db_name'

# WordPress API bilgileri
wp_site_url = 'https://yourwordpresssite.com'
wp_api_endpoint = '/wp-json/wp/v2/posts'
wp_username = 'your_wp_username'
wp_password = 'your_wp_password'

# DLE veritabanına bağlanma
connection = pymysql.connect(
    host=dle_db_host,
    user=dle_db_user,
    password=dle_db_password,
    database=dle_db_name
)

def get_dle_content():
    with connection.cursor() as cursor:
        sql = "SELECT id, title, short_story, full_story, category, tags FROM dle_post"
        cursor.execute(sql)
        result = cursor.fetchall()
    return result

def create_wp_post(title, content, categories, tags):
    url = wp_site_url + wp_api_endpoint
    data = {
        'title': title,
        'content': content,
        'status': 'publish',
        'categories': categories,
        'tags': tags
    }
    response = requests.post(url, json=data, auth=HTTPBasicAuth(wp_username, wp_password))
    return response

def main():
    dle_contents = get_dle_content()
    for content in dle_contents:
        title = content[1]
        short_story = content[2]
        full_story = content[3]
        category = [int(cat) for cat in content[4].split(',')]
        tags = content[5].split(',')

        # WordPress gönderisi oluşturma
        response = create_wp_post(title, full_story, category, tags)
        if response.status_code == 201:
            print(f"Başarıyla oluşturuldu: {title}")
        else:
            print(f"Hata: {response.status_code} - {response.text}")

if __name__ == "__main__":
    main()