Stopping get events after ~12 hours

The next code is used to get events and count them. Every two hours it counts the number of available events for horse racing. If I run it for ~12 hours the number of events are going decreasing till reach zero. If I kill the process and start it again I will get several events afresh.
Why this behaviour?

The following code is only to exemplify the problem.

#!/usr/bin/env python3

from datetime import datetime, timedelta
import os
import requests
import time
import toml

path_name = os.path.dirname(os.path.abspath(__file__))
configuration = toml.load(os.path.join(path_name, 'configuration.toml'))

#######################
### login
import logging

# Gets or creates a logger
logger_testing = logging.getLogger(__name__)  

# set log level
logger_testing.setLevel(logging.INFO)

# define file handler and set formatter
file_handler = logging.FileHandler(os.path.join(path_name, 'testing_smarkets_session-{}.log'.format(datetime.now().date())))
file_handler.setFormatter(logging.Formatter('%(asctime)s : %(levelname)s : %(name)s : %(message)s'))

# add file handler to logger
logger_testing.addHandler(file_handler)
###   END logging
##########################3


class SmarketsClient:
	
	def __init__(self):
		self.auth_token = None
		self.session_time_out = None
		self.is_summer_time = True
		
	def _auth_headers(self):
		return {'Authorization': 'Session-Token ' + self.auth_token}
	
	def init_session(self):
		"""
		The token is valid for 30min, but if you make an authenticated request with it, it will be renewed for the next 30min.
		"""
		print('initiating session')
		print('auth token before request: ', self.auth_token)
		if self.auth_token is None:
			response = requests.post(
				'{}sessions/'.format(configuration["api"]["base_url"]),
				json={
					'username': configuration['auth']['login'],
					'password': configuration['auth']['password'],
				},
			).json()
			self.auth_token = response.get('token')
			if (not self.auth_token is None):
				if (self.is_summer_time): self.session_time_out = datetime.strptime(response.get('stop'), "%Y-%m-%dT%H:%M:%S.%f") + timedelta(hours=1)
				else: self.session_time_out = datetime.strptime(response.get('stop'), "%Y-%m-%dT%H:%M:%S.%f")
			else:
				logger_testing.error("Can't log on")
		print('auth token after request: ', self.auth_token)
	
	def is_active_session(self):
		"""
		need to login again after 30 minutes,
		"""
		logger_testing.info("is_active_session: session_time_out {}".format(self.session_time_out))
		if (not self.auth_token is None and self.session_time_out > (datetime.now() + timedelta(minutes=2))): return True
		
		logger_testing.info("Previous token {}".format(self.auth_token))
		self.auth_token = None
		### make a new authentication
		self.init_session()
		logger_testing.info("Get a new session {}".format(self.auth_token))
		return True

	def get_available_events(self, vect_states = ["upcoming"], vect_types = ["horse_racing_race"],
			start_datetime_max = (datetime.now() + timedelta(days=1)) , limit = 1000):
		
		logger_testing.info("Get avaialble events")
		states_filter = '&'.join(['states={}'.format(state) for state in vect_states])
		types_filter = '&'.join(['types={}'.format(type_) for type_ in vect_types])
		page_filter = (
			'?{}&{}&sort=start_datetime,id&'.format(states_filter, types_filter) + 
			'limit={}&start_datetime_max={}'.format(limit, start_datetime_max).replace(' ', 'T')
		)
		count = 0
		events = []
		while page_filter:
			logger_testing.info("Start page: {}".format(count))
			request_url = '{}events/{}'.format(configuration["api"]["base_url"], page_filter)
			current_page = self._client_wrapper(request_url)
			events += current_page['events']
			page_filter = current_page['pagination']['next_page']
			logger_testing.info("Number ofevents: {}".format(len(events)))
			logger_testing.info("End page: {}".format(count))
			count += 1
		return events
	
	def _client_wrapper(self, url):
		
		response = requests.get(url)
		response_body = response.json()
		if response.status_code != 200:
			logger_testing.error(response_body.get('error_type'))
			return {}
		else:
			logger_testing.info(str(response_body)[:500])
		self.session_time_out = datetime.now() + timedelta(minutes=30)
		return response_body

class SMarketsPumping(object):
	'''
	classdocs
	'''


	def __init__(self):
		'''
		Constructor
		'''
		## get connection on SMarkets
		self.api_client = SmarketsClient()
		self.api_client.init_session()


	def pumping_tebing(self):
		vect_types = ["horse_racing_race"]
		while 1:		
			if (self.api_client.is_active_session()):
				vec_events = self.api_client.get_available_events(["upcoming"], vect_types)
				print("Number of events: {}".format(len(vec_events)))
				logger_testing.info("Number of events: {}".format(len(vec_events)))
			else:
				logger_testing.error("Not logged")
			print("Sleeping....")
			time.sleep(7200)


