Guest User

Untitled

a guest
Sep 29th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import dbUtils as db
  2. import pandas as pd
  3. import re
  4. from datetime import datetime
  5. from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody
  6.  
  7. processName = 'OPIS Denaturant Wire Scraper'
  8. subProcessName = 'Scraping Raw Emails'
  9.  
  10. #Check to see if this job has already run, if so then quit
  11. if db.checkForSuccess(processName, True) == True:
  12. raise SystemExit
  13.  
  14. try:
  15. credentials = Credentials(username='GPRE.local\\' + db.getCreds()['Username'], password = db.getCreds()['Password'])
  16. account = Account(primary_smtp_address = 'someone@someplace.com', credentials = credentials, autodiscover = True, access_type = DELEGATE)
  17. folder = account.inbox / 'brokerWires'
  18.  
  19. for email in folder.filter(subject__icontains = 'opis lp report'): #Find all OPIS emails
  20. try:
  21. with open('FILEPATH/lp_' + str(email.datetime_sent.year) + '-' + str(email.datetime_sent.month) + '-' + str(email.datetime_sent.day) + '.txt', 'wb') as f:
  22. f.write(email.attachments[0].content)
  23.  
  24. rawfile = open('FILEPATH/lp_' + str(email.datetime_sent.year) + '-' + str(email.datetime_sent.month) + '-' + str(email.datetime_sent.day) + '.txt') #Open file just saved
  25. rawtextdata = rawfile.read() #Read raw file as string
  26. splitrawtextdata = rawtextdata.split('\n') #Split string line by line
  27.  
  28. currentproduct = ''
  29. currentprice = ''
  30. outputdataframe = []
  31. for line in splitrawtextdata: #For each line
  32. if re.findall('opis lp prices', line, re.I): #Found product name line
  33. productdata = [line.strip()]
  34.  
  35. elif re.findall('\d', line, re.I) and not re.findall('Avg', line, re.I): #Found current price
  36. splitline = line.split()
  37. productdata.append(datetime.strptime(splitline[0] + ' ' + splitline[1] + ' ' + splitline[2], '%B %d, %Y'))
  38. productdata.append(float(splitline[-3]))
  39. productdata.append(float(splitline[-2]))
  40. productdata.append(float(splitline[-1]))
  41.  
  42. elif re.findall('\d', line, re.I) and re.findall('Avg', line, re.I): #Found average price
  43. splitline = line.split()
  44. productdata.append(float(splitline[-3]))
  45. productdata.append(float(splitline[-2]))
  46. productdata.append(float(splitline[-1]))
  47. outputdataframe.append(productdata)
  48.  
  49. outputdataframe = pd.DataFrame(outputdataframe, columns = ['Product', 'Price_Date', 'Daily_Low', 'Daily_High', 'Daily_Avg', 'Monthly_Low', 'Monthly_High', 'Monthly_Avg'])
  50. outputdataframe = outputdataframe[['Price_Date', 'Product', 'Daily_Low', 'Daily_High', 'Daily_Avg', 'Monthly_Low', 'Monthly_High', 'Monthly_Avg']]
  51.  
  52. datesuploaded = db.sqlToDataFrame('RiskSandbox', 'SELECT distinct pricedate FROM [DATABASE]') #Get distinct price dates already uploaded to SQL table
  53. if outputdataframe['Price_Date'].iloc[0].to_datetime() not in [uploadeddate.to_datetime() for uploadeddate in datesuploaded['pricedate']]: #This data was not already uploaded
  54. print 'Uploading data for: ' + str(outputdataframe['Price_Date'].iloc[0])
  55. db.dataFrameToSQL('ODBCCONN', outputdataframe, 'opisDenaturant', "//FILEPATH/opisPrices.csv", True) #Insert new data to SQL table
  56.  
  57. else: #This data was already uploaded
  58. print 'Already uploaded data for: ' + str(outputdataframe['Price_Date'].iloc[0])
  59.  
  60. except:
  61. pass
  62.  
  63. subProcessName = 'Job Complete'
  64. db.processLog(processName, subProcessName, None)
  65. except Exception, e:
  66. db.processLog(processName, subProcessName, errorLog=str(e))
Add Comment
Please, Sign In to add comment