File size: 2,859 Bytes
8d5ab2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
import random
from utils.nyt_api import get_top_stories
from utils.imgflip_api import create_meme
from config.meme_templates import MEME_TEMPLATES, get_template_for_article
from config.settings import NYT_CATEGORIES

st.set_page_config(
    page_title="The Meme York Times",
    page_icon="📰",
    layout="wide"
)

def main():
    # App header
    st.title("The Meme York Times")
    st.subheader("Where News Meets Memes")
    
    # Sidebar for settings
    st.sidebar.title("Settings")
    
    # Category selection
    category = st.sidebar.selectbox(
        "Select News Category",
        options=NYT_CATEGORIES
    )
    
    # Fetch news button
    if st.sidebar.button("Get Meme News"):
        with st.spinner("Fetching the latest news and creating memes..."):
            # Get top stories from NYT API
            articles = get_top_stories(category)
            
            if not articles:
                st.error("Unable to fetch articles. Please try again later.")
                return
            
            # Display article memes in a grid
            col1, col2 = st.columns(2)
            
            for i, article in enumerate(articles[:6]):  # Limit to 6 articles
                # Get a suitable meme template for this article
                template_id, text_fields = get_template_for_article(article)
                
                # Create the meme
                meme_url = create_meme(
                    template_id=template_id,
                    text_fields=text_fields,
                    article_title=article['title'],
                    article_abstract=article.get('abstract', '')
                )
                
                # Display in alternating columns
                current_col = col1 if i % 2 == 0 else col2
                
                with current_col:
                    st.subheader(article['title'])
                    if meme_url:
                        st.image(meme_url, use_column_width=True)
                    else:
                        st.error("Failed to create meme")
                    
                    st.markdown(f"[Read the full article]({article['url']})")
                    st.markdown("---")
    else:
        # Display welcome message
        st.info("Select a news category and click 'Get Meme News' to start!")
        
        # Show random meme templates as examples
        st.subheader("Example Meme Templates")
        cols = st.columns(3)
        sample_templates = random.sample(list(MEME_TEMPLATES.values()), 3)
        
        for col, template in zip(cols, sample_templates):
            with col:
                st.image(f"https://i.imgflip.com/thumbnail/{template['id']}.jpg", 
                        use_column_width=True)
                st.caption(template['name'])

if __name__ == "__main__":
    main()