Skip to content

Commit

Permalink
Merge branch 'dev' into feature/BE-US-stocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hikasap authored Dec 16, 2024
2 parents 33be45f + 3c9490f commit 4915e19
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 110 deletions.
4 changes: 3 additions & 1 deletion backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ services:
python3 manage.py migrate --noinput &&
python3 manage.py collectstatic --noinput &&
python3 manage.py update_currencies &&
python3 manage.py update_stocks &&
python3 manage.py update_US_stocks &&
python3 manage.py update_stocks &&
python3 manage.py generate_posts &&
python3 manage.py update_indices &&
gunicorn backend.wsgi:application --bind 0.0.0.0:8000'
restart: always
environment:
Expand Down
81 changes: 81 additions & 0 deletions backend/marketfeed/management/commands/generate_posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import random
from faker import Faker
from django.core.management.base import BaseCommand
from marketfeed.models import Post
from onboarding.models import User

class Command(BaseCommand):
help = 'Generate fake data for the Post model'

def add_arguments(self, parser):
parser.add_argument('total', type=int, help='Number of fake posts to generate')

def handle(self, *args, **kwargs):
fake = Faker()
total = kwargs['total']

economy_topics = [
f"Why is everyone talking about {fake.bs()}?"[:100],
f"Is {fake.bs()} really the future?"[:100],
f"{fake.company()} just did something amazing with {fake.bs()}!"[:100],
f"I can’t believe how {fake.bs()} is changing the game"[:100],
f"Anyone else seeing the hype around {fake.bs()}?"[:100],
f"{fake.bs()} and what it means for us all"[:100],
f"The cool ways {fake.company()} uses {fake.bs()}"[:100],
f"{fake.currency_name()} in {fake.bs()}—what’s happening?"[:100],
f"Big moves in {fake.bs()}—check this out!"[:100],
f"How {fake.bs()} is reshaping the world"[:100],
f"What’s your take on {fake.bs()} trends?"[:100],
f"Let’s talk about {fake.bs()} innovations"[:100],
f"I just read about {fake.bs()} and it’s wild"[:100],
f"{fake.bs()} in the news—here’s what I think"[:100],
f"Do you think {fake.bs()} is overhyped?"[:100],
f"{fake.bs()} updates—what’s happening now?"[:100],
f"How {fake.company()} is mastering {fake.bs()}"[:100],
f"{fake.bs()} is trending, but why?"[:100],
f"Let’s chat about {fake.bs()} strategies"[:100],
f"What’s so exciting about {fake.bs()}?"[:100]
]

economy_contents = [
f"I’ve been thinking a lot about {fake.bs()} lately. It feels like everywhere I look, someone is talking about how it’s changing the world! What are your thoughts?",
f"{fake.bs()} might sound boring to some, but it’s a topic I can’t stop researching. The way {fake.company()} is involved is fascinating!",
f"Anyone else worried about how {fake.currency_name()} is affecting {fake.bs()}? I think it’s time we started discussing this more seriously.",
f"I just read an article on {fake.bs()}, and it’s blowing my mind. How are companies like {fake.company()} staying ahead of this trend?",
f"Sometimes I wonder if {fake.bs()} is just a fad or if it’s truly the future of {fake.currency_name()} economics. What do you think?",
f"Every time I think I understand {fake.bs()}, something new comes up. It’s like this endless puzzle, especially when you consider what {fake.company()} is doing!",
f"Feeling inspired by a podcast I listened to about {fake.bs()} today. They mentioned {fake.company()} and how they’re leveraging it—so cool!",
f"If you’re not paying attention to {fake.bs()}, you’re missing out. I’ve been diving into how it’s reshaping industries, and it’s incredible.",
f"Spent my afternoon reading about {fake.bs()} and drinking coffee. It’s crazy how fast things are changing in this space!",
f"Is it just me, or does it seem like {fake.bs()} is becoming a bigger deal every day? Let’s talk about how this impacts our daily lives.",
f"Have you seen the latest about {fake.company()}? Their work in {fake.bs()} is super innovative! Makes me want to learn more.",
f"I’ve been dabbling in some research about {fake.bs()}, and I think I’m onto something exciting. Let’s connect if you’re interested!",
f"People keep saying {fake.bs()} is the key to the future. I wonder if we’re all ready for what’s coming next.",
f"Big shoutout to {fake.company()} for leading the way in {fake.bs()}. It’s companies like this that make me hopeful for what’s ahead.",
f"The intersection of {fake.bs()} and everyday life is more significant than we think. It’s something I’ve been reflecting on lately.",
f"Had an enlightening conversation with a friend about {fake.bs()} and how it’s changing the game for companies like {fake.company()}.",
f"Woke up with {fake.bs()} on my mind (yes, I’m that person now). The future is looking so dynamic and exciting!",
f"There’s something about {fake.bs()} that feels so revolutionary. Anyone else digging into how it affects {fake.currency_name()}?",
f"What role do you think {fake.company()} plays in the whole {fake.bs()} evolution? Let’s debate in the comments!"
]

users = list(User.objects.all())
if not users:
self.stdout.write(self.style.ERROR('No users found in the database. Please create some users first.'))
return

for _ in range(total):
title = random.choice(economy_topics)
content = random.choice(economy_contents)
author = random.choice(users)

post = Post(
title=title,
content=content,
author=author,
created_at=fake.date_time_this_year(),
updated_at=fake.date_time_this_year(),
)
post.save()

