잉여개발기 (SPDV) 2 years ago
parent
commit
ea84e8bf35
1 changed files with 107 additions and 0 deletions
  1. 107 0
      app.py

+ 107 - 0
app.py

@@ -201,6 +201,112 @@ with get_db_connect() as conn:
 
         conn.commit()
 
+def back_up(data_db_set):
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+    
+        try:
+            curs.execute(db_change('select data from other where name = "back_up"'))
+            back_time = curs.fetchall()
+            back_time = float(number_check(back_time[0][0], True)) if back_time and back_time[0][0] != '' else 0
+
+            curs.execute(db_change('select data from other where name = "backup_count"'))
+            back_up_count = curs.fetchall()
+            back_up_count = int(number_check(back_up_count[0][0])) if back_up_count and back_up_count[0][0] != '' else 3
+
+            if back_time != 0:
+                curs.execute(db_change('select data from other where name = "backup_where"'))
+                back_up_where = curs.fetchall()
+                back_up_where = back_up_where[0][0] if back_up_where and back_up_where[0][0] != '' else data_db_set['name'] + '.db'
+
+                print('Back up state : ' + str(back_time) + ' hours')
+                print('Back up directory : ' + back_up_where)
+                if back_up_count != 0:
+                    print('Back up max number : ' + str(back_up_count))
+
+                    file_dir = os.path.split(back_up_where)[0]
+                    file_dir = '.' if file_dir == '' else file_dir
+                    
+                    file_name = os.path.split(back_up_where)[1]
+                    file_name = re.sub(r'\.db$', '_[0-9]{14}.db', file_name)
+
+                    backup_file = [for_a for for_a in os.listdir(file_dir) if re.search('^' + file_name + '$', for_a)]
+                    backup_file = sorted(backup_file)
+                    
+                    if len(backup_file) >= back_up_count:
+                        remove_dir = os.path.join(file_dir, backup_file[0])
+                        os.remove(remove_dir)
+                        print('Back up : Remove (' + remove_dir + ')')
+
+                now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
+                new_file_name = re.sub(r'\.db$', '_' + now_time + '.db', back_up_where)
+                shutil.copyfile(
+                    data_db_set['name'] + '.db', 
+                    new_file_name
+                )
+
+                print('Back up : OK (' + new_file_name + ')')
+            else:
+                print('Back up state : Turn off')
+
+                back_time = 1
+        except Exception as e:
+            print('Back up : Error')
+            print(e)
+
+            back_time = 1
+
+        threading.Timer(60 * 60 * back_time, back_up, [data_db_set]).start()
+
+def do_ban_end():
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+
+        curs.execute(db_change("update rb set ongoing = '' where end < ? and end != '' and ongoing = '1'"), [get_time()])
+        conn.commit()
+
+        threading.Timer(60, do_ban_end).start()
+
+def do_vote_end():
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+    
+        curs.execute(db_change('select id, type from vote where type = "open" or type = "n_open"'))
+        for for_a in curs.fetchall():
+            curs.execute(db_change('select data from vote where id = ? and name = "end_date" and type = "option"'), [for_a[0]])
+            db_data = curs.fetchall()
+            if db_data:
+                time_db = db_data[0][0].split()[0]
+                time_today = get_time().split()[0]
+
+                if time_today > time_db:
+                    curs.execute(db_change("update vote set type = ? where user = '' and id = ? and type = ?"), ['close' if for_a[1] == 'open' else 'n_close', for_a[0], for_a[1]])
+
+        conn.commit()
+
+        threading.Timer(60 * 60 * 24, do_ban_end).start()
+
+def do_make_sitemap():
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+
+        curs.execute(db_change('select data from other where name = "sitemap_auto_make"'))
+        db_data = curs.fetchall()
+        if db_data and db_data[0][0] != '':
+            main_setting_sitemap(1)
+
+            print('Make sitemap')
+
+        threading.Timer(60 * 60 * 24, do_make_sitemap).start()
+
+def auto_do_something(data_db_set):
+    if data_db_set['type'] == 'sqlite':
+        back_up(data_db_set)
+
+    do_ban_end()
+    do_vote_end()
+    do_make_sitemap()
+
 auto_do_something(data_db_set)
 
 print('Now running... http://localhost:' + server_set['port'])
@@ -593,6 +699,7 @@ app.route('/setting/robot', methods = ['POST', 'GET'])(main_setting_robot)
 app.route('/setting/external', methods = ['POST', 'GET'])(main_setting_external)
 app.route('/setting/acl', methods = ['POST', 'GET'])(main_setting_acl)
 app.route('/setting/sitemap', methods = ['POST', 'GET'])(main_setting_sitemap)
+app.route('/setting/sitemap_set', methods = ['POST', 'GET'])(main_setting_sitemap_set)
 app.route('/setting/skin_set', methods = ['POST', 'GET'])(main_setting_skin_set)
 
 app.route('/easter_egg')(main_func_easter_egg)