2du il y a 5 ans
Parent
commit
a77d37eaca
2 fichiers modifiés avec 124 ajouts et 97 suppressions
  1. 107 96
      emergency_tool.py
  2. 17 1
      route/api_topic_sub.py

+ 107 - 96
emergency_tool.py

@@ -2,117 +2,127 @@
 import time
 from route.tool.func import *
 
-# DB
 while 1:
-    try:
-        set_data = json.loads(open('data/set.json', encoding = 'utf8').read())
-        if not 'db_type' in set_data:
-            try:
-                os.remove('data/set.json')
-            except:
-                print('Please delete set.json')
-                print('----')
-                raise
-        else:
-            print('DB name : ' + set_data['db'])
-            print('DB type : ' + set_data['db_type'])
+    print('Load DB (Y) [Y, N] : ', end = '')
+    data_db_load = input()
+    if data_db_load in ['Y', 'N']:
+        break
+
+if data_db_load == 'Y':
+    # DB
+    while 1:
+        try:
+            set_data = json.loads(open('data/set.json', encoding = 'utf8').read())
+            if not 'db_type' in set_data:
+                try:
+                    os.remove('data/set.json')
+                except:
+                    print('Please delete set.json')
+                    print('----')
+                    raise
+            else:
+                print('DB name : ' + set_data['db'])
+                print('DB type : ' + set_data['db_type'])
 
-            break
-    except:
-        if os.getenv('NAMU_DB') != None or os.getenv('NAMU_DB_TYPE') != None:
-            set_data = {
-                "db" : os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data',
-                "db_type" : os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
-            }
+                break
+        except:
+            if os.getenv('NAMU_DB') != None or os.getenv('NAMU_DB_TYPE') != None:
+                set_data = {
+                    "db" : os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data',
+                    "db_type" : os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
+                }
 
-            print('DB name : ' + set_data['db'])
-            print('DB type : ' + set_data['db_type'])
+                print('DB name : ' + set_data['db'])
+                print('DB type : ' + set_data['db_type'])
 
-            break
-        else:
-            new_json = ['', '']
-            normal_db_type = ['sqlite', 'mysql']
-
-            print('DB type (sqlite) [sqlite, mysql] : ', end = '')
-            new_json[0] = str(input())
-            if new_json[0] == '' or not new_json[0] in normal_db_type:
-                new_json[0] = 'sqlite'
-
-            all_src = []
-            for i_data in os.listdir("."):
-                f_src = re.search(r"(.+)\.db$", i_data)
-                if f_src:
-                    all_src += [f_src.group(1)]
-
-            if all_src != [] and new_json[0] != 'mysql':
-                print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
+                break
             else:
-                print('DB name (data) : ', end = '')
+                new_json = ['', '']
+                normal_db_type = ['sqlite', 'mysql']
 
-            new_json[1] = str(input())
-            if new_json[1] == '':
-                new_json[1] = 'data'
+                print('DB type (sqlite) [sqlite, mysql] : ', end = '')
+                new_json[0] = str(input())
+                if new_json[0] == '' or not new_json[0] in normal_db_type:
+                    new_json[0] = 'sqlite'
 
-            with open('data/set.json', 'w', encoding = 'utf8') as f:
-                f.write('{ "db" : "' + new_json[1] + '", "db_type" : "' + new_json[0] + '" }')
+                all_src = []
+                for i_data in os.listdir("."):
+                    f_src = re.search(r"(.+)\.db$", i_data)
+                    if f_src:
+                        all_src += [f_src.group(1)]
 
-            set_data = json.loads(open('data/set.json', encoding = 'utf8').read())
+                if all_src != [] and new_json[0] != 'mysql':
+                    print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
+                else:
+                    print('DB name (data) : ', end = '')
 
-            break
+                new_json[1] = str(input())
+                if new_json[1] == '':
+                    new_json[1] = 'data'
 
-db_data_get(set_data['db_type'])
+                with open('data/set.json', 'w', encoding = 'utf8') as f:
+                    f.write('{ "db" : "' + new_json[1] + '", "db_type" : "' + new_json[0] + '" }')
 
-if set_data['db_type'] == 'mysql':
-    try:
-        set_data_mysql = json.loads(open('data/mysql.json', encoding = 'utf8').read())
-    except:
-        new_json = {}
+                set_data = json.loads(open('data/set.json', encoding = 'utf8').read())
 
-        while 1:
-            print('DB user ID : ', end = '')
-            new_json['user'] = str(input())
-            if new_json['user'] != '':
                 break
 