self.stdout.write(self.style.SUCCESS(f'Successfully created {total} fake posts.'))
63 changes: 63 additions & 0 deletions backend/marketfeed/management/commands/update_indices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import requests
from bs4 import BeautifulSoup
from django.core.management.base import BaseCommand
import json
from django.conf import settings
import os
from marketfeed.serializers import *
from marketfeed.models import *

class Command(BaseCommand):
help = 'Update or Insert the supported indices to db'

def handle(self, *args, **kwargs):
url = "https://www.kap.org.tr/en/Endeksler"

file_path = os.path.join(settings.BASE_DIR,'marketfeed', 'management', 'indices.json')
if not os.path.exists(file_path):
self.stdout.write(self.style.ERROR(f'Index data file not found: {file_path}'))
return
with open(file_path, 'r') as file:
indices = json.load(file)
response = requests.get(url)
response.raise_for_status()

soup = BeautifulSoup(response.text, "html.parser")

index_elements = soup.find_all("div", class_="vcell")

for index_element in index_elements:
index_name = index_element.text.strip()

if index_name not in indices:
continue
# Get the index name
next_div = index_element.find_next("div", class_="column-type7 wmargin")
stock_symbols = []
if next_div:
stock_elements = next_div.find_all("div", class_="comp-cell _02 vtable")
for stock_element in stock_elements:
stock_name = stock_element.find("a", class_="vcell").text.strip() if stock_element.find("a", class_="vcell") else None

if stock_name:
stock_symbols.append(stock_name)

stocks_in_index = Stock.objects.filter(symbol__in=stock_symbols)
currency = Currency.objects.get(code='TRY')
symbol = indices[index_name]


try:
index_obj, created = Index.objects.update_or_create(
name = index_name,
symbol = symbol,
currency = currency,
#stocks = stocks_in_index
)

index_obj.stocks.add(*stocks_in_index)
except Exception as e:
print(e)



46 changes: 46 additions & 0 deletions backend/marketfeed/management/indices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"BIST 100": "XU100",
"BIST 50": "XU050",
"BIST 30": "XU030",
"BIST LIQUID BANKS": "XLBNK",
"BIST LIQUID 10 EX BANKS": "X10XB",
"BIST STARS": "XYLDZ",
"BIST IPO": "XHARZ",
"BIST SME INDUSTRIAL": "XKOBI",
"BIST DIVIDEND 25": "XTM25",
"BIST CORPORATE GOVERNANCE": "XKURY",
"BIST SUSTAINABILITY 25": "XSD25",
"BIST INDUSTRIALS": "XUSIN",
"BIST FOOD, BEVERAGE": "XGIDA",
"BIST CHEM., PETROL, PLASTIC": "XKMYA",
"BIST MINING": "XMADN",
"BIST BASIC METAL": "XMANA",
"BIST METAL PRODUCTS, MACH.": "XMESY",
"BIST WOOD, PAPER, PRINTING": "XKAGT",
"BIST NON-METAL MIN. PRODUCT": "XTAST",
"BIST TEXTILE, LEATHER": "XTEKS",
"BIST SERVICES": "XUHIZ",
"BIST ELECTRICITY": "XELKT",
"BIST TELECOMMUNICATION": "XILTM",
"BIST CONSTRUCTION": "XINSA",
"BIST SPORTS": "XSPOR",
"BIST W. AND RETAIL TRADE": "XTCRT",
"BIST TOURISM": "XTRZM",
"BIST TRANSPORTATION": "XULAS",
"BIST FINANCIALS": "XUMAL",
"BIST BANKS": "XBANK",
"BIST INSURANCE": "XSGRT",
"BIST LEASING FACTORING": "XFINK",
"BIST HOLD. AND INVESTMENT": "XHOLD",
"BIST REAL EST. INV. TRUSTS": "XGMYO",
"BIST BROKERAGE HOUSES": "XAKUR",
"BIST INVESTMENT TRUSTS": "XYORT",
"BIST TECHNOLOGY": "XUTEK",
"BIST INF. TECHNOLOGY": "XBLSM",
"BIST PARTICIPATION ALL SHARES": "XKTUM",
"BIST PARTICIPATION 100": "XK100",
"BIST PARTICIPATION DIVIDEND": "XKTMT",
"BIST SUSTAINABILITY PARTICIPATION": "XSRDK",
"BIST BUYBACK": "XUGRA"
}

18 changes: 18 additions & 0 deletions backend/marketfeed/migrations/0010_alter_post_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2024-12-16 15:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('marketfeed', '0009_post_stocks'),
]

operations = [
migrations.AlterField(
model_name='post',
name='title',
field=models.CharField(max_length=100),
),
]
2 changes: 1 addition & 1 deletion backend/marketfeed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Portfolio(models.Model):


class Post(models.Model):
title = models.CharField(max_length=50)
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
3 changes: 2 additions & 1 deletion backend/marketfeed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,9 @@ def list(self, request):
)
for index in serializerData
]
if not symbols:
return Response([], status=status.HTTP_200_OK)
data = yf.download(tickers=symbols, period="1d", interval="1d")

prices = {
symbol.split(".")[0]: float(data["Close"][symbol]) for symbol in symbols
}
Expand Down
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ feedparser
yfinance
requests
beautifulsoup4
Faker==18.11.1

1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
python3 manage.py collectstatic --noinput &&
python3 manage.py update_currencies &&
python3 manage.py update_stocks &&
python3 manage.py update_indices &&
gunicorn backend.wsgi:application --bind 0.0.0.0:8000'
restart: always
environment:
Expand Down
Loading

0 comments on commit 4915e19

Please # to comment.