优化拼接查询
This commit is contained in:
		
							
								
								
									
										43
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -208,6 +208,12 @@ func main() {
 | 
			
		||||
 | 
			
		||||
	// 屏蔽 /favicon.ico
 | 
			
		||||
	http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
		http.Error(w, "Not Found", http.StatusNotFound)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/api/default", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
		http.Error(w, "Not Found", http.StatusNotFound)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
@@ -264,7 +270,7 @@ func main() {
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// 获取图片信息列表(分页)
 | 
			
		||||
	http.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	http.HandleFunc("/api/images", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
 | 
			
		||||
		// 私域: (自己的图片, 自己的文章, 自己的精选集, 点赞收藏精选集)
 | 
			
		||||
@@ -284,19 +290,17 @@ func main() {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 拼接查询条件, 超级简洁写法
 | 
			
		||||
		conditions := ""
 | 
			
		||||
		if authors := QueryConditions("authors"); len(authors) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND author IN (%s)", strings.Join(authors, ","))
 | 
			
		||||
		var addCondition = func(conditions *strings.Builder, key, column string) {
 | 
			
		||||
			if values := QueryConditions(key); len(values) > 0 {
 | 
			
		||||
				conditions.WriteString(fmt.Sprintf(" AND %s IN (%s)", column, strings.Join(values, ",")))
 | 
			
		||||
			}
 | 
			
		||||
		if tags := QueryConditions("tags"); len(tags) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND tag IN (%s)", strings.Join(tags, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if categories := QueryConditions("categories"); len(categories) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND categorie IN (%s)", strings.Join(categories, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if sets := QueryConditions("sets"); len(sets) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND sets IN (%s)", strings.Join(sets, ","))
 | 
			
		||||
		}
 | 
			
		||||
		var conditions strings.Builder
 | 
			
		||||
		addCondition(&conditions, "authors", "author")
 | 
			
		||||
		addCondition(&conditions, "tags", "tag")
 | 
			
		||||
		addCondition(&conditions, "categories", "categorie")
 | 
			
		||||
		addCondition(&conditions, "sets", "sets")
 | 
			
		||||
 | 
			
		||||
		var ids []int64
 | 
			
		||||
		if similar := QueryConditions("similar"); len(similar) > 0 {
 | 
			
		||||
			id, err := strconv.Atoi(strings.Trim(similar[0], "'"))
 | 
			
		||||
@@ -311,19 +315,20 @@ func main() {
 | 
			
		||||
				idsStr[i] = strconv.FormatInt(v, 10)
 | 
			
		||||
			}
 | 
			
		||||
			if len(idsStr) > 0 {
 | 
			
		||||
				conditions += fmt.Sprintf(" AND id IN (%s)", strings.Join(idsStr, ",")) // 拼接查询条件
 | 
			
		||||
				conditions.WriteString(fmt.Sprintf(" AND id IN (%s)", strings.Join(idsStr, ","))) // 拼接查询条件
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if conditions != "" {
 | 
			
		||||
			conditions = strings.Replace(conditions, " AND", "", 1) // 去掉第一个 AND
 | 
			
		||||
			conditions = " WHERE" + conditions                      // 拼接 WHERE
 | 
			
		||||
		if conditions.Len() > 0 {
 | 
			
		||||
			conditionsStr := conditions.String()
 | 
			
		||||
			conditionsStr = strings.Replace(conditionsStr, " AND", "", 1) // 去掉第一个 AND
 | 
			
		||||
			conditionsStr = " WHERE" + conditionsStr                      // 拼接 WHERE
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 获取图片列表
 | 
			
		||||
		var images ListView
 | 
			
		||||
		var image_list []Image
 | 
			
		||||
		images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
 | 
			
		||||
		rows, err := mysqlConnection.Database.Query("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images"+conditions+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
 | 
			
		||||
		rows, err := mysqlConnection.Database.Query("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images"+conditions.String()+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取图片列表失败", err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
@@ -431,7 +436,7 @@ func main() {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 获取总数
 | 
			
		||||
		err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions).Scan(&images.Total)
 | 
			
		||||
		err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取图片总数失败", err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
@@ -614,7 +619,7 @@ func main() {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Println("safeParam", safeParam)
 | 
			
		||||
		//fmt.Println("safeParam", safeParam)
 | 
			
		||||
		urls, err := models.GetVideoM3U8(safeParam)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取视频链接失败", err)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										68
									
								
								bin/ocr.py
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								bin/ocr.py
									
									
									
									
									
								
							@@ -1,68 +0,0 @@
 | 
			
		||||
# python 實現圖片文字辨識(ocr)
 | 
			
		||||
 | 
			
		||||
import pytesseract
 | 
			
		||||
from PIL import Image
 | 
			
		||||
 | 
			
		||||
# 設置tesseract執行文件的路徑(linux)
 | 
			
		||||
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
 | 
			
		||||
 | 
			
		||||
# 設置提取中文和英文
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 讀取圖片
 | 
			
		||||
image = Image.open('data/tt.jpeg')
 | 
			
		||||
 | 
			
		||||
# 圖片轉為灰度圖
 | 
			
		||||
image = image.convert('L')
 | 
			
		||||
 | 
			
		||||
# 圖片二值化
 | 
			
		||||
threshold = 127
 | 
			
		||||
table = []
 | 
			
		||||
for i in range(256):
 | 
			
		||||
    if i < threshold:
 | 
			
		||||
        table.append(0)
 | 
			
		||||
    else:
 | 
			
		||||
        table.append(1)
 | 
			
		||||
image = image.point(table, '1')
 | 
			
		||||
 | 
			
		||||
# 圖片轉為字符串
 | 
			
		||||
#text = pytesseract.image_to_string(image, lang='chi_sim+eng')
 | 
			
		||||
#print(text)
 | 
			
		||||
#
 | 
			
		||||
#data = pytesseract.image_to_boxes(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
# 座標(全數據)
 | 
			
		||||
#data = pytesseract.image_to_data(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
data = pytesseract.image_to_alto_xml(image, lang='chi_sim+eng')
 | 
			
		||||
#print(data)
 | 
			
		||||
 | 
			
		||||
## 打印格式化的XML
 | 
			
		||||
#from xml.dom.minidom import parseString
 | 
			
		||||
#dom = parseString(data)
 | 
			
		||||
#print(dom.toprettyxml())
 | 
			
		||||
 | 
			
		||||
# 打印格式化的JSON
 | 
			
		||||
import json
 | 
			
		||||
from xmljson import badgerfish as bf
 | 
			
		||||
from xml.etree.ElementTree import fromstring
 | 
			
		||||
 | 
			
		||||
# 過濾掉@CONTENT爲空的數據和爲數字的數據
 | 
			
		||||
def filter_data(data):
 | 
			
		||||
    if isinstance(data, dict):
 | 
			
		||||
        if '@CONTENT' in data:
 | 
			
		||||
            if data['@CONTENT'] == '' or data['@CONTENT'].isdigit():
 | 
			
		||||
                return None
 | 
			
		||||
        for k, v in data.items():
 | 
			
		||||
            if isinstance(v, dict):
 | 
			
		||||
                data[k] = filter_data(v)
 | 
			
		||||
            elif isinstance(v, list):
 | 
			
		||||
                data[k] = [filter_data(i) for i in v]
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
xml = fromstring(data)
 | 
			
		||||
json = json.dumps(bf.data(xml), indent=4, ensure_ascii=False)
 | 
			
		||||
print(json)
 | 
			
		||||
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
import json
 | 
			
		||||
import uuid
 | 
			
		||||
import towhee
 | 
			
		||||
 | 
			
		||||
from http.server import BaseHTTPRequestHandler, HTTPServer
 | 
			
		||||
@@ -40,19 +39,6 @@ class ResNetServer(BaseHTTPRequestHandler):
 | 
			
		||||
                </html>
 | 
			
		||||
            ''', 'utf-8'))
 | 
			
		||||
 | 
			
		||||
        ## 定義一個處理 POST 請求的方法
 | 
			
		||||
        #def do_POST(self):
 | 
			
		||||
        #    self.send_response(200)                              # 設置服務器響應的狀態碼
 | 
			
		||||
        #    self.send_header('Content-type', 'application/json') # 設置服務器響應的標頭
 | 
			
		||||
        #    self.end_headers()                                   # 完成服務器響應的標頭
 | 
			
		||||
        #    content_length = int(self.headers['Content-Length']) # 獲取請求的內容長度
 | 
			
		||||
        #    body = self.rfile.read(content_length)               # 獲取請求的內容
 | 
			
		||||
        #    params = json.loads(body)                            # 將請求的內容解析為字典
 | 
			
		||||
        #    img_path = params['img_path']                        # 獲取圖像的路徑
 | 
			
		||||
        #    feat = towhee.glob(img_path).image_decode().image_embedding.timm(model_name='resnet50').tensor_normalize().to_list()
 | 
			
		||||
        #    results = json.dumps(feat[0].tolist())               # 將結果轉換為JSON格式
 | 
			
		||||
        #    self.wfile.write(bytes(results, 'utf-8'))
 | 
			
		||||
 | 
			
		||||
        # 定義一個處理 POST 請求的方法, 接收二進制圖像文件
 | 
			
		||||
        def do_POST(self):
 | 
			
		||||
            self.send_response(200)                              # 設置服務器響應的狀態碼
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user