OXIESEC PANEL
- Current Dir:
/
/
var
/
www
/
zopalv1
/
opalv1
Server IP: 10.0.0.4
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
09/25/2021 03:02:46 AM
rwxr-xr-x
📄
0__init__.py
5.13 KB
09/20/2021 06:07:13 AM
rw-r--r--
📄
__init__.py
18.61 KB
09/20/2021 06:07:13 AM
rw-r--r--
📁
__pycache__
-
09/20/2021 06:07:09 AM
rwxr-xr-x
📁
documents
-
09/20/2021 06:07:07 AM
rwxr-xr-x
📄
o2__init__.py
8.26 KB
09/20/2021 06:07:14 AM
rw-r--r--
📄
o__init__.py
11.75 KB
09/20/2021 06:07:14 AM
rw-r--r--
Editing: 0__init__.py
Close
#!/bin/usr/python3 import flask from flask import request, jsonify import pymongo from pymongo import MongoClient from bson import json_util, ObjectId import json app = flask.Flask(__name__) app.config['DEBUG'] = True #test data as a list of Python dictionary books = [ {'id': 0, 'title': 'A Fire Upon the Deep', 'author': 'Vernor Vinge', 'first_sentence': 'The coldsleep itself was dreamless.', 'year_published': '1992'}, {'id': 1, 'title': 'The Ones Who Walk Away From Omelas', 'author': 'Ursula K. Le Guin', 'first_sentence': 'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.', 'published': '1973'}, {'id': 2, 'title': 'Dhalgren', 'author': 'Samuel R. Delany', 'first_sentence': 'to wound the autumnal city.', 'published': '1975'} ] @app.route('/', methods=['GET']) def home(): # return jsonify(books) return '''<h1>Opal Archives</h1><p>This is a prototype api for opal archives</p>''' @app.route('/api/v1/resources/books/all', methods=['GET']) def api_all(): return jsonify(books) @app.route('/api/v1/resources/books', methods=['GET']) def api_id(): if 'id' in request.args: id = int(request.args['id']) else: return "Error: id not found. please provide a valid id" results = [] for book in books: if book['id'] == id: results.append(book) return jsonify(results) @app.route('/api/v1/resources/articlesbyauthor', methods=['GET']) def api_author(): if 'author' in request.args: my_author = request.args['author'] #return my_author else: return "Error: author not specified. Please provide an author" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test query = {'author' : { "$regex" : my_author, "$options": "i" }} articles = collection.find(query) for art in articles: art_results.append(art) if len(art_results) > 0: return json_util.dumps(art_results) else: return "No document found matching your author keyword" @app.route('/api/v1/resources/articlesbytitle', methods=['GET']) def api_title(): if 'title' in request.args: keyword = request.args['title'] # return my_author else: return "Error: keyword not specified. Please provide a keyword" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test # #build_fragment = "author:" + "/" + my_author + "/" query = {'title' : { "$regex" : keyword, "$options": "i" }} articles = collection.find(query) for art in articles: art_results.append(art) if len(art_results) > 0: return json_util.dumps(art_results) else: return "No document found matching your title keyword" @app.route('/api/v1/resources/articlesbycategory', methods=['GET']) def api_category(): if 'category' in request.args: cat_key = request.args['category'] # return my_author else: return "Error: keyword not specified. Please provide a keyword" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test query = {'category' : { "$regex" : cat_key, "$options": "i" }} articles = collection.find(query) for art in articles: art_results.append(art) if len(art_results) > 0: return json_util.dumps(art_results) else: return "No document found matching your category keyword" @app.route('/api/v1/resources/cms/upload', methods=['GET']) def api_upload(): my_title = request.args['title'] my_author = request.args['author'] my_category = request.args['category'] my_year_published = request.args['year_published'] my_first_sentence = request.args['first_sentence'] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test my_id = collection.count() query = {"id" : my_id+1, "title": my_title, "author": my_author, "category": my_category, "year_published" : my_year_published, "first_sentence" : my_first_sentence} result = collection.insert_one(query) if result: return "Article added successfully" else: return "Failed to add article..." @app.route('/api/v1/resources/articles/featured', methods=['GET']) def api_featured(): client = MongoClient("localhost", 27017) if not client: return [{"status: 500", "Resource not available"}] db = client.opalv1 collection = db.test random_result = collection.find({}, { "id" : 1, "title" : 1 , "first_sentence" : 1, "_id" : 0 }).limit(3) #random_result = collection.find([{ "$project" : { "id" : 1, "title" : 1 , "author" : 1 } }]).limit(3) #random_result = collection.aggregate([ { $sample: { size: 3 } } ]) featured_articles = [{"status: 200"}] for art in random_result: featured_articles.append(art) if len(featured_articles) > 0: return json_util.dumps(featured_articles) else: return "Sorry no documents found" @app.route('/api/v1/resources/article', methods=['GET']) def api_featured_article(): my_id = request.args['id'] return my_id client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test result = collection.find({"id" : my_id}) for art in result: return json_util.dumps(art) else: return "Sorry no documents found" #with changes if __name__ == "__main__": app.run()