# Path: src/merveilles/utils.py
# def gen_thumbnail_for_url(url, filename):
#     thumbnail_location = current_app.config['THUMBNAIL_DIR'] if current_app else THUMBNAIL_DIR
#     is_image = url.lower().endswith(("jpg", "jpeg", "gif", "png"))
#     if not is_image:
#         return None
# 
#     ext = url.split(".")[-1]
#     full_filepath = "{0}{1}.{2}".format(thumbnail_location,
#         filename, ext.lower())
# 
#     if os.path.isfile(full_filepath):
#         print("File exists: {}".format(full_filepath))
#         return "thumbnails/{}.{}".format(filename, ext.lower())
# 
#     r = requests.get(url)
# 
#     if r.status_code == 200:
#         with open("/tmp/tmp_img", 'wb') as f:
#             for chunk in r.iter_content():
#                 f.write(chunk)
# 
#         im = Image.open("/tmp/tmp_img")
#         im.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
#         print("Thumbnail writing to {}".format(full_filepath))
#         if ext.lower() == 'jpg':
#             im.save(full_filepath, 'JPEG')
#         else:
#             im.save(full_filepath, ext)
# 
#         return "thumbnails/{}.{}".format(filename, ext.lower())
#     return None
# 
# Path: src/merveilles/constants.py
# FILTER_MAX = 25
# 
# PERSON_COLORS = ["#FFD923", "#AA2BEF", "#366EEF", "#A68B0B", "#18BECD",
#     "#4BFD03", "#32D7B7", "#E13611"
# ]
# 
# Path: src/merveilles/utils.py
# def get_domain(raw_url):
#     return get_domain_filter(raw_url['url'])
# 
# def visible(element):
#     if element.parent.name in ['style', 'script', '[document]',
#         'head', 'title', 'link', 'meta', 'h1', 'h2', 'h3', 'h4',
#         'ul']:
#         return False
#     elif re.match('<!--.*-->', str(element)):
#         return False
# 
#     return True


from flask import current_app, session, g, Response
from bcrypt import hashpw, gensalt
from bs4 import BeautifulSoup
from datetime import datetime, timedelta, date
from kyotocabinet import DB
from json import dumps, loads
from time import mktime
from urllib.request import urlopen
from .utils import gen_thumbnail_for_url
from .constants import FILTER_MAX, PERSON_COLORS
from .utils import get_domain, visible
from functools import reduce
    from merveilles.database import _get_user_str
import random
            continue

        if item_filter(rec):
            items.append(rec)

        cur.step_back()
    cur.disable()
    db.close()

    sorted_items = sorted(items, key=get_key, reverse=True)
    sorted_items_for_viewing = [loads(item[1]) for item in sorted_items]
    for item in sorted_items_for_viewing:
        if item['title'] is None or item['title'] == "":
            item['title'] = item['url']
    return sorted_items_for_viewing

def get_items_on_page(page, db_file):
    item_iter = 0
    items = []
    db = DB()
    if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
        print("Could not open database.")

    cur = db.cursor()
    cur.jump_back()
    while len(items) < FILTER_MAX:
        rec = cur.get(False)
        if not rec:
            break


# Path: src/merveilles/utils.py
# def gen_thumbnail_for_url(url, filename):
#     thumbnail_location = current_app.config['THUMBNAIL_DIR'] if current_app else THUMBNAIL_DIR
#     is_image = url.lower().endswith(("jpg", "jpeg", "gif", "png"))
#     if not is_image:
#         return None
# 
#     ext = url.split(".")[-1]
#     full_filepath = "{0}{1}.{2}".format(thumbnail_location,
#         filename, ext.lower())
# 
#     if os.path.isfile(full_filepath):
#         print("File exists: {}".format(full_filepath))
#         return "thumbnails/{}.{}".format(filename, ext.lower())
# 
#     r = requests.get(url)
# 
#     if r.status_code == 200:
#         with open("/tmp/tmp_img", 'wb') as f:
#             for chunk in r.iter_content():
#                 f.write(chunk)
# 
#         im = Image.open("/tmp/tmp_img")
#         im.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
#         print("Thumbnail writing to {}".format(full_filepath))
#         if ext.lower() == 'jpg':
#             im.save(full_filepath, 'JPEG')
#         else:
#             im.save(full_filepath, ext)
# 
#         return "thumbnails/{}.{}".format(filename, ext.lower())
#     return None
# 
# Path: src/merveilles/constants.py
# FILTER_MAX = 25
# 
# PERSON_COLORS = ["#FFD923", "#AA2BEF", "#366EEF", "#A68B0B", "#18BECD",
#     "#4BFD03", "#32D7B7", "#E13611"
# ]
# 
# Path: src/merveilles/utils.py
# def get_domain(raw_url):
#     return get_domain_filter(raw_url['url'])
# 
# def visible(element):
#     if element.parent.name in ['style', 'script', '[document]',
#         'head', 'title', 'link', 'meta', 'h1', 'h2', 'h3', 'h4',
#         'ul']:
#         return False
#     elif re.match('<!--.*-->', str(element)):
#         return False
# 
#     return True


# Path: src/merveilles/database.py
from flask import current_app, session, g, Response
from bcrypt import hashpw, gensalt
from bs4 import BeautifulSoup
from datetime import datetime, timedelta, date
from kyotocabinet import DB
from json import dumps, loads
from time import mktime
from urllib.request import urlopen
from .utils import gen_thumbnail_for_url
from .constants import FILTER_MAX, PERSON_COLORS
from .utils import get_domain, visible
from functools import reduce
    from merveilles.database import _get_user_str
import random

            continue

        if item_filter(rec):
            items.append(rec)

        cur.step_back()
    cur.disable()
    db.close()

    sorted_items = sorted(items, key=get_key, reverse=True)
    sorted_items_for_viewing = [loads(item[1]) for item in sorted_items]
    for item in sorted_items_for_viewing:
        if item['title'] is None or item['title'] == "":
            item['title'] = item['url']
    return sorted_items_for_viewing

def get_items_on_page(page, db_file):
    item_iter = 0
    items = []
    db = DB()
    if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
        print("Could not open database.")

    cur = db.cursor()
    cur.jump_back()
    while len(items) < FILTER_MAX:
        rec = cur.get(False)
        if not rec:
            break


        if item_iter >= (FILTER_MAX * page):