Last active
May 28, 2022 13:49
-
-
Save dubeyji10/a28a70ecd78999ed2e672fb72ff2ea43 to your computer and use it in GitHub Desktop.
generating leads within last 10 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mysql.connector | |
from mysql.connector import Error | |
from datetime import date, time, datetime ,timedelta | |
import time | |
fileName = '2015-01-07' | |
''' | |
for generating payloads - data to be inserted in APIs - with timestamps | |
- a difference of 15 minutes | |
''' | |
# today = '' | |
exampleDate = '2015-01-07' | |
global leads_Query | |
with open('leads/leadsQuery.txt', 'r') as file: | |
data = file.read() | |
leads_Query = data.rstrip() | |
print("running for leads - query : \n",leads_Query) | |
leads_Query_withTimestamp = '' | |
# exampleDate | |
aDate = datetime(2015,1,7,10) | |
# for example start datetime operations at 10:00 am | |
# for example going back in time -- | |
# capital H,M,S - 12 format | |
# | |
def printTimeInterval2(dateTimeObject): | |
global leads_Query_withTimestamp | |
print("waiting for 10 minutes") | |
time.sleep(10) | |
# after 10 seconds later change it to 60*10 -- 10 minutes | |
''' | |
pass the datetime object | |
wait for 10 minutes | |
query all records in the last 10 minutes | |
''' | |
# now = datetime.now() | |
dt_string = dateTimeObject.strftime('%Y-%m-%d %H:%M:%S ') | |
# print("now \t:", dt_string) | |
diff = dateTimeObject + timedelta(minutes=10) | |
dateTimeObject = diff | |
dt_string_diff = diff.strftime('%Y-%m-%d %H:%M:%S ') | |
# print("10 minutes ago : ",dt_string_diff) | |
# print("--> timestamp for difference = {} and {} ".format(dt_string,dt_string_diff)) | |
# print("run this query :\n\n ") | |
# print("updated leads query with timestamp") | |
leads_Query_withTimestamp = leads_Query+" WHERE added_on between '{}' and '{}' ".format(dt_string,dt_string_diff) | |
# print("\n\n\n\n") | |
return diff | |
try: | |
connection = mysql.connector.connect(host='localhost', | |
database='db-name', | |
user='your-username', | |
password='your-password') | |
print('\n\n--connection successful--\n\n') | |
# change to 10*60 -- 10 minute wait | |
# TO-DO could make a variable for 24hr/10 minutes -- when 0 stop the execution | |
while True: | |
# dd/mm/YY H:M:S | |
# this keeps running forever | |
aDate = printTimeInterval2(aDate) | |
# if connection.is_connected(): | |
# db_Info = connection.get_server_info() | |
# print("Connected to MySQL Server version ", db_Info) | |
for_dateTime = leads_Query_withTimestamp | |
print("run this query : \n\n\n",for_dateTime[-73:]) | |
# last 73 characters | |
cursor = connection.cursor() | |
result = cursor.execute(for_dateTime) | |
leadsOutput = cursor.fetchall() | |
outPut = leadsOutput[0] | |
print('-'*50) | |
print("-> query output : \n\n",outPut[0]) | |
print("\n\ntype -> ",type(outPut[0]),len(outPut)) | |
print('-'*50) | |
print('\\/'*25) | |
except Error as e: | |
print("\n\nError while connecting to MySQL", e) | |
finally: | |
if connection.is_connected(): | |
cursor.close() | |
connection.close() | |
print("MySQL connection is closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment