-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/be daily currency stock update #442
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e51ac23
feat: new serializer for post to not call api at post request
cemgungor1 f2d3653
feat: command to add supported currencies to db at each build
cemgungor1 718ab41
feat: add or update the db with all Turkish Stocks
cemgungor1 c634f51
feat: update stocks on build
cemgungor1 c2e7074
fix: merge conflict
cemgungor1 f1ad64c
Merge branch 'dev' into feature/BE-daily-currency-stock-update
rukiyeaslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"} | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's discuss other currencies, so stocks, in the future |
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!