I try to run a script to extract data from a json file which is updated every 1 to 2 minutes. The basic concept is that the script execute the extraction procedure first and then sleep for 1 minutes and execute the extraction procedure again. it is infinite loop;
It worked fine for more than one month and stopped suddenly one day without any error message, I restarted it and it worked fine. However, after some days it stopped again for no reason.
I have no idea what's the problem and could just provide my script. below is the python file I wrote.
from requests.auth import HTTPBasicAuth
import sys
import requests
import re
import time
import datetime
import json
from CSVFileGen1 import csv_files_generator1
from CSVFileGen2 import csv_files_generator2
from CSVFileGen3 import csv_files_generator3
from CSVFileGen4 import csv_files_generator4
def passpara():
current_time = datetime.datetime.now()
current_time_string = current_time.strftime('%Y-%m-%d %H:%M:%S')
sys.path.append('C:\\semester3\\data_copy\\WAZE\\output_scripts\\TNtool')
FileLocation1 = 'C:\\semester3\\data_copy\\www\\output\\test1'
FileLocation2 = 'C:\\semester3\\data_copy\\www\\output\\test2'
FileLocation3 = 'C:\\semester3\\data_copy\\www\\output\\test3'
FileLocation4 = 'C:\\semester3\\data_copy\\www\\output\\test4'
try:
r1 = requests.get('https://www...=JSON')
json_text_no_lines1 = r1.text
csv_files_generator1(current_time, json_text_no_lines1, FileLocation1)
except requests.exceptions.RequestException as e:
print 'request1 error'
print e
try:
r2 = requests.get('https://www...=JSON')
json_text_no_lines2 = r2.text
csv_files_generator2(current_time, json_text_no_lines2, FileLocation2)
except requests.exceptions.RequestException as e:
print 'request2 error'
print e
try:
r3 = requests.get('https://www...=JSON')
json_text_no_lines3 = r3.text
csv_files_generator3(current_time, json_text_no_lines3, FileLocation3)
except requests.exceptions.RequestException as e:
print 'request3 error'
print e
try:
r4 = requests.get('https://www...JSON')
json_text_no_lines4 = r4.text
csv_files_generator4(current_time, json_text_no_lines4, FileLocation4)
except requests.exceptions.RequestException as e:
print 'request4 error'
print e
print current_time_string + ' Data Operated. '
while True:
passpara()
time.sleep(60)
Here is the CSVFileGen1 that the first script calls. This script parses the json file and saves the information to a csv file.
import json
import datetime
import time
import os.path
import sys
from datetime import datetime
from dateutil import tz
def meter_per_second_2_mile_per_hour(input_meter_per_second):
return input_meter_per_second * 2.23694
def csv_files_generator1(input_datetime, input_string, target_directory):
try:
real_json = json.loads(input_string)
#get updatetime string
updatetime_epoch = real_json['updateTime']
update_time = datetime.fromtimestamp(updatetime_epoch/1000)
updatetime_string = update_time.strftime('%Y%m%d%H%M%S')
file_name = update_time.strftime('%Y%m%d%H%M')
dir_name = update_time.strftime('%Y%m%d')
if not os.path.exists(target_directory + '\\' + dir_name):
os.makedirs(target_directory + '\\' + dir_name)
if not os.path.isfile(target_directory + '\\' + dir_name + '\\' + file_name):
......#some detailed information I delete it for simplicity
except ValueError, e:
print e