def clean_files(dbfile, expire=86400): with JsonDB(dbfile=dbfile) as db: if db.lock_error: LOG.warning('Cant clean files') return False for k, v in list(db.db.iteritems()): if int(db.db[k]['timestamp']) < int(time.time() - int(expire)): remove_file(db=db, file_id=k)
def check_holiday(): holidays = JsonDB("data/holidays.json").dictionary current_local_time = time.localtime() try: return holidays["{}.{}".format(current_local_time[1], current_local_time[2])] except KeyError: return "Today there is no holiday("
def db_purge(dbfile): """If a file is not present but present in db this function will clean it from the db""" with JsonDB(dbfile=dbfile) as db: for k, v in db.db.items(): elt = db.db[k]['storage_full_filename'] if not os.path.exists(elt): LOG.info("%s present in db but doesn't exist" % elt) db.delete(k)
def upload_file(request, config): value_burn_after_read = strtobool(request.form.get('burn', 'false')) if value_burn_after_read: burn_after_read = True else: burn_after_read = False # Write tmp file on disk try: file_md5, tmp_full_filename = utils.write_tmpfile_to_disk( file=request.files['file'], dest_dir=config['TMP_FOLDER']) except IOError: return 'Server error, contact administrator\n' secure_name = secure_filename(request.files['file'].filename) with JsonDB(dbfile=config['FILE_LIST']) as db: # Just inform for debug purpose if db.lock_error: LOG.error("Unable to get lock during file upload %s" % file_md5) # Try to write file on disk and db. Return false if file is not writed storage_full_filename = os.path.join(config['UPLOAD_FOLDER'], file_md5) mime_type = magic.from_file(tmp_full_filename, mime=True) _type = magic.from_file(tmp_full_filename) succed_add_file = add_new_file(filename=secure_name, source=tmp_full_filename, dest=storage_full_filename, mime_type=mime_type, type=_type, db=db, md5=file_md5, burn_after_read=burn_after_read) if not succed_add_file: # In the case the file is not in db, we have 2 reason : # * We was not able to have the lock and write the file in the db. # * Or an error occure during the file processing # In any case just tell the user to try later try: os.remove(tmp_full_filename) except OSError as e: LOG.error("Can't remove tmp file: %s" % e) LOG.info('Unable lock the db and find' 'the file %s in db during upload' % file_md5) return 'Unable to upload the file, try again later ...\n' LOG.info("[POST] Client %s has successfully uploaded: %s (%s)" % (request.remote_addr, storage_full_filename, file_md5)) return "%s/%s\n" % (utils.build_base_url(env=request.environ), file_md5)
def get_all_files(request, config): # Open JsonDB for read only db = JsonDB(dbfile=config['FILE_LIST'], logger=config['LOGGER_NAME']) db.load() files_list_infos = {} for k, v in db.db.iteritems(): _infos = get_file_info(id_file=k, config=config, env=request.environ) if not _infos: continue files_list_infos[k] = _infos return files_list_infos
def check_holiday(): holidays = JsonDB("holidays.json").dictionary current_local_time = time.localtime() result = "Today there is no holiday(" for date in holidays.keys(): if date.split(".") == [ str(current_local_time[1]), str(current_local_time[2]) ]: result = holidays[date] break return result
def delete_file(request, id_file, dbfile): with JsonDB(dbfile=dbfile) as db: if db.lock_error: return "Lock timed out\n" if id_file not in db.db: return abort(404) if not remove_file(db=db, file_id=id_file): return 'Unable to delete file %s\n' % id_file LOG.info("[DELETE] Client %s has deleted: %s" % (request.remote_addr, id_file)) return "File %s deleted\n" % id_file
def bot_processor(delay): global lock db = JsonDB("db.json") bot = Bot(db.dictionary["token"]) while True: lock.acquire() messages = bot.get_last_messages() for message in messages: if round(time.time() ) - message["date"] <= db.dictionary["max_time_diff"]: try: incoming_message = { "text": message["text"], "chat_id": message["chat"]["id"] } # some messages have not text (stickers, files etc) except: continue outgoing_message = message_handler(incoming_message) if outgoing_message is None: continue elif outgoing_message["method"] == "send_message": bot.send_message(outgoing_message["chat_id"], outgoing_message["text"]) elif outgoing_message["method"] == "send_location": bot.send_location(outgoing_message["chat_id"], outgoing_message["coordinates"]) elif outgoing_message["method"] == "send_photo": bot.send_file(outgoing_message["chat_id"], outgoing_message["photo"], "photo", outgoing_message["caption"]) elif outgoing_message["method"] == "send_audio": bot.send_file(outgoing_message["chat_id"], outgoing_message["audio"], "audio") elif outgoing_message["method"] == "send_document": if outgoing_message["caption"].startswith("Log"): if outgoing_message["chat_id"] == bot.admin_id: bot.send_file(bot.admin_id, open(bot.log_file, "rb"), "document", outgoing_message["caption"]) else: bot.send_message( bot.admin_id, "Unresolved attempt to access to log file from {}" .format(outgoing_message["chat_id"])) else: pass db.dictionary["last_checked_update_id"] = bot.last_checked_update_id db.write() lock.release() time.sleep(delay)
def index(): print(request.form) # if request.method == 'GET': db = JsonDB() db.load() print(request.form) if request.method == 'POST': db['todos'].append({ 'text': request.form['text'], 'priority': request.form['priority'] }) db.save() return render_template("index.html", todo=db['todos'])
def create(): # print(request.method) # print(request.form) db = JsonDB() db.load() if request.method == 'POST': db['contacts'].append({ 'first_name': request.form['first_name'], 'last_name': request.form['last_name'], 'phone': request.form['phone'], 'email': request.form['email'], 'blood_type': request.form['blood_type'], 'favorite_color': request.form['favorite_color'] }) db.save() return redirect('/') return render_template('create.html', blood_types=db['blood_types'])
def get_file(request, id_file, config): # First, open JsonDB for read-only db = JsonDB(dbfile=config['FILE_LIST']) db.load() if id_file not in db.db or db.db[id_file]['burn_after_read'] == 'Burned': return abort(404) if db.db[id_file]['burn_after_read'] == 'True': # Now, try to lock the db if not db._lock(): return "Can't lock db for burning file" db.db[id_file]['burn_after_read'] = 'Burned' db.save() db._release() filename = os.path.basename(db.db[id_file]['storage_full_filename']) LOG.info("[GET] Client %s has requested: %s (%s)" % (request.remote_addr, db.db[id_file]['real_name'], id_file)) if not os.path.isabs(config['UPLOAD_FOLDER']): path = "%s/%s" % (os.path.dirname(config['instance_path']), config['UPLOAD_FOLDER']) else: path = config['UPLOAD_FOLDER'] # If the user agent is in the display list, # format headers to direct display feature if request.user_agent.browser in config['DISPLAY_FOR']: return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file] ['real_name']) # Else keep the regular send file return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file]['real_name'], as_attachment=True)
from jsondb import JsonDB locations_list = JsonDB("data/locations.json")["locations"] def get_coordinates(location_name): for location in locations_list: if location["name"] == location_name: return location["coordinates"] return {"latitude": 0.0, "longitude": 0.0}
def index(): db = JsonDB() db.load() # print(db['contacts']) return render_template('index.html', contacts=db['contacts'])
import random from jsondb import JsonDB lootbox_db = JsonDB("data/lootbox.json") def usual_lootbox(): return random.choice(lootbox_db["usual_items"]) def weapon_lootbox(): def get_random(d): weights_sum = sum([d[key] for key in d.keys()]) res = random.random() * weights_sum lower_bound = 0 for key in d.keys(): if lower_bound <= res < lower_bound + d[key]: return key lower_bound += d[key] return get_random(lootbox_db["weapon"]["name"]) + " | " + get_random( lootbox_db["weapon"]["style"])
def get_infos_file_from_md5(md5, dbfile): # Open JsonDB for read only db = JsonDB(dbfile=dbfile) db.load() return db.read(md5)
from flask import Flask, render_template, request, redirect from jsondb import JsonDB # create a new flask app app = Flask('todo') # class which reads and writes from a .json file db = JsonDB('./db.json') db.load() # index page which renders a template with todo items @app.route('/') def index(): return render_template('index.html', todos=db.data['todos']) # a view which receives a form submission # and saves a todo item to the database @app.route('/addtodo/', methods=['GET', 'POST']) def addtodo(): print(request.form) todo = {'text': request.form['text'], 'priority': request.form['priority']} db.data['todos'].append(todo) db.save() return redirect('/') app.run()
def message_handler(incoming_message): global shadow_db # default / and ? commands from commands.json commands = JsonDB("commands.json")["commands"] # quotes for bot's random quote feature quotes = JsonDB("data/quotes.json")["quotes"] result = { "method": "send_message", "chat_id": incoming_message["chat_id"], "text": "?" } for command_name in commands.keys(): if incoming_message["text"] == command_name: result["text"] = commands[command_name] # random quote feature if incoming_message["text"] == "/quote": result["text"] = random.choice(quotes) # lootboxes feature if incoming_message["text"] == "/lootbox": result["text"] = usual_lootbox() if incoming_message["text"] == "/weapon_lootbox": result["text"] = weapon_lootbox() # throwing dice feature if incoming_message["text"].startswith("/dice"): result["text"] = throw_dice(incoming_message["text"]) # random choice feature if incoming_message["text"].startswith("/random"): try: random_result = random.choice( incoming_message["text"].split(" ")[1:]) except: random_result = "Type command correctly" finally: result["text"] = random_result # days until newyear or summer feature if incoming_message["text"] == "/newyear": result["text"] = days_until_newyear() if incoming_message["text"] == "/summer": result["text"] = days_until_summer() # locations feature if incoming_message["text"].startswith("/where"): try: location = incoming_message["text"].split(" ")[1] result = { "method": "send_location", "coordinates": get_coordinates(location), "chat_id": incoming_message["chat_id"] } except: result = { "method": "send_message", "chat_id": incoming_message["chat_id"], "text": "Type command correctly" } if incoming_message["text"].startswith( "/location") and incoming_message["text"] != "/locations": try: location = incoming_message["text"].split(" ")[1:] result = { "method": "send_location", "coordinates": { "latitude": float(location[0]), "longitude": float(location[1]) }, "chat_id": incoming_message["chat_id"] } except: result = { "method": "send_message", "chat_id": incoming_message["chat_id"], "text": "Type command correctly" } # chat id getter if incoming_message["text"] == "/chat_id": result["text"] = incoming_message["chat_id"] # unix time feature if incoming_message["text"] == "/unix_time": result["text"] = "{} seconds since 00:00:00 1 January 1970".format( str(round(time.time()))) # holiday feature if incoming_message["text"] == "/holiday": result["text"] = check_holiday() # rgb feature if incoming_message["text"].startswith("/rgb"): try: rgb = [ int(color) for color in incoming_message["text"].split(" ")[1:] ] rgb = tuple(rgb) if len(rgb) != 3: raise ValueError except: rgb = (255, 255, 255) finally: result = { "method": "send_photo", "photo": open(show_color_rgb(rgb), "rb"), "caption": "Red - {}, Green - {}, Blue - {}".format( rgb[0], rgb[1], rgb[2]), "chat_id": incoming_message["chat_id"] } # drop log file feature if incoming_message["text"] == "/droplog": result = { "method": "send_document", "caption": "Log", "chat_id": incoming_message["chat_id"] } if "text" in result.keys(): if result["text"] == "?": result = None return result
elif outgoing_message["method"] == "send_document": if outgoing_message["caption"].startswith("Log"): if outgoing_message["chat_id"] == bot.admin_id: bot.send_file(bot.admin_id, open(bot.log_file, "rb"), "document", outgoing_message["caption"]) else: bot.send_message( bot.admin_id, "Unresolved attempt to access to log file from {}" .format(outgoing_message["chat_id"])) else: pass db["last_checked_update_id"] = bot.last_checked_update_id db.write() lock.release() time.sleep(delay) if __name__ == '__main__': db = JsonDB("db.json") lock = multiprocessing.Lock() manager = multiprocessing.Manager() shadow_db = manager.dict() bot_process = multiprocessing.Process(target=bot_processor, args=(db["delays"]["bot"], )) bot_process.start() bot_process.join()
from jsondb import JsonDB db = JsonDB("ExampleDB.json") print(db.show_tables()) print(db.get_cols("Users")) print(db.select("Users")) print(db.select("Users", col="ID")) print(db.select("Users", col="User")) print(db.select("Users", where={"ID": 0})) print(db.select("Users", where={"Pass": "******"})) print(db.select("Users", col="User", where={"Pass": "******"})) input("\n--Enter_to_Continue--")
def usual_lootbox(): return random.choice(JsonDB("lootbox.json").dictionary["usual_items"])
import multiprocessing import time import random import images from telegram_bot import Bot from dice import throw_dice import parser import digest import locations from jsondb import JsonDB from lootbox import usual_lootbox # delays determine how often processes run delays = JsonDB("delays.json").dictionary["delays"] # default / and ? commands from commands.json commands = JsonDB("commands.json").dictionary["commands"] # quotes for bot's random quote feature quotes = JsonDB("quotes.json").dictionary["quotes"] # this function update parser's data def update_parser(delay): global shadow_db, lock while True: lock.acquire() for c_name in parser.cc_chart_currencies.keys(): shadow_db[c_name] = parser.cc_chart_price_usd(c_name) lock.release() time.sleep(delay)
from jsondb import JsonDB locations_list = JsonDB("locations.json").dictionary["locations"] def get_coordinates(location_name): for location in locations_list: if location["name"] == location_name: return location["coordinates"] return {"latitude": 0.0, "longitude": 0.0}
def delete(index): db = JsonDB() db.load() db['contacts'].pop(index) db.save() return redirect('/')