Guest User

Untitled

a guest
Sep 5th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. import psycopg2
  2. import time
  3. import selenium
  4. from selenium import webdriver
  5. import openpyxl
  6. from openpyxl import load_workbook
  7. import subprocess
  8. from email.mime.text import MIMEText
  9. from email.mime.application import MIMEApplication
  10. from email.mime.multipart import MIMEMultipart
  11. from smtplib import SMTP
  12. import smtplib
  13. import datetime
  14. from PyPDF2 import PdfFileWriter, PdfFileReader
  15. from time import mktime
  16. import sys
  17. import os
  18. import re
  19. import redis
  20. import requests
  21.  
  22. class Useful_Functions:
  23.  
  24. def __init__(self):
  25. pass
  26.  
  27. def check_cache(cache_key):
  28. """
  29. Checks the cache to see if a key exists
  30. param cache_key: A string representing a key
  31. type cache_key: string
  32. """
  33. db = redis.from_url(os.environ.get("REDIS_URL"))
  34. if db.get(cache_key):
  35. print("Found cache data, continuing and then comparing against this.")
  36. return True
  37. else:
  38. return False
  39.  
  40.  
  41. def write_cache(data, cache_key):
  42. """
  43. Write data with a key of cache_key to the redis cache
  44. param data: A python dictionary holding the data you want cached
  45. param cache_key: A string representing a key
  46. type data: dict
  47. type cache_key: string
  48. """
  49. db = redis.from_url(os.environ.get("REDIS_URL"))
  50. try:
  51. db.set(cache_key, json.dumps(data))
  52. return True
  53. except Exception as e:
  54. print(e)
  55. print("Failed to write to cache")
  56.  
  57.  
  58. def read_cache(cache_key):
  59. """
  60. Read data with a key of cache_key from the redis cache
  61. param cache_key: A string representing a key
  62. type cache_key: string
  63. """
  64.  
  65. db = redis.from_url(os.environ.get("REDIS_URL"), decode_responses=True)
  66. data = json.loads(db.get(cache_key))
  67.  
  68. return data
  69.  
  70. def send_slack_message(**kwargs):
  71. """
  72. Sends a messages to Slack via a Webhook
  73.  
  74. param: webhook_url
  75. param: msg_username
  76. param: msg_text
  77. param: msg_channel
  78. param: icon_url
  79. type webhook_url: string
  80. type msg_username: string
  81. type msg_text: string
  82. type msg_channel: string
  83. type icon_url: string
  84. """
  85.  
  86. if not webhook_url:
  87. post_url = os.environ.get("SLACK_WEBHOOK_URL")
  88. else:
  89. post_url = webhook_url
  90.  
  91. payload={"text": msg_text),
  92. "username": msg_username,
  93. "channel": msg_channel,
  94. "icon_url": icon_url}
  95.  
  96. response = requests.post(post_url, json=payload)
  97.  
  98. if response.status_code == 201:
  99. print("Message sent to Slack")
  100. return True
  101. else:
  102. return False
  103. def postgres_ref_db_connection():
  104. """
  105. Set up the postgres connection
  106. """
  107. return psycopg2.connect(host='HOST',
  108. user='USERNAME',
  109. password='PASSWORD',
  110. database='DB_NAME',
  111. port=5432)
  112.  
  113. def insert_records(insert_statement, values):
  114. """
  115. Inserts a list of values into a database
  116.  
  117. param insert_statement: The SQL query used to insert a row of data
  118. type insert_statement: string representing a SQL query
  119. param values: the list of tuples to insert into the database
  120. type values: list containing a tuple
  121. """
  122. if len(values) > 0:
  123. db = postgres_ref_db_connection()
  124.  
  125. cur = db.cursor()
  126. try:
  127. if len(values) >= 1:
  128. cur.executemany(insert_statement, values)
  129. db.commit()
  130. print("INSERTED INTO DB")
  131.  
  132. except Exception as err:
  133. print("EXCEPTION")
  134. print(err)
  135. db.rollback()
  136. pass
  137. finally:
  138. cur.close()
  139. db.close()
  140.  
  141. def create_firefox_driver(path_to_profile):
  142. """
  143. Creates a firefox driver using a specified profile
  144.  
  145. param path_to_profile: The filepath of the profile
  146. type path_to_profile: String representing a filepath
  147. """
  148. profile = webdriver.FirefoxProfile(path_to_profile)
  149. driver = webdriver.Firefox(profile)
  150. return driver
  151.  
  152. def make_pdf_from_xlsx(filename, path_to_edit, path_to_completed, pdf_ready):
  153. """
  154. Converts XLSX to PDF
  155.  
  156. param filename: The name of the xlsx file to edit
  157. type filename: String representing a file name
  158. param path_to_edit: The location of the folder containing a file to edit
  159. type path_to_edit: String representing a filepath
  160. param path_to_edit: The location of the folder to place a completed file
  161. type path_to_completed: String representing a filepath
  162. param pdf_ready: The location of the folder containing a file to edit
  163. type pdf_ready: String representing a filepath
  164. """
  165. try:
  166. for i in range(5):
  167. pdf_name = filename.replace('xlsx', 'pdf')
  168. try:
  169. with timeout(45, exception=RuntimeError):
  170. pdf_command = 'cd /Applications/LibreOffice.app/Contents/MacOS && ./soffice --headless --convert-to pdf --outdir ' + pdf_ready + ' ' + filename
  171. subprocess.check_output(['bash', '-c', pdf_command])
  172. break
  173. except RuntimeError:
  174. kill_command = 'killall soffice && killall libreoffice && killall libre'
  175. process = subprocess.Popen(kill_command.split(), stdout=subprocess.PIPE)
  176. time.sleep(5)
  177. output, error = process.communicate()
  178. continue
  179. except Exception as e:
  180. print(e)
  181. print('Fucked up converting to pdf')
  182. return None
  183.  
  184. try:
  185. to_edit = path_to_edit + pdf_name
  186. edited = path_to_completed + pdf_name
  187. edit_pdf(to_edit)
  188. crop_pdf(to_edit, edited)
  189.  
  190. return edited
  191. except Exception as e:
  192. print(e)
  193. print('Fucked up editing the PDF')
  194. return None
  195.  
  196.  
  197. def send_gmail(attachment, attachment2, recipient, message_text, subject, email_from, reply_to, username, password):
  198. """
  199. Sends an email from gmail with up to 2 attachments
  200.  
  201. param attachment: The first attachment to the email
  202. type attachment: String representing a file path
  203. param attachment2: The second attachment to the email
  204. type attachment2: String representing a file path
  205. param recipient: The recipient's email address
  206. type recipient: String representing an email address
  207. param message_text: The body text of the email
  208. type message_text: String representing a message
  209. param subject: The subject of the email
  210. type subject: String representing a subject line
  211. param email_from: The sender email address (your email)
  212. type email_from: String representing an email address
  213. param reply_to: The email address that will receive a reply
  214. type reply_to: String representing an email address
  215. param username: Your gmail login
  216. type username: String representing an email address
  217. param password: Your gmail password
  218. type password: String representing a password
  219. """
  220. recipients = [recipient]
  221. emaillist = [elem.strip().split(',') for elem in recipients]
  222. msg = MIMEMultipart()
  223. msg['Subject'] = subject
  224. msg['From'] = email_from
  225. msg['Reply-to'] = reply_to
  226.  
  227. msg.preamble = 'Multipart massage.\n'
  228. part = MIMEText(message_text)
  229. msg.attach(part)
  230.  
  231. part = MIMEApplication(open(str(pdf_file),"rb").read())
  232. pdf_name = pdf_file
  233. part.add_header('Content-Disposition', 'attachment', filename=pdf_name)
  234. msg.attach(part)
  235. part = MIMEApplication(open(str(xlsx_file),"rb").read())
  236. part.add_header('Content-Disposition', 'attachment', filename=xlsx_file)
  237. msg.attach(part)
  238.  
  239. server = smtplib.SMTP("smtp.gmail.com:587")
  240. server.ehlo()
  241. server.starttls()
  242. server.login(username, password)
  243.  
  244. server.sendmail(msg['From'], emaillist , msg.as_string())
  245.  
  246. def clean_text(text):
  247. """
  248. Clean up text and remove the nonsense
  249.  
  250. param text: review text
  251. type text: string
  252. """
  253. return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", text).split())
  254.  
  255. def get_sentiment(review):
  256. """
  257. Get sentiment analysis from a review
  258.  
  259. param review: An Amazon product review
  260. type review: string
  261. """
  262. analysis = TextBlob(clean_text(review))
  263. if analysis.sentiment.polarity > 0:
  264. return 'positive', analysis.sentiment.polarity
  265. elif analysis.sentiment.polarity == 0:
  266. return 'neutral', analysis.sentiment.polarity
  267. else:
  268. return 'negative', analysis.sentiment.polarity
  269.  
  270. def get_2fa_from_pushbullet(api_key, text_to_strip=''):
  271. """
  272. Receives a 2fa code from Pushbullet that was sent there from your phone using something like IFTTT or AutoMagic
  273.  
  274. param api_key: Your pushbullet api key
  275. type api_key: String representing an api key
  276. param text_to_strip: The extraneous text you want to strip from the 2fa message
  277. type text_to_strip: String
  278. """
  279. pb = Pushbullet(api_key)
  280. for i in range(100):
  281. pushes = pb.get_pushes()
  282. latest = pushes[0]['body']
  283. check = open('2fa.txt', 'r').read()
  284. if latest == check:
  285. time.sleep(10)
  286. else:
  287. open('2fa.txt', 'w').close()
  288. with open("2fa.txt", "w") as text_file:
  289. text_file.write(latest)
  290. break
  291. pb.delete_push(pushes[0].get("iden"))
  292. code = latest.replace(text_to_strip, '')
  293. return code
Add Comment
Please, Sign In to add comment