def create_tree(lang, leaf_size=50): count = db.exe("select count(*) from sites where lang = ?", (lang, )).fetchone()[0] vecs = np.empty((count, 512)) ids = np.empty(count, '|S22') for i, row in enumerate( db.exe("select vector, id from sites where lang = ?", (lang, ))): vecs[i] = struct.unpack("512f", row[0]) ids[i] = row[1] return ids, BallTree(vecs, leaf_size), count
def conf(): #全局设定信息 global NAME,Subtitle,description,keywords,Category,UUID conf = db.db("SELECT SITENAME,subtitle,description,keywords,uuid FROM CONFIG")[0] NAME = conf[0] Subtitle = conf[1] description = conf[2] keywords = conf[3] UUID= conf[4] if not UUID: UUID=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes) print db.exe("UPDATE config SET uuid='%s' WHERE ID=1" % UUID) Category = [(i[0],i[1]) for i in db.db("SELECT ID,Category FROM Category")] Category.append((' ',' '))
def conf(): #全局设定信息 global configure configure = {} conf = db.db("SELECT SITENAME,subtitle,description,keywords,uuid FROM CONFIG")[0] configure['NAME'] = conf[0] configure['Subtitle'] = conf[1] configure['description'] = conf[2] configure['keywords'] = conf[3] UUID= conf[4] if not UUID: UUID=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes) print db.exe("UPDATE config SET uuid='%s' WHERE ID=1" % UUID) configure['UUID'] = UUID Category = [(i[0],i[1]) for i in db.db("SELECT ID,Category FROM Category")] Category.append((' ',' ')) configure['Category'] = Category return configure
def post(self): TYPE=self.get_argument('TYPE','') ID=self.get_argument('ID','') subject = self.get_argument('subject','') tags = self.get_argument('tags','') markdown = self.get_argument('markdown','') Category = self.get_argument('Category','') if TYPE=='DEL': Category = Category[0:-1] SQL = "DELETE FROM Category WHERE ID IN (%s)" % Category self.write(db.exe(SQL)) elif TYPE=='NEW': SQL = "INSERT INTO Category (Category) VALUES ('%s')" % Category self.write(db.exe(SQL)) elif None or "" in (subject,tags,markdown): self.write(u"主题、标签、类别及内容均不可为空!") else: if db.edit(TYPE,subject.encode("utf-8"),tags.encode("utf-8"),markdown.encode("utf-8"),Category.encode("utf-8"),ID): tohtml.html().ALL() self.write(u'OK,文章已成功发布!') else: self.write(u'Error!')
def search_tree(query, ids, tree, tree_size): vector = embed(query) dd, ii = tree.query(vector, k=min(tree_size, 15)) # ii, dd = tree.query_radius( # vector, 2, return_distance=True, sort_results=True) distances, indexes = dd[0], ii[0] wanted_ids = map(lambda i: str(ids[i])[1:], indexes) print(distances) results = [] for wanted_id in wanted_ids: sql = f"{DB_SELECTOR} where sites.id = {wanted_id}" row = db.exe(sql).fetchone() results.append(row_to_dict(row)) return results
def search_match(query, lang="?"): use_lang = lang != "?" pattern = f"%{query.strip()}%" results = [] sql = f""" {DB_SELECTOR} where {"sites.lang = ? and" if use_lang else ""} (domains.name like ? or sites.url like ? or sites.title like ? or sites.description like ?) order by case when domains.name like ? then 1 else 2 end, case when sites.url like ? then 2 else 3 end, case when sites.title like ? then 3 else 4 end, case when sites.description like ? then 4 else 5 end, domains.alexa_rank """ params = (*([lang] if use_lang else []), *tuple(pattern for _ in range(8))) for row in db.exe(sql, (params)): results.append(row_to_dict(row)) return results
def init(): langs = db.exe("select distinct lang from sites") for lang in langs: new_tree(*lang)