Advertisement
Guest User

new

a guest
Feb 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. from flask import Flask, flash, render_template, request, url_for
  2. import pymysql.cursors
  3.  
  4. conn = pymysql.connect(user = 'root',
  5. passwd = '2244ssff11',
  6. host = '127.0.0.1',
  7. db = 'tweets',
  8. charset = 'utf8mb4',
  9. cursorclass=pymysql.cursors.SSCursor)
  10.  
  11. cursor = conn.cursor()
  12.  
  13.  
  14. app = Flask(__name__)
  15.  
  16. @app.route("/", methods=["GET", "POST"])
  17. def index():
  18. if request.method == "POST":
  19. result = []
  20. search_open = open("accounts.txt", "r")
  21. search_query = request.form.get("query")
  22. search_query = search_query.lower()
  23.  
  24. for account_name in search_open.readlines():
  25. cursor.execute("SELECT id, name, tweet, created_at, url FROM tweets WHERE name=%s ORDER BY created_at DESC" % (account_name.strip()))
  26. test_call = cursor.fetchall()
  27. for data in test_call:
  28. if search_query in data[2].lower():
  29. result.append({'id':data[0], 'name':data[1], 'tweet':data[2], 'created_at':data[3], 'url':data[4]})
  30.  
  31. cursor.close()
  32. search_open.close()
  33. conn.close()
  34. return render_template("search.html", results = result)
  35.  
  36. else:
  37. return render_template("index.html")
  38.  
  39.  
  40.  
  41.  
  42.  
  43. if __name__ == "__main__":
  44. app.run(host='0.0.0.0')
  45. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement