def scraper(): """ Scraper page - allows users to be scrape either an account or a twitter query """ ut.sessiongen(True) if request.method == 'POST': if current_user.credits <= 0: flash("Insufficient Credits to use the scraper.", 'danger') return redirect(url_for('webapp.scraper')) else: query = request.form.get("Query") hdl = request.form.get("Handle") if query is not None: flash("Thanks! We've received your request for scraping // '" +str(query) + "' //. Expect an email from us shortly! 👀", 'info') current_user.credits -= 1 webut.Twint_Scrape_Query.submit(query, current_user) else: r = requests.get('https://twitter.com/users/username_available?username='******'warning') else: flash("Thanks! We've received your request for scraping @" +str(hdl) + ". Expect an email from us shortly! 👀", 'info') current_user.credits -= 1 webut.Twint_Scrape_Account.submit(hdl, current_user) db.session.commit() return redirect(url_for('webapp.scraper')) return render_template("/webapp/scraper.html")
def account(): """ User account page """ ut.sessiongen(True) form = Form_Update_Account() if form.validate_on_submit(): """ Checks email + username are not taken before updating account """ if form.avatar.data: current_user.avatar = userut.save_user_avatar(form.avatar.data) if form.username.data: user = User.query.filter_by(username=form.username.data).first() if user: flash("Failed to update Username - Already Taken!", "danger") else: current_user.username = form.username.data if form.email.data: user = User.query.filter_by(email=form.email.data.lower()).first() if user: flash("Failed to update email - Already in use!", "danger") else: current_user.email = form.email.data.lower() db.session.commit() flash("Account details updated sucessfully!", "success") return redirect(url_for("users.account")) return render_template("user/account.html", form=form)
def aclist(): """ Displays list of scraped Accounts within the database """ ut.sessiongen(True) aclist = webut.Get_Account_List("aclist") return render_template("/webapp/aclist.html", aclist= aclist)
def logout(): """ Logs user out + Removes Token from user session """ ut.sessiongen(False) logout_user() if session["utoken"] != []: session["utoken"] = [] flash("Logged out sucessfully!", 'success') return redirect(url_for('misc.index'))
def verify(): """Requests a verification email - sends link with token to verify account""" ut.sessiongen(False) if current_user.verified == 1: flash("Account Already Verified", 'info') return redirect(url_for("misc.index")) else: user = current_user userut.Email_Account_Verification.submit(user) flash("Expect an email from us shortly! 👀", 'success') return redirect(url_for('misc.index'))
def reset(): """Reset password request - user inserts email & gets email with token""" ut.sessiongen(False) if current_user.is_authenticated: return redirect(url_for('misc.index')) form = Form_Password_Request() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data.lower()).first() userut.Email_Password_Reset.submit(user) flash("Thanks! - Expect an email from us shortly 👀", 'success') return redirect(url_for("misc.index")) return render_template("user/reset.html", title="SA - Login", form=form)
def about(): """ About page of service. """ if ut.sessiongen(True) == False: return redirect(url_for('misc.index')) return render_template("misc/about.html", title="About SA")
def api(): """ Page for API documentation. """ if ut.sessiongen(True) == False: return redirect(url_for('misc.index')) return render_template("misc/api.html")
def search(): """Search page - returns results for either Tweets / Accounts""" ut.sessiongen(True) query = request.args.get('search') query = "%"+ query + "%" param = request.args.get('parameter') if param == "account": data = Author.query.filter(Author.handle.like(query)).all() else: data = Tweet.query.filter(Tweet.text.like(query)).all() return render_template("/webapp/search.html", data=data, param=param)
def verify_confirm(token): """Verifies a user account against a valid email address with token sent earlier + gives 5 sceaping credits""" ut.sessiongen(False) user = User.verify_token(token) if user is None: flash("Invalid or Expired Token", 'warning') return redirect(url_for('users.login')) if current_user.verified == 0: flash("Account verified!", 'success') current_user.credits = 5 current_user.verified = True db.session.commit() else: flash("Already Verified", 'info') return redirect(url_for('misc.index'))
def register(): """account_creation - checks unique constraints and then adds user to db +token etc.""" ut.sessiongen(False) if current_user.is_authenticated: return redirect(url_for('misc.index')) form = Form_Register() if form.validate_on_submit(): dbverification = True user = User.query.filter_by(email=form.email.data.lower()).first() if user: dbverification = False flash("Email already in use - Please enter another and try again", 'danger') user = User.query.filter_by(username=form.username.data).first() if user: dbverification = False flash("username taken - Please choose another and try again", 'danger') if dbverification == True: password_hash = crypt.generate_password_hash( form.password.data).decode('utf-8') tok = secrets.token_urlsafe(30) user = User(username=form.username.data, email=form.email.data.lower(), password_hash=password_hash, token=tok, verified=0, credits=0) db.session.add(user) db.session.commit() userut.Email_Account_Creation.submit(user) flash(f"Registration Sucessful as {form.username.data}!", 'success') session["utoken"].append(user.token) return redirect(url_for('users.login')) return render_template("user/register.html", title="SA - Registration", form=form)
def login(): """Login page + flow for a user.""" ut.sessiongen(False) if current_user.is_authenticated: return redirect(url_for('misc.index')) form = Form_Login() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data.lower()).first() if user and crypt.check_password_hash(user.password_hash, form.password.data): session["utoken"].append(user.token) login_user(user, remember=form.rememberme.data) flash(f"Logged in as {form.email.data.lower()}!", 'success') Forward = request.args.get("next") if Forward: return redirect(Forward) else: return redirect(url_for('misc.index')) else: flash(f"Login Failed - Please check your email and password", 'danger') return render_template("user/login.html", title="SA - Login", form=form)
def reset_confirm(token): """Confirms password reset - checks token and updates password""" ut.sessiongen(False) user = User.verify_token(token) if user: form = Form_Reset_Password() if form.validate_on_submit(): dbverification = True password_hash = crypt.generate_password_hash( form.password.data).decode('utf-8') user.password_hash = password_hash db.session.commit() flash(f"Password updated sucessfully!", 'success') return redirect(url_for('users.login')) elif user is None: flash("Invalid or Expired Token", 'warning') return redirect(url_for("users.reset")) return render_template("user/reset_confirm.html", title="SA - Login", form=form)
def dashboard(): """Main data dashboard - mashup of all data on system + general stats""" if ut.sessiongen(True) == False: return redirect(url_for('misc.index')) CPU, MEM, UserTot, TweetTot, AuthorTot, DataTot = ut.getstats() aclist = webut.Get_Account_List("app") RANDOM = webut.Get_Random_Three("*") TOP3 = webut.Get_Top_Three("*") RECENT3 = webut.Get_Recent_Three("*") return render_template("/webapp/webapp.html",RANDOM=RANDOM, TOP3= TOP3, RECENT3=RECENT3 ,CPU = CPU, MEM = MEM, UserTot = UserTot, TweetTot = TweetTot, AuthorTot = AuthorTot, DataTot = DataTot, ACLIST=aclist)
def stats(): """ Stats page for the service. Serverload + db etc. """ if ut.sessiongen(True) == False: return redirect(url_for('misc.index')) CPU, MEM, UserTot, TweetTot, AuthorTot, DataTot = ut.getstats() return render_template("misc/stats.html", CPU=CPU, MEM=MEM, UserTot=UserTot, TweetTot=TweetTot, AuthorTot=AuthorTot, DataTot=DataTot)
def AuthorTweetsPage(author): """Author Tweets page - allows all tweets of an author to be viewed through the magic of JS""" if ut.sessiongen(True) == False: return redirect(url_for('misc.index')) if author != "*": author = Author.query.filter_by(handle=author).first() if author is None: flash("The requested author does not exist.", 'danger') return redirect(url_for('misc.index')) author = author.handle else: author = "*" return render_template("/webapp/tweetspage.html", Author = author)
def AuthorPage(handle): if ut.sessiongen(True) == False: return redirect(url_for('index')) """Author "dashboard" returns similar data to dashboard but more account specific""" data = Author.query.filter_by(handle=handle).first() if data is None: flash("The requested author does not exist.", 'danger') return redirect(url_for('misc.index')) RANDOM = webut.Get_Random_Three(handle) TOP3 = webut.Get_Top_Three(handle) RECENT3 = webut.Get_Recent_Three(handle) trained=False if handle == "realdonaldtrump": trained = True return render_template("/webapp/author.html",data = data, RANDOM=RANDOM, TOP3= TOP3, RECENT3=RECENT3, trained=trained)
def index(): """ Homepage. """ ut.sessiongen(False) return render_template("index.html")