diff --git a/.gitignore b/.gitignore index 6f94891a10a1de817a240c07e7c08f46c3f74b50..a2404997d8c1214ea9cf7d0735419d641c991aa8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ user_list.py KNM.csv .~lock* test.json +wikstraktorenv diff --git a/README.md b/README.md index c408fc9ca843a1f8cde0289967d5fa91e6dd5308..af9db3268414ee6dceb7288d9e28287d7e4e36d0 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,38 @@ This project does depend on python packages. * [```pip install wikitextparser```](https://pypi.org/project/wikitextparser/) * [```pip install importlib```](https://pypi.org/project/importlib/) +### Wikstraktor Server +If you want wikstraktor as a server, you need to install [flask](https://flask.palletsprojects.com/en/2.0.x/installation/), and best practice is to do so in a [virtual environment](https://docs.python.org/3/library/venv.html#module-venv). + +The following commands are extracted from the aforementionned documentation, it is probably more secure to click on the link and follow the modules documentation : +```bash +python3 -m venv wikstraktorenv #create wikstraktorenv environment +. wikstraktorenv/bin/activate #activate environment +pip install Flask +``` + +## Use +### Wikstraktor +```python +from wikstraktor import Wikstraktor +f = Wikstraktor.get_instance('fr', 'en') #create a wikstraktor, + # first parameter is the language of the wiki + # second parameter is the language of the word sought for +f.fetch("blue") #fetch an article +str(f) #convert content to json +``` + +### Wikstraktor Server +The server runs by default on port 5000, you can change that in the ```wikstraktor_server_config.py``` file. +```bash +./wikstraktor_server.py +``` +Then there is a very simple API : +```http +GET server_url/search/<word> #Searches the word in the default wiktionary +GET server_url/search/<wiktlang>/<wordlang>/<word> #Searches the word In wordlang in the wiktlang wiktionary +``` +Both API calls return a json object + ## Licence TODO but will be open source diff --git a/wikstraktor.py b/wikstraktor.py index 5257c6cc1d773ba5f5fb69da61591c3dd4455d1e..a40089d4abe5338aa26f485c3e0d77744ba46e8a 100755 --- a/wikstraktor.py +++ b/wikstraktor.py @@ -373,14 +373,14 @@ class Wikstraktor: res = title in self.constants['pro'] print(title, res) return res - + def isEty(self, title): if type(self.constants['ety']) == str: res = title == self.constants['ety'] else: res = title in self.constants['ety'] return res - + def process_POS(self, parsedwikitext): pass#in subclass diff --git a/wikstraktor_server.py b/wikstraktor_server.py new file mode 100755 index 0000000000000000000000000000000000000000..804b03db9a25e2711808d1a7025967bc807813e0 --- /dev/null +++ b/wikstraktor_server.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +from flask import Flask #server +from flask import request #to handle the different http requests +from flask import Response #to reply (we could use jsonify as well but we handled it) +from wikstraktor import Wikstraktor +import wikstraktor_server_config as config + +app = Flask(__name__) +@app.route('/', methods=['GET']) +def index(): + c = request.remote_addr + response = f"<p>Server is running, your ip is {c}</p>" + return Response(response, 200) + +@app.route('/search/<word>', methods=['GET']) +def default_search(word): + return search(config.wiktlang, config.wordlang, word) + +@app.route('/search/<wiktlang>/<wordlang>/<word>', methods=['GET']) +def search(wiktlang, wordlang, word): + w = Wikstraktor.get_instance(wiktlang, wordlang) + if w.fetch(word) > 0: + resp = w.__str__() + status = 200 + else: + resp = f"{word} is unknown" + status = 404 + return Response(resp, status=status) + +if __name__ == "__main__": + app.run(host=config.host, port=config.port, debug=config.debugging) diff --git a/wikstraktor_server_config.py b/wikstraktor_server_config.py new file mode 100644 index 0000000000000000000000000000000000000000..951a8d3dbb6e3ca60ee35ac43b443d3d1e6a7e53 --- /dev/null +++ b/wikstraktor_server_config.py @@ -0,0 +1,5 @@ +wiktlang = "en" +wordlang = "en" +host = "0.0.0.0" +port = 5000 +debugging = True