-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from bounswe/feature/BE-daily-currency-stock-…
…update Adds daily currency stock update
- Loading branch information
Showing
9 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Empty file.
Empty file.
36 changes: 36 additions & 0 deletions
36
backend/marketfeed/management/commands/update_currencies.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
from django.core.management.base import BaseCommand | ||
from marketfeed.models import Currency | ||
from django.conf import settings | ||
import os | ||
|
||
class Command(BaseCommand): | ||
help = 'Update or Insert the supported currencies to db' | ||
|
||
def handle(self, *args, **kwargs): | ||
# Define the file path (assuming it's inside a 'currency_data' folder) | ||
file_path = os.path.join(settings.BASE_DIR,'marketfeed', 'management', 'currencies.json') | ||
|
||
# Check if the file exists | ||
if not os.path.exists(file_path): | ||
self.stdout.write(self.style.ERROR(f'Currency data file not found: {file_path}')) | ||
return | ||
|
||
with open(file_path, 'r') as file: | ||
currencies_data = json.load(file) | ||
|
||
for currency_data in currencies_data: | ||
code = currency_data.get('code') | ||
name = currency_data.get('name') | ||
|
||
# Get or create the currency | ||
currency, created = Currency.objects.get_or_create( | ||
code=code, | ||
defaults={'name': name} | ||
) | ||
|
||
# Output the result | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f'Inserted {currency.code} into the database.')) | ||
else: | ||
self.stdout.write(self.style.SUCCESS(f'{currency.code} already exists in the database.')) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
from django.core.management.base import BaseCommand | ||
from marketfeed.models import Stock, Currency | ||
from django.conf import settings | ||
import requests | ||
import re | ||
|
||
# Search whether the stock name has FON, FONU or BYF in it to pass those: they are not stocks | ||
def notStock(stockName): | ||
return bool(re.search(r'\b(FON|FONU|BYF)\b', stockName, re.IGNORECASE)) | ||
|
||
class Command(BaseCommand): | ||
help = 'Update or Insert the Turkish stock market stocks to db' | ||
|
||
|
||
def handle(self, *args, **kwargs): | ||
# Url to fetch stock list | ||
url = 'https://bigpara.hurriyet.com.tr/api/v1/hisse/list' | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
stocks = response.json().get('data', []) | ||
currency = Currency.objects.get(code='TRY') | ||
for stock in stocks: | ||
try: | ||
if notStock(stock['ad']): | ||
continue | ||
Stock.objects.update_or_create( | ||
symbol=stock['kod'], | ||
defaults={ | ||
'name': stock['ad'], | ||
'currency': currency, | ||
} | ||
) | ||
except Exception as e: | ||
print(e) | ||
except Exception as e: | ||
print(e) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{"code": "TRY", "name": "Turkish Lira"}, | ||
{"code": "USD", "name": "US Dollar"} | ||
] | ||
|
This file contains 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
This file contains 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
This file contains 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