<commit_before>from mysql import mysql
from mysql import DB_Exception
from shutil import copyfile
from display_api import get_user_id
from display_api import display_data_type
from PIL import Image
import os
import os.path
import shutil
import bcrypt
import json
import tornado
import time
import random
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import httplib2
from apiclient import discovery
import datetime
#
def add_like_count(db, target_id):
    try:
        sql = ""
        if target_id[0:4]=="imge":
            sql = ("UPDATE image_data SET img_like_count=img_like_count+1 WHERE img_id='" + str(target_id) + "'")
        elif target_id[0:4]=="text":
            sql = ("UPDATE text_data SET text_like_count=text_like_count+1 WHERE text_id='" + str(target_id) + "'")
        else :
            return 0
        db.cmd(sql)
        
        return 1
    except:
        return 0
#
def find_now_schedule(db):
    try:
        sql = "SELECT sche_target_id FROM schedule WHERE sche_is_used=0 ORDER BY sche_sn ASC LIMIT 1"
        pure_result = db.query(sql)
        return str(pure_result[0][0])
    except:
        return 0
#
def add_now_like_count():
    try:
        return_msg = {}
        return_msg["result"] = "fail"
        db = mysql()
        db.connect()

        #find image or text id from current schedule
        target_id = find_now_schedule(db)
        if target_id==0:
            db.close()
            return_msg["error"] = "can not find current schedule"
            return return_msg
            
        #add like count
        if add_like_count(db, target_id)==0:
            db.close()
            return_msg["error"] = "can not add like count"
            return return_msg

        return_msg["result"] = "success"
        return return_msg
                
    except DB_Exception as e:
        db.close()
        return_msg["error"] = e.args[1]
        return return_msg 
    except Exception as e:
        db.close()
        return_msg["error"] = e
        return return_msg
def register_preference(data):
    try:
        db = mysql()
        db.connect()
        inside_type = str(display_data_type(type_name='inside'))
        techOrange_type = str(display_data_type(type_name='techOrange'))
        medium_type = str(display_data_type(type_name='medium'))
        pttBeauty_type = str(display_data_type(type_name='pttBeauty'))
        pttjoke_type = str(display_data_type(type_name='pttjoke'))
        pttStupidClown_type = str(display_data_type(type_name='pttStupidClown'))

        pref_str = ""
        if "all" in data["user_preference"]:
            pref_str = inside_type+" "+medium_type+" "+pttBeauty_type+" "+pttStupidClown_type+" "+pttjoke_type+" "+techOrange_type+" "
        else:
            if "inside" in data["user_preference"]:
                pref_str = pref_str + inside_type + " "
            if "techOrange" in data["user_preference"]:
                pref_str = pref_str + techOrange_type + " "
            if "medium" in data["user_preference"]:
                pref_str = pref_str + medium_type + " "
            if "pttBeauty" in data["user_preference"]:
                pref_str = pref_str + pttBeauty_type + " "
            if "pttjoke" in data["user_preference"]:
                pref_str = pref_str + pttjoke_type + " "
            if "pttStupidClown" in data["user_preference"]:
                pref_str = pref_str + pttStupidClown_type + " "
            if len(pref_str)>0:
                pref_str = pref_str[:-1]

        #generate new id
        sql = ("SELECT pref_id FROM user_prefer ORDER BY pref_set_time DESC LIMIT 1")
        pure_result = db.query(sql)
        pref_id = "pref0000000001"
        try:
            pref_id = "pref" + "{0:010d}".format(int(pure_result[0][0][4:]) + 1)
        except:
            pref_id = "pref0000000001"
        #find user id
        sql = ("SELECT user_id FROM user WHERE user_bluetooth_id='"+str(data["bluetooth_id"])+"'")
        pure_result = db.query(sql)
        #insert user preference
        sql = "INSERT INTO user_prefer" \
        +"(pref_id,user_id,pref_data_type_01,pref_data_type_02,pref_data_type_03,pref_data_type_04,pref_data_type_05) VALUES ('" \
            +pref_id+"', "+str(pure_result[0][0])+",'"+pref_str+"','"+pref_str+"','"+pref_str+"','"+pref_str+"','"+pref_str+"')"
        db.cmd(sql)

        db.close()
        return 1
    except Exception as e:
        print(e)
        db.close()
        return 0
#
def check_bluetooth_id_exist(db, bluetooth_id):
    try:
        sql = "SELECT count(*) FROM user WHERE user_bluetooth_id='"+str(bluetooth_id)+"'"
        pure_result = db.query(sql)
        return int(pure_result[0][0])

    except:
        return -1
#
def check_bluetooth_mode_available():
    file_dir = "setting"
    file_name = "server_setting.txt"
    #check setting file exist
    if not os.path.exists(file_dir):<commit_msg>Fix register perference sql bug<commit_after>from mysql import mysql
