update economic function

This commit is contained in:
Justin 2023-06-14 17:00:48 +08:00 committed by GitHub
parent 33461dd190
commit 4225df4e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -698,6 +698,116 @@ def updateForex(api_key, logf):
def updateEconomic(api_key, logf):
try:
with open('csv/economic_settings.json','r') as f:
all_economic_settings = json.load(f)
except:
all_economic_settings = {"feature": "Economic Calendar", "speed": "medium", "speed2": "medium", "animation": "up", "importance": "Med - High", "title": True, "timezone": "US/Eastern", "countries": ["United States"], "events": []}
try:
url = 'https://economic-calendar.tradingview.com/events'
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
country_codes = {
'United States':'US', 'Canada':'CA', 'Hong Kong':'HK', 'China':'CN', 'Germany':'DE','France':'FR', 'Italy':'IT', 'Singapore':'SG',
'South Korea':'KR', 'Japan':'JP', 'Australia':'AU', 'New Zealand':'NZ', 'India':'IN', 'Switzerland':'CH', 'United Kingdom':'GB',
'Spain':'ES', 'Portugal':'PT', 'Netherlands':'NL', 'Belgium':'BE', 'Austria':'AT', 'Denmark':'DK', 'Turkey':'TR', 'Brazil':'BR',
'Mexico':'MX', 'Sweden':'SE', 'Finland':'FI', 'Norway':'NO', 'Taiwan':'TW', 'Indonesia':'ID', 'Philippines':'PH', 'Thailand':'TH',
'South Africa':'ZA'}
country = all_economic_settings['countries']
if all_economic_settings['importance'] == 'Low - High'
importance = -1
elif all_economic_settings['importance'] == 'Med - High'
importance = 0
elif all_economic_settings['importance'] == 'High'
importance = 1
country_codes_list = [country_codes[c] for c in country]
result = ",".join(country_codes_list)
timezone = pytz.timezone(all_economic_settings['timezone'])
# date_local = datetime.now(timezone)
# date_local = (date_local - timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:00')
date_local = datetime.now(timezone).strftime('%Y-%m-%dT00:00:00')
date_local2 = datetime.now(timezone).strftime('%Y-%m-%dT23:59:00')
date_from = timezone.localize(datetime.strptime(date_local,'%Y-%m-%dT%H:%M:%S')).astimezone(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%S.000Z')
date_to = timezone.localize(datetime.strptime(date_local2,'%Y-%m-%dT%H:%M:%S')).astimezone(pytz.timezone('GMT')).strftime('%Y-%m-%dT%H:%M:%S.000Z')
params = {
'from': date_from,
'to': date_to,
'countries': result,
'minImportance': importance
}
data = requests.get(url, params=params, headers=headers).json()
events = []
try:
for each in data['result']:
try:
print(each['actual'], each['previous'], each['forecast'])
time = pytz.timezone('GMT').localize(datetime.strptime(each['date'], '%Y-%m-%dT%H:%M:%S.%fZ')).astimezone(timezone).strftime('%a %m/%d %H:%M%p')
if datetime.now(timezone).strftime('%a %m/%d %H:%M%p') >= time:
happened = True
else:
happened = False
try:
unit = str(each['unit'])
except:
unit = ''
try:
scale = str(each['scale'])
except:
scale = ''
if scale != '':
if each['actual'] != '' and each['actual'] is not None:
each['actual'] = str(each['actual']) + scale
if each['previous'] != '' and each['previous'] is not None:
each['previous'] = str(each['previous']) + scale
if each['forecast'] != '' and each['forecast'] is not None:
each['forecast'] = str(each['forecast']) + scale
if unit != '' and scale != '':
if each['actual'] != '' and each['actual'] is not None:
each['actual'] = str(each['actual']) + ' ' + unit
if each['previous'] != '' and each['previous'] is not None:
each['previous']= str(each['previous']) + ' ' + unit
if each['forecast'] != '' and each['forecast'] is not None:
each['forecast'] = str(each['forecast']) + ' ' + unit
elif unit != '' and scale == '':
if each['actual'] != '' and each['actual'] is not None:
each['actual'] = str(each['actual']) + unit
if each['previous'] != '' and each['previous'] is not None:
each['previous']= str(each['previous']) + unit
if each['forecast'] != '' and each['forecast'] is not None:
each['forecast'] = str(each['forecast']) + unit
events.append({
'title':each['title'],
'indicator':each['indicator'],
'period':each['period'],
'source':each['source'],
'country':each['country'],
'currency':each['currency'],
'importance':each['importance'],
'time':time,
'actual':each['actual'],
'previous':each['previous'],
'forecast':each['forecast'],
'happened': happened
})
except:
pass
except:
pass
all_economic_settings['events'] = events
with open('csv/economic_settings.json', 'w') as f:
f.write(all_economic_settings)
except:
pass
def updateNews(api_key, logf):