Guest User

Untitled

a guest
Nov 16th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. def make_JSON_creds(filename="./.usercreds.json"):
  2. """Creates a JSON credentials file with name <filename>"""
  3.  
  4. # Get keys (without them showing up on screen)
  5. print("Enter the following credentials:")
  6. SSH_USERNAME = getpass("SSH USERNAME")
  7. SSH_PRIVATE_KEY = getpass("SSH PASSWORD")
  8. DB_USERNAME = getpass("DATABASE ACCESS USERNAME")
  9. DB_PASSWORD = getpass("DATABASE ACCESS PASSWORD")
  10.  
  11. # Write to file. TODO: Encrypt this.
  12. with open(filename, "w") as the_file:
  13. json.dump({"SSH_USERNAME" : SSH_USERNAME,
  14. "SSH_PRIVATE_KEY" : SSH_PRIVATE_KEY,
  15. "DB_USERNAME" : DB_USERNAME,
  16. "DB_PASSWORD" : DB_PASSWORD}, the_file)
  17.  
  18.  
  19. def get_JSON_creds(filename="./.usercreds.json", ask=True):
  20. """Loads JSON credentials from a credentials file.
  21. Creates the file if it doesn't exist.
  22. Returns an OAuth1 header object.
  23. """
  24. # Make the file if it isn't there. Or if you just want to.
  25. if ask or not os.path.isfile(filename): # Note: This is technically unsafe
  26. make_JSON_creds(filename)
  27.  
  28. # Then it's there. Get it.
  29. with open(filename, "r") as the_file:
  30. creds = json.load(the_file)
  31.  
  32. return(creds)
  33.  
  34. #SSH tunnelling with port forwarding to database server
  35.  
  36. def query(q):
  37. with SSHTunnelForwarder(
  38. (host, 22),
  39. ssh_username=ssh_username,
  40. ssh_password=ssh_private_key,
  41. remote_bind_address=(localhost, 3306),
  42. ) as server:
  43. conn = db.connect(host=localhost,
  44. port=server.local_bind_port,
  45. user=user,
  46. passwd=password,
  47. db=database)
  48. return pd.read_sql_query(q, conn)
  49.  
  50.  
  51. creds = get_JSON_creds(filename="./.usercreds_tide.json", ask=True)
  52. # ssh variables
  53. host = 'Hostname'
  54. localhost = '127.0.0.1'
  55. ssh_username = creds.get("SSH_USERNAME").strip()
  56. ssh_private_key = creds.get("SSH_PRIVATE_KEY").strip()
  57.  
  58. # database variables
  59. user=creds.get("DB_USERNAME").strip()
  60. password=creds.get("DB_PASSWORD").strip()
  61. database='tidal'
  62.  
  63.  
  64. df = query("SELECT comm.Art,comm.Text,COUNT(comm.Text),
  65. comm.Info,commurl.Title,commurl.Source FROM comm INNER JOIN commurl ON
  66. comm.Id=commurl.ID GROUP BY Id HAVING COUNT(comm.Text)>100")
Add Comment
Please, Sign In to add comment