if __name__ == "__main__":
	tebing_pumping = SMarketsPumping()
	tebing_pumping.pumping_tebing()

File configuration.toml

Blockquote
[auth]
login = "mylogin@xpto.com"
password = “mypass”
[api]
base_url = “https://api.smarkets.com/v3/

Log output file

2019-09-30 22:13:50,651 : INFO : __main__ : is_active_session: session_time_out 2019-09-30 22:43:36.478859
2019-09-30 22:13:50,651 : INFO : __main__ : Get avaialble events
2019-09-30 22:13:50,651 : INFO : __main__ : Start page: 0
2019-09-30 22:13:50,844 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-09-30 22:13:50,844 : INFO : __main__ : Number ofevents: 44
2019-09-30 22:13:50,844 : INFO : __main__ : End page: 0
2019-09-30 22:13:50,844 : INFO : __main__ : Number of events: 44
2019-10-01 00:13:50,881 : INFO : __main__ : is_active_session: session_time_out 2019-09-30 22:43:36.478859
2019-10-01 00:13:50,881 : INFO : __main__ : Previous token WzE2ODM1ODM2LDcxOTA1NTE5XQ.L7SslnQt4pyKUeQAaHZYb0JKEQM
2019-10-01 00:13:51,194 : INFO : __main__ : Get a new session WzE2ODM4MzU0LDcxOTEzNjAyXQ.0REwvfvkpaPJr_DANGmL63DQ1IA
2019-10-01 00:13:51,194 : INFO : __main__ : Get avaialble events
2019-10-01 00:13:51,194 : INFO : __main__ : Start page: 0
2019-10-01 00:13:51,374 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 00:13:51,374 : INFO : __main__ : Number ofevents: 44
2019-10-01 00:13:51,374 : INFO : __main__ : End page: 0
2019-10-01 00:13:51,374 : INFO : __main__ : Number of events: 44
2019-10-01 02:13:51,464 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 00:44:42.407962
2019-10-01 02:13:51,465 : INFO : __main__ : Previous token WzE2ODM4MzU0LDcxOTEzNjAyXQ.0REwvfvkpaPJr_DANGmL63DQ1IA
2019-10-01 02:13:51,880 : INFO : __main__ : Get a new session WzE2ODQwNTEyLDcxOTE3MzY5XQ.n08Ybh_ImDnoaAa3B4ZHx5SnUCs
2019-10-01 02:13:51,880 : INFO : __main__ : Get avaialble events
2019-10-01 02:13:51,880 : INFO : __main__ : Start page: 0
2019-10-01 02:13:52,070 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 02:13:52,070 : INFO : __main__ : Number ofevents: 44
2019-10-01 02:13:52,070 : INFO : __main__ : End page: 0
2019-10-01 02:13:52,071 : INFO : __main__ : Number of events: 44
2019-10-01 04:13:52,149 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 02:44:29.079706
2019-10-01 04:13:52,150 : INFO : __main__ : Previous token WzE2ODQwNTEyLDcxOTE3MzY5XQ.n08Ybh_ImDnoaAa3B4ZHx5SnUCs
2019-10-01 04:13:52,418 : INFO : __main__ : Get a new session WzE2ODQyNTkxLDcxOTIwMzk0XQ.ryHR9Y9ijPtKLeNiNbFP3SLybP4
2019-10-01 04:13:52,418 : INFO : __main__ : Get avaialble events
2019-10-01 04:13:52,418 : INFO : __main__ : Start page: 0
2019-10-01 04:13:52,612 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 04:13:52,612 : INFO : __main__ : Number ofevents: 44
2019-10-01 04:13:52,612 : INFO : __main__ : End page: 0
2019-10-01 04:13:52,612 : INFO : __main__ : Number of events: 44
2019-10-01 06:13:52,681 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 04:44:39.053459
2019-10-01 06:13:52,681 : INFO : __main__ : Previous token WzE2ODQyNTkxLDcxOTIwMzk0XQ.ryHR9Y9ijPtKLeNiNbFP3SLybP4
2019-10-01 06:13:52,942 : INFO : __main__ : Get a new session WzE2ODQ0NjkwLDcxOTIzOTA4XQ.bvg3bVBu3-A5Hemk75fBmWAd_pU
2019-10-01 06:13:52,942 : INFO : __main__ : Get avaialble events
2019-10-01 06:13:52,942 : INFO : __main__ : Start page: 0
2019-10-01 06:13:53,136 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 06:13:53,136 : INFO : __main__ : Number ofevents: 44
2019-10-01 06:13:53,136 : INFO : __main__ : End page: 0
2019-10-01 06:13:53,136 : INFO : __main__ : Number of events: 44
2019-10-01 08:13:53,175 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 06:44:50.497674
2019-10-01 08:13:53,176 : INFO : __main__ : Previous token WzE2ODQ0NjkwLDcxOTIzOTA4XQ.bvg3bVBu3-A5Hemk75fBmWAd_pU
2019-10-01 08:13:53,424 : INFO : __main__ : Get a new session WzE2ODQ3MDA0LDcxOTMwODQ1XQ.6MHYDtskS7J1BHx_5QWm_kS88oI
2019-10-01 08:13:53,424 : INFO : __main__ : Get avaialble events
2019-10-01 08:13:53,424 : INFO : __main__ : Start page: 0
2019-10-01 08:13:53,611 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 08:13:53,611 : INFO : __main__ : Number ofevents: 44
2019-10-01 08:13:53,611 : INFO : __main__ : End page: 0
2019-10-01 08:13:53,612 : INFO : __main__ : Number of events: 44
2019-10-01 10:13:53,658 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 08:43:40.915639
2019-10-01 10:13:53,658 : INFO : __main__ : Previous token WzE2ODQ3MDA0LDcxOTMwODQ1XQ.6MHYDtskS7J1BHx_5QWm_kS88oI
2019-10-01 10:13:53,908 : INFO : __main__ : Get a new session WzE2ODQ5NTgyLDcxOTM5MjU3XQ.ze-Sj1XmXoWj9pDelIMi3j-7r74
2019-10-01 10:13:53,908 : INFO : __main__ : Get avaialble events
2019-10-01 10:13:53,908 : INFO : __main__ : Start page: 0
2019-10-01 10:13:54,106 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.933451Z', 'description': '7 furlongs 210 
yards Flat', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011110', 'hidden': False, 'id': '18169992
', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.933451Z', 'name': '11:10', 'parent_id': '915833', 'short_name': '11:10 @ Vaal', 'slug': 
'vaal-2019-10-01T11:10', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-
2019-10-01 10:13:54,106 : INFO : __main__ : Number ofevents: 44
2019-10-01 10:13:54,106 : INFO : __main__ : End page: 0
2019-10-01 10:13:54,106 : INFO : __main__ : Number of events: 44
2019-10-01 12:13:54,181 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 10:44:27.626331
2019-10-01 12:13:54,181 : INFO : __main__ : Previous token WzE2ODQ5NTgyLDcxOTM5MjU3XQ.ze-Sj1XmXoWj9pDelIMi3j-7r74
2019-10-01 12:13:54,429 : INFO : __main__ : Get a new session WzE2ODUyNTgzLDcxOTQ5MDIwXQ.PwYFEaisbqZZu4zVKAQTfD8rsbw
2019-10-01 12:13:54,430 : INFO : __main__ : Get avaialble events
2019-10-01 12:13:54,430 : INFO : __main__ : Start page: 0
2019-10-01 12:13:54,610 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-30T06:18:27.078886Z', 'description': '7 furlongs 46 y
ards Flat Handicap', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/vaal/2019/10/01/201910011250', 'hidden': False, 'id': '
18169980', 'inplay_enabled': True, 'modified': '2019-09-30T06:18:27.078886Z', 'name': '12:50', 'parent_id': '915833', 'short_name': '12:50 @ Vaal', 
'slug': 'vaal-2019-10-01T12:50', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime'
2019-10-01 12:13:54,610 : INFO : __main__ : Number ofevents: 41
2019-10-01 12:13:54,610 : INFO : __main__ : End page: 0
2019-10-01 12:13:54,611 : INFO : __main__ : Number of events: 41
2019-10-01 14:13:54,717 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 12:43:49.028308
2019-10-01 14:13:54,717 : INFO : __main__ : Previous token WzE2ODUyNTgzLDcxOTQ5MDIwXQ.PwYFEaisbqZZu4zVKAQTfD8rsbw
2019-10-01 14:13:54,955 : INFO : __main__ : Get a new session WzE2ODU1NzQ4LDcxOTYwMjQwXQ.33FaLYZFpmWaN4o59RSMtqzUKsg
2019-10-01 14:13:54,955 : INFO : __main__ : Get avaialble events
2019-10-01 14:13:54,955 : INFO : __main__ : Start page: 0
2019-10-01 14:13:55,080 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-29T09:53:22.083107Z', 'description': '6 furlongs Flat
 Handicap', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/ayr/2019/10/01/201910011420', 'hidden': False, 'id': '18007709',
 'inplay_enabled': True, 'modified': '2019-09-29T09:53:22.083107Z', 'name': '14:20', 'parent_id': '153366', 'short_name': '14:20 @ Ayr', 'slug': 'ay
