示例#1
0
def search():
    ''' function to create search bar'''
    if not g.search_form.validate():
        return redirect(url_for('news_sites'))

    news, total = News.search(g.search_form.q.data)

    new_list = []
    bbc_list = []
    ger_list = []
    abc_list = []
    guardian_list = []
    # print("now is test")
    for new in list(news):
        link = new.link
        title = new.title
        summary = new.summary
        language = new.language
        if len(summary) > 300:
            summary = summary[0:300] + '...'
        ## to deal with cartoons ##
        elif 'cartoon' in title:
            title = title.replace('- cartoon', '')
            summary = f'Cartoon: {title}'
        outlet = new.outlet
        pic_link = new.pic_link
        if pic_link == None:
            pic_link = "https://static.bbc.co.uk/news/1.312.03569/img/brand/generated/news-light.png"
        if outlet == 'theguardian':
            outlet = 'The Guardian'
            summary = title
        if outlet == 'Ger':
            outlet = 'Der Spiegel'
            summary = ''
        if outlet == 'Spa':
            outlet = 'El Pais'

        if outlet == 'BBC':
            bbc_list.append(
                (link, title, summary, outlet, language, new, pic_link))
        if outlet == 'ABC':
            abc_list.append(
                (link, title, summary, outlet, language, new, pic_link))
        if outlet == 'Der Spiegel':
            ger_list.append(
                (link, title, summary, outlet, language, new, pic_link))
        if outlet == 'The Guardian':
            guardian_list.append(
                (link, title, summary, outlet, language, new, pic_link))

    for i in range(len(bbc_list)):
        new_list.append(bbc_list.pop())
        if len(guardian_list) > 0:
            new_list.append(guardian_list.pop())
        if len(abc_list) > 0:
            new_list.append(abc_list.pop())
        if len(ger_list) > 0:
            new_list.append(ger_list.pop())

    return render_template('search.html', title=_('Search'), news=new_list)
示例#2
0
def search_topics(topic, category):
    '''function to search through the topics and give news articles using elasticsearch'''

    #print(f'search topic is: {topic}')

    news, total = News.search(topic)

    new_list = []
    bbc_list = []
    ger_list = []
    abc_list = []
    guardian_list = []
    titles = []
    # print("now is test")
    for new in list(news):
        link = new.link
        title = new.title
        summary = new.summary
        language = new.language
        category_database = new.category
        if len(summary) > 300:
            summary = summary[0:300] + '...'
        ## to deal with cartoons ##
        elif 'cartoon' in title:
            title = title.replace('- cartoon', '')
            summary = f'Cartoon: {title}'
        outlet = new.outlet
        pic_link = new.pic_link
        if pic_link == None:
            pic_link = "https://static.bbc.co.uk/news/1.312.03569/img/brand/generated/news-light.png"
        if outlet == 'theguardian':
            outlet = 'The Guardian'
            summary = title
        if outlet == 'Ger':
            outlet = 'Der Spiegel'
        if outlet == 'Spa':
            outlet = 'El Pais'
        if title not in titles:
            titles.append(title)
            if category.lower() == category_database.lower():
                topic = topic.lower()
                new_list.append(
                    (link, title, summary, outlet, language, new, pic_link))
    return render_template('search_topics.html',
                           title=_('Search'),
                           topic=topic,
                           news=new_list)