def companies_by_type(news_type): # list of companies names , type can be good_companies bad_companies if news_type == 'good_companies': match = ['POS', 'POSITIVE'] elif news_type == 'bad_companies': match = ['NEG', 'NEGATIVE'] # articles_cursor = list(mongo.find_matches_containing_many('articles', 'direction', match)) today = datetime.date.today() today_date = '{}/{}/{}'.format(today.month, today.day, today.year) articles_cursor = list(mongo.find_matches_two_fields('articles', 'direction', match, 'subtitle', [today_date])) companies_new_list = [] for article in articles_cursor: comp_dict = mongo.find_one_match('companies', {'name': article.get('company')}) if comp_dict is None: comp_dict = { 'company_name': article.get('company'), 'company_logo': "https://webview.scrader.com/static/images/default.png" } else: comp_dict['company_name'] = comp_dict.pop('name') if not any(d['company_name'] == comp_dict.get('company_name') for d in companies_new_list): companies_new_list.append(comp_dict) return companies_new_list
def get_companies_articles(company): today = datetime.date.today() today_date = '{}/{}/{}'.format(today.month, today.day, today.year) return list(mongo.find_matches_two_fields('articles', 'company', [company], 'subtitle', [today_date] ))
def get_all_news_for_companies(companies_list): today = datetime.date.today() today_date = '{}/{}/{}'.format(today.month, today.day, today.year) requested_news = list(mongo.find_matches_two_fields('articles', 'subtitle', [today_date], 'company', companies_list)) return requested_news
def get_news_by_direction_and_company(company, direction_list, date): # list of news by their direction good bad if date == 'all_news': news_list = list(mongo.find_matches_two_fields('articles', 'company', [company], 'direction', direction_list)) else: today = datetime.date.today() today_date = '{}/{}/{}'.format(today.month, today.day, today.year) news_list = list(mongo.find_matches_three_fields('articles', 'company', [company], 'direction', direction_list, 'subtitle', [today_date])) return news_list
def company_news_type(company_given, news_time): # list of news type a company has if news_time == 'all_news': news_cursor = mongo.find_matches('articles', {'company': company_given}) else: today = datetime.date.today() today_date = '{}/{}/{}'.format(today.month, today.day, today.year) news_cursor = mongo.find_matches_two_fields('articles', 'company', [company_given], 'subtitle', [today_date]) comp_type_of_news = [] for new in news_cursor: if 'POS' in new['direction']: comp_type_of_news.append('good_companies') elif 'NEG' in new['direction']: comp_type_of_news.append('bad_companies') return list(set(comp_type_of_news))
def get_news_by_direction_and_company(company, direction_list): # list of news by their direction good bad news_list = list(mongo.find_matches_two_fields('articles', 'company', [company], 'direction', direction_list)) return news_list
def get_development_news(news_type, page_num, user): elements = [] element = { "title": '', "image_url": '', "subtitle": '', "item_url": '', "buttons": [{ "type": "web_url", "url": '', "title": '' }, { "type": "web_url", "url": '', "title": '' }, { "type": "web_url", "url": '', "title": '' }] } messages = [] message = { "attachment": { "type": "template", "payload": { "template_type": "generic", "elements": elements } } } quick_replies = [] quick_reply = {"title": '', "url": '', "type": "json_plugin_url"} requested_news = list(mongo.find_matches_two_fields('dev_articles', 'checked', [False], 'direction', [news_type])) # print(len(requested_news)) f = lambda A, n=3: [A[i:i + n] for i in range(0, len(A), n)] news_per_page = f(requested_news) # print(news_per_page) news_to_show = news_per_page[int(page_num) - 1] # print(news_to_show) all_quick_replies_page_numbers = [ i + 1 for i, _ in enumerate(news_per_page) ] # print(all_quick_replies_page_numbers) quick_replies_page_numbers_to_show = filter(lambda x: x != int(page_num), all_quick_replies_page_numbers) # print(quick_replies_page_numbers_to_show) for new in news_to_show: element = copy.deepcopy(element) element['title'] = new.get('title')[0:79] element['image_url'] = str(new.get('image_url')) element['subtitle'] = new.get('subtitle') element['item_url'] = str(new.get('item_url')) id = str(new.get('_id')) element['buttons'][0]['url'] = "http://146.185.138.240/checked_article/{}/{}/{}/{}/{}".\ format(news_type, id, 'Correct', page_num, user) element['buttons'][0]['title'] = "Correct Estim" element['buttons'][0]['type'] = "json_plugin_url" element['buttons'][1]['url'] = "http://146.185.138.240/checked_article/{}/{}/{}/{}/{}".\ format(news_type, id, 'Wrong', page_num, user) # print(element['buttons'][1]['url']) element['buttons'][1]['title'] = "Wrong Estim" element['buttons'][1]['type'] = "json_plugin_url" element['buttons'][2]['url'] = "http://146.185.138.240/checked_article/{}/{}/{}/{}/{}".\ format(news_type, id, 'Skip', page_num, user) element['buttons'][2]['title'] = "Skip Estim" element['buttons'][2]['type'] = "json_plugin_url" elements.append(element) num = 0 for page_number in quick_replies_page_numbers_to_show: num += 1 if num > 11: break quick_reply = copy.deepcopy(quick_reply) quick_reply['title'] = "Page {}".format(page_number) quick_reply['url'] = "http://146.185.138.240/dev_news/{}/{}/{}".format( news_type, page_number, user) quick_replies.append(quick_reply) if quick_replies: message['quick_replies'] = quick_replies message['attachment']['payload']['elements'] = elements top_message = {"text": '{} unchecked articles found'. format(len(requested_news))} if int(page_num) == 1: messages.append(top_message) LOG.info(message) # print(message) messages.append(message) response_data = {"messages": messages} return response_data