<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Recall is a program for storing bookmarks of different things
# Copyright (C) 2012  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from urllib.parse import unquote

from bottle import abort, request, Bottle, response

from recall import convenience as conv
from recall import (
    plugins,
    search,
    data,
    jobs,
    )

from bs4 import BeautifulSoup

logger = conv.logger("bookmarks")

app = Bottle()
app.install(plugins.ppjson)
app.install(plugins.auth)
app.install(plugins.cors)
app.error_handler = plugins.handler_dict

@app.post("/<who>/public/<when>/")
def add_public(who, when, user):
    if "~" not in request.json or "@" not in request.json:
        abort(400, "You must include @ and ~ with all bookmarks")
    if request.json["@"] != who or who != user["email"]:
        abort(400, "You may only add bookmarks as yourself")
    if request.json["~"] != int(when):
        abort(400, "You must use the same time in the bookmark as you post to")
    if data.has_problematic_keys(request.json):
        abort(400, "Bookmarks must not have keys prefixed with $ or £")
    request.json["£created"] = conv.unixtime()
    conv.db().bookmarks.insert(request.json)
    del request.json["_id"]
    jobs.enqueue(search.IndexRecord(request.json), priority=1)
    response.status = 202

@app.post("/<who>/private/<when>/")
def add_private(who, when, user):
    request.json["%private"] = True
    add_public(who, when, user)

@app.get("/public/")
def public_bookmarks():
    query = search.SearchQueryBuilder()
    if "q" in request.params:
        query.with_keywords(request.params["q"])
    query.anonymously()
    total, results = search.search(query)
    response.set_header("X-Recall-Total", total)
    if results == []:
        response.status = 404
    data.strip_generated_keys(results)
    return results

@app.get("/<who>/all/")
def user_all_bookmarks(who, user):
    if who != user["email"]:
        abort(400, "You may only look at your own bookmarks")
    query = search.SearchQueryBuilder()
    if "q" in request.params:
        query.with_keywords(request.params["q"])
    query.as_user(user)
    total, results = search.search(query)
    if results == []:
        response.status = 404
    data.strip_generated_keys(results)
    return results

@app.route("/<who>/", method="PATCH")
def import_(who, user):
    soup = BeautifulSoup(request.body)
    if soup.contents[0] != "NETSCAPE-Bookmark-file-1":
        abort(400, "You must send a bookmark file with the doctype " +
              " 'NETSCAPE-Bookmarks-file-1'")<commit_msg>Change import to use POST<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Recall is a program for storing bookmarks of different things
# Copyright (C) 2012  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from urllib.parse import unquote

from bottle import abort, request, Bottle, response

from recall import convenience as conv
from recall import (
    plugins,
    search,
    data,
    jobs,
    )

from bs4 import BeautifulSoup

logger = conv.logger("bookmarks")

app = Bottle()
app.install(plugins.ppjson)
app.install(plugins.auth)
app.install(plugins.cors)
app.error_handler = plugins.handler_dict

@app.post("/<who>/public/<when>/")
def add_public(who, when, user):
    if "~" not in request.json or "@" not in request.json:
        abort(400, "You must include @ and ~ with all bookmarks")
    if request.json["@"] != who or who != user["email"]:
        abort(400, "You may only add bookmarks as yourself")
    if request.json["~"] != int(when):
        abort(400, "You must use the same time in the bookmark as you post to")
    if data.has_problematic_keys(request.json):
        abort(400, "Bookmarks must not have keys prefixed with $ or £")
    request.json["£created"] = conv.unixtime()
    conv.db().bookmarks.insert(request.json)
    del request.json["_id"]
    jobs.enqueue(search.IndexRecord(request.json), priority=1)
    response.status = 202

@app.post("/<who>/private/<when>/")
def add_private(who, when, user):
    request.json["%private"] = True
    add_public(who, when, user)

@app.get("/public/")
def public_bookmarks():
    query = search.SearchQueryBuilder()
    if "q" in request.params:
        query.with_keywords(request.params["q"])
    query.anonymously()
    total, results = search.search(query)
    response.set_header("X-Recall-Total", total)
    if results == []:
        response.status = 404
    data.strip_generated_keys(results)
    return results

@app.get("/<who>/all/")
def user_all_bookmarks(who, user):
    if who != user["email"]:
        abort(400, "You may only look at your own bookmarks")
    query = search.SearchQueryBuilder()
    if "q" in request.params:
        query.with_keywords(request.params["q"])
    query.as_user(user)
    total, results = search.search(query)
    if results == []:
        response.status = 404
    data.strip_generated_keys(results)
    return results

@app.route("/<who>/", method="POST")
def import_(who, user):
    soup = BeautifulSoup(request.body)
    if soup.contents[0] != "NETSCAPE-Bookmark-file-1":
        abort(400, "You must send a bookmark file with the doctype " +
              " 'NETSCAPE-Bookmarks-file-1'")
{'fraction_non_alphanumeric': 0.12347584503781447, 'fraction_numerical': 0.011730205278592375, 'mean_word_length': 3.5665961945031714, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2937805', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1965, 'n_words': 639}