r-2019-10-01T14:20', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-10-0
2019-10-01 14:13:55,080 : INFO : __main__ : Number ofevents: 34
2019-10-01 14:13:55,080 : INFO : __main__ : End page: 0
2019-10-01 14:13:55,081 : INFO : __main__ : Number of events: 34
2019-10-01 16:13:55,133 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 14:44:07.990087
2019-10-01 16:13:55,133 : INFO : __main__ : Previous token WzE2ODU1NzQ4LDcxOTYwMjQwXQ.33FaLYZFpmWaN4o59RSMtqzUKsg
2019-10-01 16:13:55,400 : INFO : __main__ : Get a new session WzE2ODU4OTU4LDcxOTcxNzg3XQ.uzMjugY0mt9nACGC_cFiG-3s0NY
2019-10-01 16:13:55,400 : INFO : __main__ : Get avaialble events
2019-10-01 16:13:55,401 : INFO : __main__ : Start page: 0
2019-10-01 16:13:55,529 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-29T09:53:25.249439Z', 'description': '7 furlongs 50 y
ards Flat Handicap', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/ayr/2019/10/01/201910011620', 'hidden': False, 'id': '1
8007718', 'inplay_enabled': True, 'modified': '2019-09-29T09:53:25.249439Z', 'name': '16:20', 'parent_id': '153366', 'short_name': '16:20 @ Ayr', 's
lug': 'ayr-2019-10-01T16:20', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '
2019-10-01 16:13:55,529 : INFO : __main__ : Number ofevents: 16
2019-10-01 16:13:55,529 : INFO : __main__ : End page: 0
2019-10-01 16:13:55,529 : INFO : __main__ : Number of events: 16
2019-10-01 18:13:55,592 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 16:43:54.072752
2019-10-01 18:13:55,593 : INFO : __main__ : Previous token WzE2ODU4OTU4LDcxOTcxNzg3XQ.uzMjugY0mt9nACGC_cFiG-3s0NY
2019-10-01 18:13:55,860 : INFO : __main__ : Get a new session WzE2ODYyNDU4LDcxOTg2NzE2XQ.3DzrC5b3J7BuYgGJoatwYGWyDHI
2019-10-01 18:13:55,860 : INFO : __main__ : Get avaialble events
2019-10-01 18:13:55,860 : INFO : __main__ : Start page: 0
2019-10-01 18:13:55,960 : INFO : __main__ : {'events': [{'bettable': True, 'created': '2019-09-29T09:52:21.159371Z', 'description': '7 furlongs Flat
', 'display_order': 0, 'end_date': None, 'full_slug': '/sport/horse-racing/kempton/2019/10/01/201910011840', 'hidden': False, 'id': '18007560', 'inp
lay_enabled': True, 'modified': '2019-09-29T09:52:21.159371Z', 'name': '18:40', 'parent_id': '153011', 'short_name': '18:40 @ Kempton', 'slug': 'kem
pton-2019-10-01T18:40', 'special_rules': None, 'start_date': '2019-10-01', 'start_datetime': '2019-1
2019-10-01 18:13:55,960 : INFO : __main__ : Number ofevents: 4
2019-10-01 18:13:55,960 : INFO : __main__ : End page: 0
2019-10-01 18:13:55,960 : INFO : __main__ : Number of events: 4
2019-10-01 20:13:56,003 : INFO : __main__ : is_active_session: session_time_out 2019-10-01 18:43:46.089804
2019-10-01 20:13:56,003 : INFO : __main__ : Previous token WzE2ODYyNDU4LDcxOTg2NzE2XQ.3DzrC5b3J7BuYgGJoatwYGWyDHI
2019-10-01 20:13:56,283 : INFO : __main__ : Get a new session WzE2ODY2NjUzLDcyMDA3NjIzXQ.tazx1beQFVy_FzlM8l7QseC0aEc
2019-10-01 20:13:56,284 : INFO : __main__ : Get avaialble events
2019-10-01 20:13:56,284 : INFO : __main__ : Start page: 0
2019-10-01 20:13:56,386 : INFO : __main__ : {'events': [], 'pagination': {'next_page': None}}
2019-10-01 20:13:56,387 : INFO : __main__ : Number ofevents: 0
2019-10-01 20:13:56,387 : INFO : __main__ : End page: 0
2019-10-01 20:13:56,387 : INFO : __main__ : Number of events: 0

Hi Miguel,

Your start_datetime_max default value won’t get updated on each loop. Therefore, at the end of the day, you can’t get any more events because the races for the day have now settled.

João

Thank you Joao,
Sorry taking your time with my mistakes.
all the best
miguel