def getSchedule(): return_data = {} headers = request.headers username = headers.get("SCHEDULER-USERNAME", None) api_key = headers.get("SCHEDULER-API-KEY", None) db = get_db() db.row_factory = dict_factory if not username: return_data["error"] = "Username is required." return jsonify(return_data) if not api_key: return_data["error"] = "Api key is required." return jsonify(return_data) posting_user = db.execute('SELECT id FROM user WHERE username = ? AND api_key = ?',(username,api_key,)).fetchone() if not posting_user: return_data["error"] = "no such user exists." return jsonify(return_data) schedules = db.execute('SELECT * FROM post WHERE user_id = ?', (posting_user["id"],)).fetchall() # print(schedules) return_data["data"] = schedules return return_data
def getUser(): return_data = {} return_data["data"] = {} data = request.get_json() username = data.get('username', None) password = data.get('password', None) api_key = data.get('api_key', None) db = get_db() if not username: return_data["data"]["error"] = "Username is required." return jsonify(return_data) if not password: if not api_key: return_data["data"]["error"] = "Password/Api Key is required." return jsonify(return_data) user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if not user: return_data["data"]["error"] = "No such user." return_data["data"]["isValid"] = False return jsonify(return_data) if password: if not check_password_hash(user["password"], password): return_data["data"]["error"] = "Invalid user credentials." return_data["data"]["isValid"] = False return_data["data"]["validation_type"] = "password" return jsonify(return_data) return_data["data"] = { "username": user["username"], "api_key": user["api_key"], "isValid": True, "validation_type": "password" } return jsonify(return_data) if user["api_key"] == api_key: return_data["data"] = { "username": user["username"], "api_key": user["api_key"], "isValid": True, "validation_type": "api_key" } return jsonify(return_data) elif not password: return_data["data"]["error"] = "Invalid user credentials." return_data["data"]["isValid"] = False return_data["data"]["validation_type"] = "api_key" return jsonify(return_data)
def load_states(): dbs = get_db() dbs.execute('DELETE FROM state') for state in states: dbs.execute('INSERT INTO state (name)' ' VALUES (?)', [state]) dbs.commit() return redirect('/tasks')
def createSchedule(): return_data = {} headers = request.headers data = request.get_json() username = headers.get("SCHEDULER-USERNAME", None) api_key = headers.get("SCHEDULER-API-KEY", None) media_link = data.get('media_link', None) media_story = data.get('media_story', None) schedule_date = data.get('schedule_date', None) post_details = data.get('post_details', {}) db = get_db() if not media_link: return_data["error"] = "Media Link is required." return jsonify(return_data) if not media_story: return_data["error"] = "Media story is required." return jsonify(return_data) if not username: return_data["error"] = "Username is required." return jsonify(return_data) if not api_key: return_data["error"] = "Api key is required." return jsonify(return_data) if not schedule_date: return_data["error"] = "Schedule date is required." return jsonify(return_data) if post_details: if not isinstance(post_details, dict): return_data["error"] = "Post details should be dict." return jsonify(return_data) posting_user = db.execute('SELECT id FROM user WHERE username = ? AND api_key = ?',(username,api_key,)).fetchone() if not posting_user: return_data["error"] = "no such user exists." return jsonify(return_data) db.execute( 'INSERT INTO post (date, media_link, media_story, user_id, post_details, created, updated) VALUES (?,?,?,?,?,?,?)', (schedule_date, media_link, media_story, posting_user["id"], json.dumps(post_details), datetime.datetime.now(), datetime.datetime.now()) ) db.commit() return_data["status"] = "success" return_data["message"] = "Schedule action added to database.!" return jsonify(return_data)
def createUser(): return_data = {} data = request.get_json() username = data.get('username', None) password = data.get('password', None) email = data.get('email', None) db = get_db() if not username: return_data["error"] = "Username is required." return jsonify(return_data) if db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: return_data["error"] = "username already in use." return jsonify(return_data) if not password: return_data["error"] = "Password is required." return jsonify(return_data) validationResult, validationCondition = validatePassword(password) if not validationResult: return_data["error"] = "password doesn't comply security policy." return_data["condition"] = validationCondition return jsonify(return_data) password = generate_password_hash(password) if not email: return_data["error"] = "Email is required." return jsonify(return_data) if db.execute('SELECT id FROM user WHERE email = ?', (email, )).fetchone() is not None: return_data["error"] = "email already in use." return jsonify(return_data) api_key = secrets.token_urlsafe(64) db.execute( 'INSERT INTO user (username, password, api_key, email, created, updated) VALUES (?,?,?,?,?,?)', (username, password, api_key, email, datetime.datetime.now(), datetime.datetime.now())) db.commit() return "User created!"
def create_processes(): print("Forking every minute.") db = get_db() # db.row_factory = dict_factory posts = db.execute('SELECT * FROM post WHERE post_status =?', ("saved", )).fetchall() for post in posts: try: date = post["date"] if time.tzname[0] == "UTC": date = datetime.strptime(post["date"], '%Y-%m-%d:%H:%M:%S:%z') date = date.astimezone(pytz.UTC) print( f"Date converted to UTC as system timezone is {time.tzname}" ) print(date, datetime.now()) tDiff = int( ((date - datetime.now(timezone.utc)).total_seconds()) / 60) print(tDiff) # if -5 <= tDiff <= 5: if tDiff <= 5: print("placing task.") schedulerProcess = multiprocessing.Process( name="schedulerProcess", target=schedulerProcessFn, args=[post, db]) db.execute('UPDATE post set post_status=? where id=?', ("processing", post["id"])) db.commit() schedulerProcess.start() except Exception as e: post_details = json.loads(post["post_details"]) post_details["error"] = "Task processing failed." post_details["stacktrace"] = f"{e}" db.execute( 'UPDATE post set post_details=?, post_status=? where id=?', (json.dumps(post_details), "failed", post["id"])) db.commit() print("Task processing failed.") print(e)
def tasks(): dbs = get_db() if request.method == 'POST': tracking_id = request.form['tracking_id'] url = request.form['url'] time = request.form['time'] visits = request.form['visits'] date = request.form['date'] dbs.execute( 'INSERT INTO task (tracking_id, url, generating_time, state_name, visits, start_time)' ' VALUES (?, ?, ?, ?, ?, ?)', (tracking_id, url, time, 'NEW', visits, date)) dbs.commit() return redirect('/tasks') else: all_tasks = dbs.execute('SELECT tracking_id, state_name' ' FROM task').fetchall() return render_template('add_task.html', tasks=all_tasks)
def clear_tasks(): dbs = get_db() dbs.execute('DELETE FROM task') dbs.commit() return redirect('/tasks')