Przeglądaj źródła

서치 API도 Go로 전환

잉여개발기 (SPDV) 2 lat temu
rodzic
commit
2c698c66dc

+ 4 - 2
app.py

@@ -695,8 +695,10 @@ app.route('/api/thread/<int:topic_num>/<tool>/<int:num>')(api_topic)
 app.route('/api/thread/<int:topic_num>/<tool>')(api_topic)
 app.route('/api/thread/<int:topic_num>')(api_topic)
 
-app.route('/api/search/<everything:name>/doc_num/<int:num>/<int:page>')(api_search)
-app.route('/api/search/<everything:name>')(api_search)
+app.route('/api/search/<everything:name>', defaults = { 'db_set' : db_set_str })(api_search)
+app.route('/api/search_page/<int:num>/<everything:name>', defaults = { 'db_set' : db_set_str })(api_search)
+app.route('/api/search_data/<everything:name>', defaults = { 'search_type' : 'data', 'db_set' : db_set_str })(api_search)
+app.route('/api/search_data_page/<int:num>/<everything:name>', defaults = { 'search_type' : 'data', 'db_set' : db_set_str })(api_search)
 
 app.route('/api/recent_change/<int:num>')(api_recent_change)
 app.route('/api/recent_change')(api_recent_change)

+ 2 - 1
route/__init__.py

@@ -2,7 +2,6 @@ from route.api_func_lang import api_func_lang
 from route.api_image_view import api_image_view
 from route.api_recent_change import api_recent_change
 from route.api_recent_discuss import api_recent_discuss
-from route.api_search import api_search
 from route.api_setting import api_setting
 from route.api_skin_info import api_skin_info
 from route.api_topic import api_topic
@@ -171,6 +170,8 @@ from route.vote_select import vote_select
 
 from route.go_api_func_sha224 import api_func_sha224
 
+from route.go_api_search import api_search
+
 from route.go_api_w_raw import api_w_raw
 
 from route.go_view_random import view_random

+ 0 - 29
route/api_search.py

@@ -1,29 +0,0 @@
-from .tool.func import *
-
-def api_search(name = 'Test', num = 10, page = 1):
-    with get_db_connect() as conn:
-        curs = conn.cursor()
-
-        num = 1 if num > 1000 else num
-        page = (page * (num - 1)) if page * num > 0 else 0
-
-        # 개편 예정
-        curs.execute(db_change('select data from other where name = "count_all_title"'))
-        if int(curs.fetchall()[0][0]) < 30000:
-            curs.execute(db_change("" + \
-                "select distinct title, case " + \
-                "when title like ? then 'title' else 'data' end from data " + \
-                "where (title like ? or data like ?) order by case " + \
-                "when title like ? then 1 else 2 end limit ?, ?"),
-                ['%' + name + '%', '%' + name + '%', '%' + name + '%', '%' + name + '%', page, num]
-            )
-        else:
-            curs.execute(db_change("select title from data where title like ? order by title limit ?, ?"),
-                ['%' + name + '%', page, num]
-            )
-            
-        all_list = curs.fetchall()
-        if all_list:
-            return flask.jsonify(all_list)
-        else:
-            return flask.jsonify({})

+ 26 - 0
route/go_api_search.py

@@ -0,0 +1,26 @@
+from .tool.func import *
+
+def api_search(db_set, name = 'Test', search_type = 'title', num = 1):
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+
+        other_set = {}
+        other_set["name"] = name
+        other_set["search_type"] = search_type
+        other_set["num"] = str(num)
+        other_set = json.dumps(other_set)
+
+        if platform.system() == 'Linux':
+            if platform.machine() in ["AMD64", "x86_64"]:
+                data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+            else:
+                data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+        else:
+            if platform.machine() in ["AMD64", "x86_64"]:
+                data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+            else:
+                data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+
+        data = data.decode('utf8')
+
+        return flask.Response(response = data, status = 200, mimetype = 'application/json')

+ 1 - 1
route/main_search.py

@@ -2,4 +2,4 @@ from .tool.func import *
 
 def main_search():
     with get_db_connect() as conn:
-        return redirect('/search/' + url_pas(flask.request.form.get('search', 'test')))
+        return redirect('/search/' + url_pas(flask.request.form.get('search', 'test')))

BIN
route_go/bin/main.amd64.bin


BIN
route_go/bin/main.amd64.exe


BIN
route_go/bin/main.arm64.bin


BIN
route_go/bin/main.arm64.exe


+ 2 - 0
route_go/main.go

@@ -16,5 +16,7 @@ func main() {
 		route.Api_func_sha224(call_arg[1:])
 	} else if call_arg[0] == "view_random" {
 		route.View_random(call_arg[1:])
+	} else if call_arg[0] == "api_search" {
+		route.Api_search(call_arg[1:])
 	}
 }

+ 93 - 0
route_go/route/api_search.go

@@ -0,0 +1,93 @@
+package route
+
+import (
+	"encoding/json"
+	"fmt"
+	"strconv"
+
+	"opennamu/route/tool"
+)
+
+func Api_search(call_arg []string) {
+	db_set := map[string]string{}
+	json.Unmarshal([]byte(call_arg[0]), &db_set)
+
+	other_set := map[string]string{}
+	json.Unmarshal([]byte(call_arg[1]), &other_set)
+
+	page, _ := strconv.Atoi(other_set["num"])
+	num := 0
+	if page*50 > 0 {
+		num = page*50 - 50
+	}
+
+	db := tool.DB_connect(db_set)
+	if db == nil {
+		return
+	}
+	defer db.Close()
+
+	if other_set["search_type"] == "title" {
+		stmt, err := db.Prepare(tool.DB_change(db_set, "select title from data where title like ? collate nocase order by title limit ?, 50"))
+		if err != nil {
+			return
+		}
+		defer stmt.Close()
+
+		var title string
+		var title_list []string
+
+		rows, err := stmt.Query("%"+other_set["name"]+"%", num)
+		if err != nil {
+			return
+		}
+		defer rows.Close()
+
+		for rows.Next() {
+			err := rows.Scan(&title)
+			if err != nil {
+				return
+			}
+
+			title_list = append(title_list, title)
+		}
+
+		if len(title_list) == 0 {
+			fmt.Print("{}")
+		} else {
+			json_data, _ := json.Marshal(title_list)
+			fmt.Print(string(json_data))
+		}
+	} else {
+		stmt, err := db.Prepare(tool.DB_change(db_set, "select title from data where data like ? collate nocase order by title limit ?, 50"))
+		if err != nil {
+			return
+		}
+		defer stmt.Close()
+
+		var title string
+		var title_list []string
+
+		rows, err := stmt.Query("%"+other_set["name"]+"%", num)
+		if err != nil {
+			return
+		}
+		defer rows.Close()
+
+		for rows.Next() {
+			err := rows.Scan(&title)
+			if err != nil {
+				return
+			}
+
+			title_list = append(title_list, title)
+		}
+
+		if len(title_list) == 0 {
+			fmt.Print("{}")
+		} else {
+			json_data, _ := json.Marshal(title_list)
+			fmt.Print(string(json_data))
+		}
+	}
+}