import json
import requests

class Config:
    API_URL = "https://stiri.md/new_graphql"
    WP_AUTH = "Basic cHVibGlzaGVyOkloYkIgYzIxTSB3bXR5IDJMeDYgdkdKdSBVa1Rp"
    OPERATION_NAME = "NewsList"
    PROJECT_ID = "9dcb71b1-afeb-43ef-aa3a-a77e2562953a"

class Variables:
    def __init__(self, take, projectId):
        self.take = take
        self.projectId = projectId

class StiriMD:
    def fetchArticles(self, take):
        variables = Variables(take, Config.PROJECT_ID)
        jsonObject = self.prepareJSONObject(variables)
        response = self.request(jsonObject)
        object = json.loads(response.content)

        return object["data"]["contents"]

    def prepareJSONObject(self, variables):
        jsonObject = {
            "operationName": Config.OPERATION_NAME,
            "variables": vars(variables),
            "query": self.prepareQuery()
        }

        return jsonObject

    def prepareQuery(self):
        return """
            query NewsList(
                $projectId: String!,
                $take: Int
            ) {
                contents(
                    project_id: $projectId
                    lang: "ro"
                    visible: true
                    take: $take
                ) { ...ContentsFields }
            }

            fragment ContentsFields on Content {
                id
                title { long short }
                description { long intro }
                album { image source }
                url
                tags
                dates {
                    posted: posted(format: "2 $$Jan$$. 15:04", lang: "ro", getDiff: true)
                    postedTs: posted
                    postedH: posted(format: "15:04", lang: "ro")
                    postedSeparator: posted(format: "2 $$January$$", lang: "ro")
                }
                thumbnail
                cparent {
                    id
                    url { ro }
                }
                parents {
                    id
                    description { ro }
                    title { ro }
                    url { ro }
                    type
                    attachment
                }
            }
        """

    def request(self, bodyObject):
        headers = {
            "Content-Type": "application/json",
            "Authorization": Config.WP_AUTH
        }
        response = requests.post(Config.API_URL, json=bodyObject, headers=headers, timeout=5)
        response.raise_for_status()
        
        return response

class WPCategory:
    def __init__(self, name, slug, description=None):
        self.name = name
        self.slug = slug
        self.description = description

    def createCategory(self):
        try:
            response = self.request()
            return True
        except Exception as e:
            return False

    def request(self):
        headers = {
            "Content-Type": "application/json",
            "Authorization": Config.WP_AUTH
        }
        data = {
            "name": self.name,
            "slug": self.slug,
            "description": self.description
        }
        response = requests.post("https://news.4x4.md/wp-json/wp/v2/categories", json=data, headers=headers, timeout=5)
        response.raise_for_status()

        return response

client = requests.Session()
r = client.get("https://news.4x4.md/wp-json/wp/v2/categories", headers={"Content-Type": "application/json"}, timeout=5)
print(r.text)
