def __init__(self, start_time): """ Initiates Gmail API class which is responsible for getting messages from specified email. Initiates Sheets API class which is responsible for writing values to spreadsheet cell. """ self.gmail = GmailApi() self.sheets = SheetsApi() # self.postgre = PostgreSQL() self.is_running = False self.start_time = start_time with open('logging.conf', 'r') as lc: log = json.load(lc) logging.config.dictConfig(log) self.log = logging.getLogger('MainApp')
def test_read_values(self): range = "Sheet1!A1:D5" ret = SheetsApi().get_spreadsheet_values( "1S52oEcqp6CcPr-Goo7Pkc1B401_QGVdxpth8wR5LTkM", range) import json str_json = json.dumps(ret, indent=2) print str_json
def test_create(self): json_file = TEST_DIR + "/create_spreadsheet.json" json_data = open(json_file).read() spreadsheet_body = json.loads(json_data) ret = SheetsApi().create_spreadsheet(spreadsheet_body) self.assertEqual(spreadsheet_body["properties"]["title"], ret["properties"]["title"])
def main(): if len(sys.argv) != 2: print "Arguments : ", str(sys.argv) print "Usage: python create_spreadsheet.py sample.json" sys.exit(1) json_file = sys.argv[1] print "Creating Spreadsheet from ", json_file json_data = open(json_file).read() spreadsheet_body = json.loads(json_data) ret = SheetsApi().create_spreadsheet(spreadsheet_body) print ret
class MainApp: def __init__(self, start_time): """ Initiates Gmail API class which is responsible for getting messages from specified email. Initiates Sheets API class which is responsible for writing values to spreadsheet cell. """ self.gmail = GmailApi() self.sheets = SheetsApi() # self.postgre = PostgreSQL() self.is_running = False self.start_time = start_time with open('logging.conf', 'r') as lc: log = json.load(lc) logging.config.dictConfig(log) self.log = logging.getLogger('MainApp') def __run(self): """ Gets receipts from gmail in list. Then provides following receipt list to write_to_sheet method which writes values to correct cell. :return: nothing. """ self.log.info('Starting to get receipts from Gmail API ..') self.is_running = False receipts = self.gmail.get_receipts() self.log.info('Got receipts!') # self.postgre.insert_data(receipts) self.log.info('Starting to write to Google Sheets spreadsheet .. ') self.sheets.write_to_sheet(receipts) self.log.info('Writting to spreadsheet completed!') balance = self.sheets.get_weekly_balance() fortune = get_fortune() text = f'Labas,\nSavaitės likutis: {balance}\n\nŠios dienos palinkėjimas: {fortune}' mes = self.gmail.create_message('*****@*****.**', '*****@*****.**', 'FINANSAI: Likęs balansas savaitei', text) self.gmail.send_message('me', mes) self.start() def __time_to_seconds(self, time): """ Converts specified time into seconds :param time. Specified time (e.g 12:00:01) :return int. Time in seconds """ h, m, s = time.split(':') return int(h) * 3600 + int(m) * 60 + int(s) def __get_wait_time(self): """ Gets required amount of time to wait before starting main applications sequence. :return int. Required amount of time to wait in seconds """ current_time = datetime.now().strftime('%H:%M:%S') wait_time = self.__time_to_seconds( self.start_time) - self.__time_to_seconds(current_time) if wait_time < 0: wait_time = 24 * 3600 + wait_time return wait_time def start(self, wait=True): """ Gets required wait time to start application by calling __get_wait_time() method. Initiates Timer() class with specified wait interval. :param wait. Boolean indicating whether wait for start time or not. :return nothing. """ self.log.info('Getting required amount of wait time ..') wait_time = self.__get_wait_time() self.log.debug(f'Wait time in seconds - {wait_time}') if wait is False: self.__run() if not self.is_running: self.__timer = Timer(wait_time, self.__run) self.__timer.start() self.is_running = True self.log.info(f'Waiting for {self.start_time}')
def test_read_spreadsheet(self): ret = SheetsApi().get_spreadsheet(self._spreadsheet["spreadsheetId"], [])
# -*- coding: utf-8 -*- import os import logging import time from configparser import ConfigParser from budget_bot import BudgetBot from sheets_api import SheetsApi import telebot config = ConfigParser() dir_path = os.path.dirname(os.path.abspath(__file__)) config.read(dir_path + '/config/app.ini') sheet_api = SheetsApi() bot = BudgetBot() bot.remove_webhook() time.sleep(1) PRIVATE_COMMANDS = [ command.strip() for command in config.get('BUDGET_BOT', 'private_commands').split(',') ] SUPERUSER_COMMANDS = [ command.strip() for command in config.get('BUDGET_BOT', 'superuser_commands').split(',') ] def access_check(func):