Raw Text Content QR
monkey-hawk-parrot



from flask import Flask, jsonify, request, render_template
import json
import os
import traceback
from flask_cors import CORS


API_VERSION = 'v0.1.0'


app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})


categories = {
  "ai_generated": ["#007bff", "AI content that's disguising as non-AI. Not counting interactive AI tools like chatbots"],
  "non_conclusive": ["#28a792", "Anything that donesn't answer question in title"],
  "fake_news": ["#e29015", "Fake / incorrect information"],
  "sponsored": ["#6cd33c", "Sponsored product affecting article's recommendations"],
  "non_trustworthy": ["#c958e6", "Content is known to not being moderated and having controversial articles"],
  "scam": ["#e22626", "Scam / phishing / malware"],
  "anti_patterns": ["#6d45ca", "Deliberately bad design patterns, like fullscreen popups / autoplay ads with audio / paywalls"]
}


def strip_url(url: str):
    stripped_url = url.rstrip('/').lstrip('https://').lstrip('http://').lstrip('www.')
    return stripped_url

def strip_hostname(url: str):
    stripped_hostname = strip_url(url).split('/')[0].split('?')[0].split('#')[0]
    return stripped_hostname


queries = 0
requests = 0

def count_queries(number: int):
    global queries
    global requests
    with open('db/queries.txt', 'r') as f:
        data = f.readlines()
        queries = int(data[0])
        requests = int(data[1])
    queries += number
    requests += 1
    with open('db/queries.txt', 'w') as f:
        f.write(str(queries) + '\n' + str(requests))


@app.route('/api/sites', methods=['POST'])
def sites():
    with open('db/db.json', 'r') as f:
        sites = json.load(f)
    
    urls = request.get_json()
    if not isinstance(urls, list):
        return "expected a list of URLs", 400
    result = {}
    print(f'analising {len(urls)} urls')
    for url in urls:
        result[url] = []
        site = sites.get(strip_url(url))
        if site:
          result[url] = site
        site = sites.get(strip_hostname(url))
        if site:
          result[url] = site
    count_queries(len(urls))

    return jsonify(result)


@app.route('/api/categories', methods = ['GET'])
def get_categories():
    return categories


@app.route('/api/version', methods = ['GET'])
def get_api_version():
    return API_VERSION


@app.route('/api/submit', methods = ['POST'])
def submit():
    try:
        with open('db/db.json', 'r') as f:
            jason = json.load(f)
        item = request.get_json()
        key = strip_url(item['url'])
        hostname = strip_hostname(key)
        value1 = item['category'].lower()
        value2 = item['text']
        value3 = item['type'].lower()
        if value3 not in ['website', 'article']: return f"User field not implemented in {hostname}", 500
        if value3 == 'website': key = hostname
        jason[key] = [value1, value2, value3]
        with open('db/db.json', 'w') as f:
            json.dump(jason, f, indent=4)
    except:
        return traceback.format_exc(), 500
    return "Ok", 201


@app.route('/')
def main():
    print('searching for db file...')
    if not os.path.exists('db/db.json'):
        print('creating db.json file')
        with open('db/db.json', 'w') as f:
            f.write('{}')
    if not os.path.exists('db/queries.txt'):
        with open('db/queries.txt', 'w') as f:
            f.write('0\n0')
    return render_template('index.html', queries=queries, requests=requests)


if __name__ == '__main__':
    print('searching for db file...')
    if not os.path.exists('db/db.json'):
        print('creating db.json file')
        with open('db/db.json', 'w') as f:
            f.write('{}')
    app.run(host='0.0.0.0', port=5000)

Read 4 times, last 4 days ago