def run(file_name): """ 这里只限制输出流量 """ db_data = {} with open(file_name, "r") as rf: chain_name = "" for line in rf.readlines(): if line[:5] == "Chain": chain_name = line[6:15] elif chain_name == "ssserver8": mat = re.match("^\s*(\d+)\s*(\d+)\s*ACCEPT.*spt:(\d+).*$", line) if not mat: mat = re.match("^\s*(\d+)\s*(\d+)\s*ACCEPT.*dpt:(\d+).*$", line) if mat: used_flow = int(mat.group(2)) * 1.0 / 1000 / 1000 port = mat.group(3) db_data.setdefault(port, 0) db_data[port] += used_flow print time.ctime() port_flow = {} for port, used_flow in db_data.items(): port_flow[port] = used_flow db.execute( "update user set used_flow=%s where user.user=(select owner from ssserver where port=%s);", used_flow, port) pprint.pprint(port_flow)
def post(self): roomchannel = self.get_secure_cookie("roomId") userid = str(self.get_secure_cookie("userid")) user = db.get("SELECT username FROM user WHERE id=%s", int(userid)) message = str(self.get_argument("message")) msg_type = int(self.get_argument("type")) createtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') data = json_encode({ 'username': user.username, 'msg': message, "userId": userid, "bglong": 0, "type": msg_type, "created_time": createtime }) db.execute( "insert into message(userid, msg, roomid, created_time, type) values (%s, %s, %s, %s, %s)", userid, message, roomchannel, createtime, msg_type) #收到将消息publish到Redis #print data redis_client.connect() redis_client.publish(roomchannel, data) self.write(json_encode({'result': True})) self.finish()
def _get_account_info(self, data_type, with_prefix=True): """ 返回用户该类型的端口密码 """ type_name = {0: "public_", 1: "login_", 2: "donate_"} prefix = with_prefix and type_name[data_type] or "" data = {} if data_type <= 1: data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=%s order by used limit 1;" % (prefix, prefix, data_type)) if data: db.execute("update ssserver set used=used+1 where port=%s", data["%sport" % prefix]) elif data_type == 2: data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=2 and owner=\"%s\" limit 1" % (prefix, prefix, self.get_current_user())) if not data: """ 尝试分配 """ db.execute( "update ssserver set owner=%s where owner is null and type=2 limit 1", self.get_current_user()) data = db.get( "select port as %sport ,password as %spassword from ssserver where state>0 and type=2 and owner=\"%s\" " % (prefix, prefix, self.get_current_user())) return data
def reviews(book_id): """Reviews route with AJAX""" # initialize reviews variable reviews = None # user Post review to server if request.method == "POST": # check if user has already posted a review double_post_query = "SELECT * FROM reviews WHERE user_id = :user_id AND book_id = :book_id" double_post_data = {'user_id': session['user_id'], 'book_id': book_id} result = db.execute(double_post_query, double_post_data).first() print(result) if result is not None: flash("You've already reviewed this book.") return redirect("/books/{}".format(book_id)) # create review and into database query = "INSERT INTO reviews (rating, review, user_id, book_id) VALUES (:rating, :review, :user_id, :book_id)" data = { 'rating': request.form.get("rating"), 'review': request.form.get("review"), 'user_id': session['user_id'], 'book_id': book_id } # prevent errors during async connection try: db.execute(query, data) db.commit() flash("Review Submitted") except Exception as e: flash("Review Submission Failed:") finally: db.close() return redirect("/books/{}".format(book_id)) # update reviews on page if request.method == "GET": # retrieve reviews if any from database if book_id is not None and book_id != "undefined": query = "SELECT * FROM reviews WHERE book_id = :book_id ORDER BY updated_at DESC" data = {'book_id': book_id} # prevent errors during async connection try: reviews = db.execute(query, data).fetchall() finally: db.close() return render_template("partials/review.html", reviews=reviews)
def post(self): money = int(self.get_argument("money", "0")) if money > 0: db.execute( "update user set type=2,donate_money=donate_money+%s,total_flow=2048 where user=%s", money, self.current_user) user_type = self.set_secure_cookie("user_type", "2") my_log.info("donate page : username:%s donate money:%s" % (self.current_user, money)) self.redirect("/")
def get_book_api(isbn): """Return details about requested book""" # check if book is in database query = "SELECT * FROM books WHERE isbn = :isbn" data = {'isbn': isbn} book = db.execute(query, data).first() print(book) # return 404 error if book not found if book is None: return jsonify( {'error': "Something went wrong, check that the isbn is correct"}), 404 # if the book was found if book: # set API to request book review counts api_data = goodread_api(isbn) # return api JSON response return jsonify({ 'title': book.title.rstrip(), 'author': book.author.rstrip(), 'year': str(book.year).rstrip(), 'isbn': isbn, 'review_count': api_data['books'][0]['reviews_count'], 'average_score': api_data['books'][0]['average_rating'] })
def get(self): username = str(self.get_argument("username")) password = str(self.get_argument("password")) user = self.exists_name_user(username) if user: return self.write( "<script>alert('用户名已存在!'); window.location.href='login'</script>" ) if username and password: try: db.execute( "insert into user(username, password) values (%s, %s)", username, password) except: return self.redirect("login") return self.redirect("login") else: return self.write( "<script>alert('必填项不能为空'); window.location.href='login'</script>" )
def raw_insert(cls, sql, *args): """ 执行插入语句 :param sql: insert into 语句 :param args: 插入的参数 :return: 插入成功之后记录的pk >>> sql = "INSERT INTO `{table}` (username) values (%s)".format(table=User.__table__) >>> row = User.raw_insert(sql, 'admin') """ lastrowid = conn.execute(sql, *args) return lastrowid
def post(self): user = self.get_argument("user") password = self.get_argument("password").lower() err_code = 0 user_type = 0 if EMAIL_PAT.match(user) or PHONE_PAT.match(user): # password=hashlib.sha256(password).hexdigest() if not re.match("^\w+$", password): err_code = 1003 # old user elif db.query("select * from user where user=%s", user): result = db.get( "select type from user where user=%s and password=%s limit 1", user, password) if result: user_type = result["type"] my_log.info("login page : login success user:%s" % user) else: err_code = 1001 my_log.info("login page : password error user:%s" % user) # new user else: db.execute( "insert into user(user,password,create_time,total_flow) values (%s,%s,current_timestamp,0)", user, password) user_type = 1 err_code = False my_log.info("login page : new user:%s" % user) else: err_code = 1000 my_log.info("login page : username error user:%s" % user) if not err_code: user = self.set_secure_cookie("user", self.get_argument("user")) user_type = self.set_secure_cookie("user_type", str(user_type)) self.redirect("/") else: self.redirect("/?err_code=%s" % err_code)
def books(book_id): """Show book page""" # query database for book query = "SELECT * FROM books WHERE id = :book_id" data = {'book_id': book_id} book = db.execute(query, data).first() isbn = [book.isbn.rstrip()] print(isbn) # set API to request book review counts api_data = goodread_api(isbn) return render_template("book.html", book=book, api_data=api_data)
def insert(self): """插入对象 :return: 插入成功之后记录的pk >>> user = User(username='******') >>> user.insert() """ params = dict() for k, v in self.__mappings__.iteritems(): if v.insertable: if not hasattr(self, k): setattr(self, k, v.default) params[v.name] = getattr(self, k) cols, args = zip(*params.iteritems()) query = "INSERT INTO `{table}` ({fields}) values ({values})".format(table=self.__table__, fields=','.join( ['`%s`' % col for col in cols]), values=','.join(['%s' for i in range(len(cols))])) logger.info('The SQL is {0}'.format(query)) lastrowid = conn.execute(query, *args) return lastrowid
def login(): """Login Users """ # user Post login if request.method == "POST": # clear session session.clear() # ensure username was submitted if not request.form.get("username"): flash("Please enter a valid username") return redirect(url_for("login")) # ensure password was submitted if not request.form.get("password"): flash("Please enter a valid password") return redirect(url_for("login")) # query database for user query = "SELECT * FROM users WHERE username = :username" data = {'username': request.form.get("username")} result = db.execute(query, data).first() # validate users credentials if result: if check_password_hash(result.password, request.form.get("password")): session['user_id'] = result.id session['username'] = result.username flash("You are now logged in") return redirect(url_for("index")) # Not the user return elif not result: return redirect(url_for("login")) # Get login page else: return render_template("login.html")
def post(self): roomname = self.get_argument("roomname") password = self.get_argument("password") if roomname: room = db.query("SELECT * FROM room WHERE roomname=%s", roomname) if room: return self.write(json_encode({'result': 2})) # try: createtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') roomId = db.execute( "INSERT INTO room (roomname, created_time, owner_id) VALUES (%s, %s, %s)", roomname, createtime, self.get_secure_cookie("userid")) print roomId room = db.get( "SELECT room.id, roomname, created_time, username FROM room, user WHERE room.id=%s and room.owner_id=user.id", int(roomId)) roomInfo = {} roomInfo["roomId"] = room.id roomInfo["roomname"] = room.roomname roomInfo["created_time"] = room.created_time.strftime( '%Y-%m-%d %H:%M:%S') roomInfo["username"] = room.username return self.write(roomInfo)
def search(): """Search for books""" # set values to none query = '' data = '' # if the search is by isbn if request.args.get("search") == "isbn": query = text("SELECT * FROM books WHERE isbn LIKE :isbn") data = {'isbn': f"%{request.args.get('book')}%"} # if the search is by title elif request.args.get("search") == "title": query = text("SELECT * FROM books WHERE title LIKE :title") data = {'title': f"%{request.args.get('book').title()}%"} # if the search is by author elif request.args.get("search") == "author": query = text("SELECT * FROM books WHERE author LIKE :author") data = {'author': f"%{request.args.get('book').title()}%"} # get all the books that match users search books = None if request.args.get('book') is not None and len( request.args.get('book')) > 0: # catch and hanlde errors try: books = db.execute(query, data).fetchall() finally: db.close() print(request.args.get('book')) return render_template("partials/search.html", books=books)
def insert(postID, blog, content, tag, timestamp): db.execute( "INSERT INTO logs (id, blog, content, tag, date) VALUES (%s, %s, %s, %s, %s)", (postID, blog, content, tag, timestamp))
def create(cls): query = cls.__dict__['__create_table_sql'](cls) if not cls.is_table_exist(): conn.execute(query)
def insert(postID, blog, content, tag, timestamp): db.execute("INSERT INTO logs (id, blog, content, tag, date) VALUES (%s, %s, %s, %s, %s)", (postID, blog, content, tag, timestamp))
def raw_execute(cls, sql, *args): lastrowid = conn.execute(sql, *args) return lastrowid
def unique(post): postID = post["id"] db.execute("SELECT * FROM logs WHERE id=%s", (postID, )) results = db.fetchall() return len(results) == 0
#All the imports import time from bs4 import BeautifulSoup import sys from settings import client, conn, db, blogName #Tags tags = ["Social justice","Privilege","Feminism","MRA","Misandry","Misogyny","Genderfluid","Genderqueer","Queerplatonic","Demisexual","Otherkin","Multiplicity","Plurality","Headmates","White people","Ableism","Fatphobia"] #Set the mode. Two options: sync and run if len(sys.argv) > 1: mode = "sync" else: mode = "run" db.execute("SELECT * FROM logs LIMIT 1") if len(db.fetchall()) == 0: print "Database is empty." mode = "sync" print "Running in mode %s" % mode.upper() #Some helper functions def sanitize(value): #Strip HTML soup = BeautifulSoup(value, "html.parser") for tag in soup.findAll(True): tag.hidden = True content = soup.renderContents() #Remove duplicate whitespaces/newlines
def unique(post): postID = post["id"] db.execute("SELECT * FROM logs WHERE id=%s", (postID,)) results = db.fetchall() return len(results) == 0
from settings import client, conn, db, blogName #Tags tags = [ "Social justice", "Privilege", "Feminism", "MRA", "Misandry", "Misogyny", "Genderfluid", "Genderqueer", "Queerplatonic", "Demisexual", "Otherkin", "Multiplicity", "Plurality", "Headmates", "White people", "Ableism", "Fatphobia" ] #Set the mode. Two options: sync and run if len(sys.argv) > 1: mode = "sync" else: mode = "run" db.execute("SELECT * FROM logs LIMIT 1") if len(db.fetchall()) == 0: print "Database is empty." mode = "sync" print "Running in mode %s" % mode.upper() #Some helper functions def sanitize(value): #Strip HTML soup = BeautifulSoup(value, "html.parser") for tag in soup.findAll(True): tag.hidden = True content = soup.renderContents()
def register(): """Registration page""" # Get register page if request.method == 'GET': return render_template("register.html") # Post to register page if request.method == 'POST': # set a boolean for validation valid = True # check if username or password was provided if not request.form.get("username") or not request.form.get( "password"): flash("must provide a username or password") valid = False return redirect(url_for("register")) # check if email is valid email if not EMAIL_REGEX.match(request.form.get("email")): flash("must provide an email") valid = False return redirect(url_for("register")) # check if user is already registered email_query = "SELECT * FROM users WHERE email = :email" data = {'email': request.form.get("email")} result = db.execute(email_query, data).first() # if user exists alreay redirect if result: flash("user already exists") valid = False return redirect(url_for("register")) # check for dangerous input from user if re.search(r";|'|-", request.form.get("username")) or re.search( r";|'|-", request.form.get("password")) or re.search( r";|'|-", request.form.get("email")): flash("ILLEGAL!!!") valid = False return redirect(url_for("register")) # final check of validations if not valid: return redirect(url_for("register")) # register user oncee valid elif valid: password = generate_password_hash(request.form.get("password")) query = "INSERT INTO users (username, email, password) VALUES (:username, :email, :password)" data = { 'username': request.form.get("username"), 'email': request.form.get("email"), 'password': password } # Catch and handle errors try: db.execute(query, data) flash("Registration was successful") except InternalServerError: return finally: db.commit() return redirect(url_for("login"))
time.sleep(1) continue break return r for tag in log_tags: total = 0 earliest = int(datetime.now().timestamp()) quit = False while not quit: posts = get(f"{url}{tag}&before={earliest}")["response"] if len(posts) == 0: quit = True break earliest = min([post["timestamp"] for post in posts]) posts = [post for post in posts if post["type"] == "text"] for post in posts: try: id = post["id"] body = html2text(post["body"]) timestamp = post["timestamp"] db.execute("""INSERT INTO fics (id, body, date) VALUES (%s, %s, %s)""", (id, body, timestamp)) conn.commit() total += 1 print(f"{tag}: {total}\r", end="") except Exception: conn.rollback() print()
import markovify import nltk import re import random from settings import client, conn, db, blogName #Tags tags = ["miraculous ladybug", "fanfiction", "miraculous", "ladybug"] db.execute("SELECT content FROM logs order by random() LIMIT 4000") text = [post[0] for post in db.fetchall()] text = ''.join(text) class POSifiedText(markovify.Text): def word_split(self, sentence): #words = re.split(self.word_split_pattern, sentence) words = nltk.word_tokenize(sentence.decode('utf8')) if (words): #print(words) words = ["::".join(tag) for tag in nltk.pos_tag(words)] else: words = list('', ) return words def word_join(self, words): sentence = " ".join(word.split("::")[0] for word in words) return sentence