# -*- coding: utf-8 -*-
from lektor.pluginsystem import Plugin
from flask import jsonify
import requests

class WikipediaProxyPlugin(Plugin):
    name = 'Wikipedia Proxy'
    description = 'A simple plugin to fetch data from Wikipedia.'

    def on_setup_env(self, **extra):
        @self.env.app.route('/wikipedia-proxy/get_wikipedia_data')
        def get_wikipedia_data():
            wikipediaAPI = 'https://en.wikipedia.org/w/api.php'
            pageTitle = 'List_of_hackers'
            params = {
                'action': 'query',
                'format': 'json',
                'titles': pageTitle,
                'prop': 'extracts|pageimages',
                'exintro': True,
                'explaintext': True,
                'piprop': 'original',
                'pithumbsize': 400,
            }
            response = requests.get(wikipediaAPI, params=params)
            if response.status_code == 200:
                return jsonify(response.json())
            else:
                return jsonify(error="Failed to retrieve data"), 500