-        while 1:
-            print('DB password : ', end = '')
-            new_json['password'] = str(input())
-            if new_json['password'] != '':
-                break
-                
-        print('DB host (localhost) : ', end = '')
-        new_json['host'] = str(input())
-        new_json['host'] = 'localhost' if new_json['host'] == '' else new_json['host']
-
-        print('DB port (3306) : ', end = '')
-        new_json['port'] = str(input())
-        new_json['port'] = '3306' if new_json['port'] == '' else new_json['port']
-
-        with open('data/mysql.json', 'w', encoding = 'utf8') as f:
-            f.write(json.dumps(new_json))
-
-        set_data_mysql = new_json
-
-    conn = pymysql.connect(
-        host = set_data_mysql['host'] if 'host' in set_data_mysql else 'localhost',
-        user = set_data_mysql['user'],
-        password = set_data_mysql['password'],
-        charset = 'utf8mb4',
-        port = int(set_data_mysql['port']) if 'port' in set_data_mysql else 3306
-    )
-    curs = conn.cursor()
-
-    try:
-        curs.execute(db_change('create database ? default character set utf8mb4;')%pymysql.escape_string(set_data['db']))
-    except:
-        pass
+    db_data_get(set_data['db_type'])
+
+    if set_data['db_type'] == 'mysql':
+        try:
+            set_data_mysql = json.loads(open('data/mysql.json', encoding = 'utf8').read())
+        except:
+            new_json = {}
+
+            while 1:
+                print('DB user ID : ', end = '')
+                new_json['user'] = str(input())
+                if new_json['user'] != '':
+                    break
+
+            while 1:
+                print('DB password : ', end = '')
+                new_json['password'] = str(input())
+                if new_json['password'] != '':
+                    break
+
+            print('DB host (localhost) : ', end = '')
+            new_json['host'] = str(input())
+            new_json['host'] = 'localhost' if new_json['host'] == '' else new_json['host']
+
+            print('DB port (3306) : ', end = '')
+            new_json['port'] = str(input())
+            new_json['port'] = '3306' if new_json['port'] == '' else new_json['port']
+
+            with open('data/mysql.json', 'w', encoding = 'utf8') as f:
+                f.write(json.dumps(new_json))
+
+            set_data_mysql = new_json
+
+        conn = pymysql.connect(
+            host = set_data_mysql['host'] if 'host' in set_data_mysql else 'localhost',
+            user = set_data_mysql['user'],
+            password = set_data_mysql['password'],
+            charset = 'utf8mb4',
+            port = int(set_data_mysql['port']) if 'port' in set_data_mysql else 3306
+        )
+        curs = conn.cursor()
+
+        try:
+            curs.execute(db_change('create database ' + set_data['db'] + ' default character set utf8mb4;'))
+        except:
+            pass
+
+        conn.select_db(set_data['db'])
+    else:
+        conn = sqlite3.connect(set_data['db'] + '.db')
+        curs = conn.cursor()
 
-    curs.execute(db_change('use ?')%pymysql.escape_string(set_data['db']))
+    load_conn(conn)
 else:
-    conn = sqlite3.connect(set_data['db'] + '.db')
-    curs = conn.cursor()
-
-load_conn(conn)
+    print('----')
+    print('You can use [9, 11]')
 
 # Main
 print('----')
@@ -297,7 +307,8 @@ else:
     if curs.fetchall():
         curs.execute(db_change("update user_set set data = '' where name = '2fa' and id = ?"), [user_name])
 
-conn.commit()
+if data_db_load == 'Y':
+    conn.commit()
 
 print('----')
 print('OK')

+ 17 - 1
route/api_topic_sub.py

@@ -33,6 +33,22 @@ def api_topic_sub_2(conn, topic_num):
         ip_a_2 = ip_pas([i[3] for i in data], 1)
         for i in data:
             data_v = i[1] if i[4] != 'O' or admin == 1 else ''
+            
+            data_r = render_set(data = data_v, num = 2, include = 'topic_' + i[0], acl = get_acl)
+            data_r_html = data_r[0]
+            data_r_js = data_r[1]
+            
+            data_r_html = re.sub(
+                r'<topic_a>((?:(?!<\/topic_a>).)+)<\/topic_a>', 
+                '<a href="\g<1>">\g<1></a>', 
+                data_r_html
+            )
+            data_r_html = re.sub(
+                r'&lt;topic_call&gt;@((?:(?!&lt;\/topic_call&gt;).)+)&lt;\/topic_call&gt;', 
+                '<a href="/w/user:\g<1>">@\g<1></a>', 
+                data_r_html
+            )
+            
             data_a[i[0]] = {
                 "data" : data_v,
                 "date" : i[2],
@@ -40,7 +56,7 @@ def api_topic_sub_2(conn, topic_num):
                 "blind" : i[4],
                 
                 "ip_pas" : ip_a[i[3]],
-                "data_pas" : render_set(data = data_v, num = 2, include = 'topic_' + i[0], acl = get_acl)
+                "data_pas" : [data_r_html, data_r_js]
             }
 
         return flask.jsonify(data_a)