Skip to content

Commit

Permalink
Add breaking news feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mas-4 committed Aug 24, 2024
1 parent 43c0041 commit d440bb4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/site/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def get_main_headline_df():
'title': Headline.processed,
'agency': Agency.name,
'bias': Agency._bias, # noqa prot attr
'appearance': Article.first_accessed,
'first_accessed': Headline.first_accessed,
'last_accessed': Headline.last_accessed,
'position': Headline.position,
Expand Down Expand Up @@ -213,6 +214,9 @@ def get_main_headline_df():
if len(df) == 0:
sys.exit("No headlines found. Exiting.")

df['appearance'] = df['appearance'].dt.tz_localize('utc').dt.tz_convert('US/Eastern')
now = pd.Timestamp.now(tz='US/Eastern')
df['howlong'] = (now - df['appearance']).dt.total_seconds()
df['first_accessed'] = df['first_accessed'].dt.tz_localize('utc').dt.tz_convert('US/Eastern')
df['last_accessed'] = df['last_accessed'].dt.tz_localize('utc').dt.tz_convert('US/Eastern')
roundcol = ['vader_compound', 'afinn', 'topic_score']
Expand Down
10 changes: 9 additions & 1 deletion app/site/page_headlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def cluster_and_summarize(self, df):
clusters_list = [{'cluster': key, 'data': group.to_dict(orient='records')} for key, group in grouped]
for cluster in clusters_list:
cluster['coverage'] = round(len(cluster['data']) / len(Scrapers) * 100, 2)
cluster['first'] = max(cluster['data'], key=lambda x: x['howlong'])['howlong']

clusters_list.sort(key=lambda x: len(x['data']), reverse=True)
# clusters_list.sort(key=lambda x: len(x['data']), reverse=True)
clusters_list.sort(key=lambda x: x['first'])
self.make_agency_lists(clusters_list)
self.context['clusters'] = clusters_list

Expand All @@ -114,6 +116,12 @@ def make_agency_lists(self, clusters_list):
for cluster in clusters_list:
cluster['data'].sort(key=lambda x: x['agency'])
hrefs = [f"<p>{len(cluster['data'])} headlines / {cluster['coverage']}% coverage</p>"]
# if cluster['first'] / 60 < 4 * 60:
minutes = cluster['first'] // 60
if minutes < 90:
hrefs[-1] = hrefs[-1] + f'<h3>🚨🚨🚨BREAKING! {int(minutes)}m ago! </h3>'
else:
hrefs[-1] = hrefs[-1] + f'<p>First broken {int(cluster["first"] // 60 // 60)} hours ago</p>'
last_bias = -3
for a in sorted(cluster['data'], key=lambda x: x['bias']):
if a['bias'] != last_bias:
Expand Down

0 comments on commit d440bb4

Please sign in to comment.