def __init__(self): from credentials import loadCredentials from driver import loadDriver credentials = loadCredentials() driver = loadDriver() if 'windows' in driver: Log('Platform: Windows', 'noprint') self.cnxn = pyodbc.connect( 'DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (driver['windows'], credentials['server'], credentials['database'], credentials['user'], credentials['password'])) elif 'linux' in driver: Log('Platform: Linux', 'noprint') self.cnxn = pyodbc.connect( 'DRIVER={FreeTDS};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s' % (credentials['server'], credentials['port'], credentials['database'], credentials['user'], credentials['password'])) self.cursor = self.cnxn.cursor() self.cursor.execute('SET LANGUAGE NORWEGIAN') self.YYYYMMDD = self.cursor.execute( 'SELECT CONVERT(VARCHAR(10),CURRENT_TIMESTAMP,112)').fetchone()[0] self.weekNum = self.cursor.execute( 'SELECT DATENAME(WEEK, CURRENT_TIMESTAMP)-1').fetchone()[0] self.weekday = self.cursor.execute( 'SELECT DATENAME(WEEKDAY, CURRENT_TIMESTAMP)').fetchone()[0] self.timestamp = self.cursor.execute( 'SELECT CONVERT(VARCHAR(20),CURRENT_TIMESTAMP,20)').fetchone()[0] self.time = self.cursor.execute( 'SELECT CONVERT(VARCHAR(16),GETDATE(),20)').fetchone()[0]
def initiate_backtest(webPageList): # User Information credentials = loadCredentials() sentiment_user = credentials["sentimentrader"]["username"] sentiment_password = credentials["sentimentrader"]["password"] username = sentiment_user password = sentiment_password # Chrome Driver is required in path or specify path within argument (which chromedriver to find # Initiate driver and browser instance display = Display(visible=0, size=(800, 800)) display.start() chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--headless') driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options) # To run as root, use above 3 lines. To run as regular user, the below is fine. # driver = webdriver.Chrome('/usr/bin/chromedriver') driver.get('https://sentimentrader.com/') # Login to sentimentrader.com username_field = driver.find_element_by_id('username') password_field = driver.find_element_by_id('password') username_field.send_keys(username) password_field.send_keys(password) password_field.send_keys(Keys.RETURN) print('Successfully logged in') for backtestUrl in webPageList: run_backtest(backtestUrl, driver) # Close driver for chrome. driver.close()
import psycopg2 import csv import time import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from pyvirtualdisplay import Display from credentials import loadCredentials __author__ = "Joseph Wells (Primary), Robert Kump (Secondary)" # User Information credentials = loadCredentials() sentiment_user = credentials["sentimentrader"]["username"] sentiment_password = credentials["sentimentrader"]["password"] username = sentiment_user password = sentiment_password # Chrome Driver is required in path or specify path within argument (which chromedriver to find # Initiate driver and browser instance display = Display(visible=0, size=(800, 800)) display.start() # driver = webdriver.Chrome('/usr/bin/chromedriver') # No options webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') # chrome_options.add_argument('--headless') # Unnecessary in this case but a common chrome option to add. driver = webdriver.Chrome('/usr/bin/chromedriver', options=chrome_options) driver.get('https://sentimentrader.com/')
def writeSpreadsheet(): import openpyxl from openpyxl import Workbook from openpyxl.utils import get_column_letter from openpyxl.styles import Alignment credentials = loadCredentials('cloud') def exportXLSX(fileName: str): def cloudUpload(fileName: str): def commandAsmbl(cmd): Log(f'Cloud Upload: uploading {category} {when} to {credentials["server"]}' ) subprocess.Popen(cmd, shell=True, executable='/bin/bash') commmand = 'curl -u ' commmand += credentials['user'] commmand += ':' commmand += credentials['password'] commmand += ' -T ' commmand += fileName commmand += ' https://' commmand += credentials['server'] commmand += '/remote.php/dav/files/' commmand += credentials['user'] commmand += '/' commmand += category commmand += '/' commmand += when commmand += '/' commandAsmbl(commmand) # load spreadsheet wb = Workbook() ws = wb.active # apply default col length cellLength = {} for i in range(10): cellLength[i] = 5 # set length of col based on length of longest cell value for row in data[category][when]: for i, cell in enumerate(row): if i not in cellLength: cellLength[i] = 1 try: if cellLength[i] < len(str(cell)): cellLength[i] = len(str(cell)) except KeyError: continue # apply col length from values in cellLength to cell A-J ws.column_dimensions['A'].width = cellLength[0] + 5 ws.column_dimensions['B'].width = cellLength[1] + 5 ws.column_dimensions['C'].width = cellLength[2] + 5 ws.column_dimensions['D'].width = cellLength[3] + 5 ws.column_dimensions['E'].width = cellLength[4] + 5 ws.column_dimensions['F'].width = cellLength[5] + 5 ws.column_dimensions['G'].width = cellLength[6] + 5 ws.column_dimensions['H'].width = cellLength[7] + 5 ws.column_dimensions['I'].width = cellLength[8] + 5 ws.column_dimensions['J'].width = cellLength[9] + 5 freeze = None # find freeze point for titles while appending for i, row in enumerate(data[category][when]): if len(row) > 2 and freeze == None: freeze = 'A' + str(i + 2) if category == 'Salg' and i > 2: ws.append(list(row)[:-1]) else: ws.append(list(row)) for col in ws.columns: # align all fields with value to center for cell in col: cell.alignment = Alignment(horizontal='center', vertical='center') if freeze != None: ws.freeze_panes = ws[freeze] # save spreadsheet wb.save(fileName) # send wb to cloud cloudUpload(fileName) # give column names and add date info before exporting to spreadsheet colName = [ 'Totalt', '00-01', '01-02', '02-03', '03-04', '04-05', '05-06', '06-07', '07-08', '08-09', '09-10', '10-11', '11-12', '12-13', '13-14', '14-15', '15-16', '16-17', '17-18', '18-19', '19-20', '20-21', '21-22', '22-23', '23-24' ] data['Omsetning']['Daglig'].insert(0, colName) data['Omsetning']['Daglig'].insert(0, [ data['Tider']['yesterday']['weekday'].title() + ' Uke-' + data['Tider']['yesterday']['weekNum'] ]) data['Omsetning']['Daglig'].insert(0, [data['Tider']['yesterday']['human']]) data['Omsetning']['Daglig'].insert(0, ['Omsetning']) colName = [ 'Artikkel ID', 'Merke', 'Navn', 'Importert', 'Antall Lager', 'Lagerplass', 'Lev.ID' ] data['Import']['Daglig'].insert(0, colName) data['Import']['Daglig'].insert(0, [ data['Tider']['yesterday']['weekday'].title() + ' Uke-' + data['Tider']['yesterday']['weekNum'] ]) data['Import']['Daglig'].insert(0, [data['Tider']['yesterday']['human']]) data['Import']['Daglig'].insert(0, ['Vareimport']) colName = [ 'Artikkel ID', 'Merke', 'Navn', 'Antall Lager', 'Lagerplass', 'Sist Importert', 'Lev. ID' ] data['Utsolgt']['Daglig'].insert(0, colName) data['Utsolgt']['Daglig'].insert(0, [ data['Tider']['yesterday']['weekday'].title() + ' Uke-' + data['Tider']['yesterday']['weekNum'] ]) data['Utsolgt']['Daglig'].insert(0, [data['Tider']['yesterday']['human']]) data['Utsolgt']['Daglig'].insert(0, ['Utsolgte Varer']) colName = [ 'Artikkel ID', 'Merke', 'Navn', 'Antall Solgt', 'Dato', 'Klokketime', 'Pris', 'Rabatt', 'Betalingsmate' ] data['Salg']['Daglig'].insert(0, colName) data['Salg']['Daglig'].insert(0, [ data['Tider']['yesterday']['weekday'].title() + ' Uke-' + data['Tider']['yesterday']['weekNum'] ]) data['Salg']['Daglig'].insert(0, [data['Tider']['yesterday']['human']]) data['Salg']['Daglig'].insert(0, ['Varesalg']) # loop through results in data and export spreadsheet for category in data: if category != 'Tider': for when in data[category]: if when == 'Daglig': fileName = os.path.join( dirs[category], when, data['Tider']['yesterday']['YYYYMMDD'] + '.xlsx') elif when == 'Ukentlig': fileName = os.path.join( dirs[category], when, data['Tider']['yesterday']['YYYY-weekNum'] + '.xlsx') elif when == 'Maanedlig': fileName = os.path.join( dirs[category], when, data['Tider']['yesterday']['YYYYMMDD'][:6] + '.xlsx') else: fileName == False if fileName != False: exportXLSX(fileName) Log(f'Spreadsheet: exporting {fileName}')
def __init__(self): os.makedirs('%s/inventory' % os.path.dirname(os.path.realpath(__file__)), exist_ok=True) os.makedirs('%s/inventory/sessions' % os.path.dirname(os.path.realpath(__file__)), exist_ok=True) self.getTime = datetime.now() self.intDate = self.getTime.strftime("%Y%m%d") # self.timestamp = self.getTime.strftime( # "%Y-%m-%d_%H:%M:%S.%f")[:-4] self.file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'inventory') self.sessionPath = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'inventory/sessions', self.intDate) # if no session file, touch to create empty self.sessionFile = os.path.join(self.sessionPath + '.csv') if not os.path.isfile(self.sessionFile): Log('First session run, creating session: ' + self.intDate) with open(self.sessionFile, 'a'): os.utime(self.sessionFile, None) with open( '%s/mode.json' % os.path.join(os.path.dirname(os.path.realpath(__file__))), 'r') as mode: self.mode = json.load(mode) if self.mode['shutdown'] == True: from subprocess import call # load credentials for get server self.credentialsGet = loadCredentials('get') # load credentials for post server self.credentialsPost = loadCredentials('post') # read, add, save session csv self.sessions = [ file for file in os.listdir('%s/inventory/sessions' % os.path.dirname(os.path.realpath(__file__))) if os.path.splitext(file)[-1] == '.csv' ] # check for existing sessions if self.sessions == []: Log('sessions directory /inventory/sessions/ is empty' + ', new session with stamp ' + self.intDate + '.csv will be created') open(r'%s.csv' % self.sessionPath, 'a', newline='') else: if int(self.intDate) < int(max(self.sessions)[0:8]): Log('datestamp: ' + self.intDate + ' is not up to date compared to latest session file') else: Log('session'.ljust(10) + self.intDate)
def __init__(self): from credentials import loadCredentials credentials = loadCredentials('get') driver = loadDriver() # try: if 'windows' in driver: try: Log('Getconnect: Platform: Windows') self.cnxn = pyodbc.connect('DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % ( driver['windows'], credentials['server'], credentials['database'], credentials['user'], credentials['password'] ) ) Log(f'Getconnect: Connected to {credentials["database"]}') except pyodbc.ProgrammingError: Log(f'Getconnect: Could not connect to {credentials["database"]}') exit() elif 'linux' in driver: Log('Getconnect: Platform: Linux') try: self.cnxn = pyodbc.connect( 'DRIVER={FreeTDS};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s' % ( credentials['server'], credentials['port'], credentials['database'], credentials['user'], credentials['password'] ) ) Log(f'Getconnect: Connected to {credentials["database"]}') except pyodbc.ProgrammingError: Log(f'Getconnect: Could not connect to {credentials["database"]}') exit() Log(f'Getconnect: Connected to {credentials["database"]}') self.timeGet = 'SELECT CONVERT(VARCHAR(16),GETDATE(),20)' self.timestampGet = 'SELECT CONVERT(VARCHAR(20),CURRENT_TIMESTAMP,20)' self.YYYYMMDDGET = '''SELECT CONVERT(VARCHAR(10),DATEADD(DAY, (?),CURRENT_TIMESTAMP),112)''' self.weekNumGet = '''SELECT DATENAME(WEEK, DATEADD(DAY, (?), CURRENT_TIMESTAMP))''' self.dateMonthGet = '''SELECT DATENAME(DAY, DATEADD(DAY, (?), CURRENT_TIMESTAMP))''' self.weekdayGet = '''SELECT DATENAME(WEEKDAY, DATEADD(DAY, (?), CURRENT_TIMESTAMP))''' self.monthGet = '''SELECT DATENAME(MONTH, DATEADD(DAY, (?), CURRENT_TIMESTAMP))''' self.yesterday = -1 # subtract 1 day from todays date # self.days = [-1,-8,-15] # getting from last day, same weekday the week before and the week before that self.cursor = self.cnxn.cursor() self.cursor.execute('SET LANGUAGE NORWEGIAN') self.time = self.cursor.execute(self.timeGet).fetchone()[0][11:16] self.timestamp = self.cursor.execute(self.timestampGet).fetchone()[0] self.YYYYMMDD = self.cursor.execute(self.YYYYMMDDGET,0).fetchone()[0] self.weekNum = self.cursor.execute(self.weekNumGet,0).fetchone()[0] self.weekday = self.cursor.execute(self.weekdayGet,0).fetchone()[0] self.weekdayYesterday = self.cursor.execute( self.weekdayGet,self.yesterday).fetchone()[0] self.yesterdayYYYMMDD = self.cursor.execute( self.YYYYMMDDGET,self.yesterday).fetchone()[0] self.weekNumYesterday = self.cursor.execute( self.weekNumGet,self.yesterday).fetchone()[0] self.monthYesterday = self.cursor.execute( self.monthGet,self.yesterday).fetchone()[0] self.dateHuman = humanYYYYMMDD(self.YYYYMMDD) self.dateYesterdayHuman = humanYYYYMMDD(self.yesterdayYYYMMDD)