from mysql import DB_Exception
from shutil import copyfile
from display_api import get_user_id
from display_api import display_data_type
from PIL import Image
import os
import os.path
import shutil
import bcrypt
import json
import tornado
import time
import random
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import httplib2
from apiclient import discovery
import datetime
#
def add_like_count(db, target_id):
    try:
        sql = ""
        if target_id[0:4]=="imge":
            sql = ("UPDATE image_data SET img_like_count=img_like_count+1 WHERE img_id='" + str(target_id) + "'")
        elif target_id[0:4]=="text":
            sql = ("UPDATE text_data SET text_like_count=text_like_count+1 WHERE text_id='" + str(target_id) + "'")
        else :
            return 0
        db.cmd(sql)
        
        return 1
    except:
        return 0
#
def find_now_schedule(db):
    try:
        sql = "SELECT sche_target_id FROM schedule WHERE sche_is_used=0 ORDER BY sche_sn ASC LIMIT 1"
        pure_result = db.query(sql)
        return str(pure_result[0][0])
    except:
        return 0
#
def add_now_like_count():
    try:
        return_msg = {}
        return_msg["result"] = "fail"
        db = mysql()
        db.connect()

        #find image or text id from current schedule
        target_id = find_now_schedule(db)
        if target_id==0:
            db.close()
            return_msg["error"] = "can not find current schedule"
            return return_msg
            
        #add like count
        if add_like_count(db, target_id)==0:
            db.close()
            return_msg["error"] = "can not add like count"
            return return_msg

        return_msg["result"] = "success"
        return return_msg
                
    except DB_Exception as e:
        db.close()
        return_msg["error"] = e.args[1]
        return return_msg 
    except Exception as e:
        db.close()
        return_msg["error"] = e
        return return_msg
def register_preference(data):
    try:
        db = mysql()
        db.connect()
        inside_type = str(display_data_type(type_name='inside')[0])
        techOrange_type = str(display_data_type(type_name='techOrange')[0])
        medium_type = str(display_data_type(type_name='medium')[0])
        pttBeauty_type = str(display_data_type(type_name='pttBeauty')[0])
        pttjoke_type = str(display_data_type(type_name='pttjoke')[0])
        pttStupidClown_type = str(display_data_type(type_name='pttStupidClown')[0])

        pref_str = ""
        if "all" in data["user_preference"]:
            pref_str = inside_type+" "+medium_type+" "+pttBeauty_type+" "+pttStupidClown_type+" "+pttjoke_type+" "+techOrange_type
        else:
            if "inside" in data["user_preference"]:
                pref_str = pref_str + inside_type + " "
            if "techOrange" in data["user_preference"]:
                pref_str = pref_str + techOrange_type + " "
            if "medium" in data["user_preference"]:
                pref_str = pref_str + medium_type + " "
            if "pttBeauty" in data["user_preference"]:
                pref_str = pref_str + pttBeauty_type + " "
            if "pttjoke" in data["user_preference"]:
                pref_str = pref_str + pttjoke_type + " "
            if "pttStupidClown" in data["user_preference"]:
                pref_str = pref_str + pttStupidClown_type + " "
            if len(pref_str)>0:
                pref_str = pref_str[:-1]

        #generate new id
        sql = ("SELECT pref_id FROM user_prefer ORDER BY pref_set_time DESC LIMIT 1")
        pure_result = db.query(sql)
        pref_id = "pref0000000001"
        try:
            pref_id = "pref" + "{0:010d}".format(int(pure_result[0][0][4:]) + 1)
        except:
            pref_id = "pref0000000001"
        #find user id
        sql = ("SELECT user_id FROM user WHERE user_bluetooth_id='"+str(data["bluetooth_id"])+"'")
        pure_result = db.query(sql)
        #insert user preference
        sql = "INSERT INTO user_prefer" \
        +"(pref_id,user_id,pref_data_type_01,pref_data_type_02,pref_data_type_03,pref_data_type_04,pref_data_type_05) VALUES ('" \
            +pref_id+"', "+str(pure_result[0][0])+",'"+pref_str+"','"+pref_str+"','"+pref_str+"','"+pref_str+"','"+pref_str+"')"
        db.cmd(sql)

        db.close()
        return 1
    except Exception as e:
        db.close()
        return 0
#
def check_bluetooth_id_exist(db, bluetooth_id):
    try:
        sql = "SELECT count(*) FROM user WHERE user_bluetooth_id='"+str(bluetooth_id)+"'"
        pure_result = db.query(sql)
        return int(pure_result[0][0])

    except:
        return -1
#
def check_bluetooth_mode_available():
    file_dir = "setting"
    file_name = "server_setting.txt"
    #check setting file exist
    if not os.path.exists(file_dir):
{'fraction_non_alphanumeric': 0.10994234853848488, 'fraction_numerical': 0.014564579751188429, 'mean_word_length': 2.5126110124333927, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '1721108', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3185, 'n_words': 708}