Esempio n. 1
0
    def get_chart_important_news_maker(self, _user):
        def get_news(__m):
            __a = []
            for i in _user['news_content']['news_maker']:
                if i['news_maker'] == __m['_id']:
                    __a.append(i['news'])
            return __a

        def get_direction(__news):
            for _i in _user['news_content']['direction']:
                if _i['news'] == __news:
                    return _i['direction']
            return False

        try:
            count_all = 0
            categories = []
            directions = DirectionModel().get_all('content')['value']
            series = []

            for _d in directions:
                series.append(dict(id=str(_d['id']), name=_d['name'], data=[]))
            series.append(dict(id='no_direction', name='بدون جهت گیری', data=[]))

            news_makers = _user['content']['news_maker']

            for m in news_makers:
                direction_count = dict(no_direction=0)
                for _d in directions:
                    direction_count[str(_d['id'])] = 0

                news = get_news(m)
                for n in news:
                    __dir = get_direction(n)

                    if __dir is not False:
                        direction_count[str(__dir)] += 1
                    else:
                        direction_count['no_direction'] += 1

                for s in series:
                    try:
                        s['data'].append(direction_count[s['id']])
                    except:
                        s['data'].append(0)
                categories.append(m['name'])
            self.result['value'] = dict(series=series, categories=categories, count_all=count_all)
            self.result['status'] = True
            return self.result

        except:
            Debug.get_exception(send=False)
            return self.result
Esempio n. 2
0
    def get_chart_agency_direction(self):
        try:
            count_direction = DirectionModel().count_all('source')['value']
            __directions = self.get_top_elements("direction", count_direction)
            contents = []
            count_all = 0
            categories = []
            series = [dict(name='جهت گیری', data=[])]
            for _dir in __directions:
                try:
                    direction = DirectionModel(_id=ObjectId(_dir['key'])).get_one()['value']
                    categories.append(direction['name'])
                    series[0]['data'].append(_dir['doc_count'])
                    contents.append(dict(id=_dir['key'], title=direction['name'], value=_dir['doc_count']))

                    count_all += _dir['doc_count']
                except:
                    pass

            __directions = self.get_top_directions(count_direction, 3)
            directions = []
            for _dir in __directions:
                try:
                    direction = DirectionModel(_id=ObjectId(_dir['key'])).get_one()['value']
                    _d = dict(name=direction['name'], count=_dir['doc_count'], agencies=[])
                    for _ag in _dir['agencies']:
                        agency = AgencyModel(_id=ObjectId(_ag['key'])).get_one()
                        _d['agencies'].append(dict(name=agency['name'], count=_ag['doc_count']))
                    directions.append(_d)
                except:
                    pass
            self.result['value'] = dict(directions=directions, contents=contents, series=series, categories=categories, count_all=count_all)
            self.result['status'] = True
            return self.result

        except:
            Debug.get_exception(send=False)
            return self.result
Esempio n. 3
0
    def get_agency(self, agency):
        try:
            try:
                category = CategoryModel(
                    _id=agency['category']).get_one()['value']
            except:
                category = None
            try:
                direction = DirectionModel(
                    _id=agency['direction']).get_one()['value']
            except:
                direction = None
            try:
                links = sorted(agency['links'],
                               key=lambda k: k['sort'],
                               reverse=False)
            except:
                links = agency['links']
            rss_list = agency['rss_list'] if 'rss_list' in agency else []
            try:
                rss_list = sorted(agency['rss_list'],
                                  key=lambda k: k['sort'],
                                  reverse=False)
            except:
                pass

            self.value.append(
                dict(id=agency['_id'],
                     name=agency['name'],
                     link=agency['link'],
                     color=agency['color'],
                     float_left=agency['float_left'],
                     type=agency['type'] if 'type' in agency else 'SITE',
                     rss_list=rss_list,
                     comparatives=agency['comparatives']
                     if 'comparatives' in agency else [],
                     copy_key_words=agency['copy_key_words']
                     if 'copy_key_words' in agency.keys() else [],
                     category=category,
                     direction=direction,
                     active=agency['active'],
                     pic=agency['pic'],
                     base_link=agency['base_link'],
                     links=links,
                     add_by_confirm=agency['add_by_confirm'],
                     extract_image=agency['extract_image']))
        except:
            Debug.get_exception(send=False)
            pass
Esempio n. 4
0
    def get_general_statistic_agency(self, direction):
        def get_count_direction(__agency, __dir):
            a = 0
            for _i in direction:
                if _i['agency'] == __agency and _i['direction'] == __dir:
                    a += 1
            return a
        try:
            contents = []
            directions = DirectionModel().get_all('content')['value']
            count_agency = AgencyModel().count_all()['value']
            __agencies = self.get_top_elements("agency", count_agency)
            for ag in __agencies:
                agency = AgencyModel(_id=ObjectId(ag['key'])).get_one()
                __a = dict(title=agency['name'], value=ag['doc_count'], direction=[])
                for _dir in directions:
                    __a['direction'].append(get_count_direction(ObjectId(ag['key']), _dir['id']))
                contents.append(__a)
            count_all = dict(no_direction=0, dir_count=[], total=0)
            for i in contents:
                __a = 0
                for j in range(len(i['direction'])):
                    __a += i['direction'][j]
                    try:
                        count_all['dir_count'][j] += i['direction'][j]
                    except:
                        count_all['dir_count'].append(i['direction'][j])
                i['no_direction'] = i['value'] - __a
                count_all['total'] += i['value']
                count_all['no_direction'] += i['no_direction']
            self.result['value'] = dict(contents=contents, count_all=count_all, directions=directions)
            self.result['status'] = True
            return self.result

        except:
            Debug.get_exception(send=False)
            return self.result
Esempio n. 5
0
    def get_chart_content_direction(self, direction):
        def get_direction(__news):
            for _i in direction:
                if _i['news'] == __news:
                    return _i['direction']
            return False

        def get_count_direction(__agency, __dir):
            a = 0
            for _i in direction:
                if _i['agency'] == __agency and _i['direction'] == __dir:
                    a += 1
            return a
        try:
            directions = DirectionModel().get_all('content')['value']
            key_word_query = KeyWordClass(user_keyword=self.user_keyword).get_query_keyword()
            direction_count = dict(no_direction=0)
            contents = []
            series = [dict(id='no_direction', name='بدون جهت گیری', data=[])]
            for _d in directions:
                series.append(dict(id=str(_d['id']), name=_d['name'], data=[]))
                contents.append(dict(id=str(_d['id']), title=_d['name'], value=0))
                direction_count[str(_d['id'])] = 0

            body = {
                "size": 1000000,
                "fields": ["_id", "agency"],
                "query": {
                    "filtered": {
                        "filter": {
                            "and": {
                                "filters": [{
                                    "range": {
                                        "date": {
                                            "lt": self.end.isoformat(),
                                            "gte": self.start.isoformat()
                                        }
                                    }
                                }] + key_word_query
                            }
                        }
                    }
                }
            }
            news = ElasticSearchModel(doc_type=self.doc_type, body=body).search()

            try:
                count_all = news['hits']['total']
            except:
                count_all = 0
            for n in news['hits']['hits']:
                __dir = get_direction(n['_id'])

                if __dir is not False:
                    direction_count[str(__dir)] += 1
                else:
                    direction_count['no_direction'] += 1

            for s in contents:
                try:
                    s['value'] = direction_count[s['id']]
                except:
                    s['value'] = 0

            categories = []
            count_agency = AgencyModel().count_all()['value']
            __agencies = self.get_top_elements("agency", count_agency)
            for ag in __agencies:
                direction_count = dict(no_direction=0)
                for _d in directions:
                    direction_count[str(_d['id'])] = 0
                agency = AgencyModel(_id=ObjectId(ag['key'])).get_one()
                _c = 0
                for _dir in directions:
                    _x = get_count_direction(ObjectId(ag['key']), _dir['id'])
                    direction_count[str(_dir['id'])] = _x
                    _c += _x
                series[0]['data'].append(ag['doc_count'] - _c)
                for s in series[1:]:
                    try:
                        s['data'].append(direction_count[str(s['id'])])
                    except:
                        s['data'].append(0)

                categories.append(agency['name'])

            self.result['value'] = dict(contents=contents, series=series, categories=categories, count_all=count_all)
            self.result['status'] = True
            return self.result

        except:
            Debug.get_exception(send=False)
            return self.result
Esempio n. 6
0
    def get_important_keyword_news(self, direction):
        def get_direction(__news):
            for _i in direction:
                if _i['news'] == __news:
                    return _i['direction']
            return False
        try:
            keyword_queries = KeyWordClass(user_keyword=self.user_keyword).get_list_query_keyword()
            directions = DirectionModel().get_all('content')['value']
            series = []

            for _d in directions:
                series.append(dict(id=str(_d['id']), name=_d['name'], data=[]))
            series.append(dict(id='no_direction', name='بدون جهت گیری', data=[]))
            categories = []
            keywords = []
            for query in keyword_queries:
                direction_count = dict(no_direction=0)
                for _d in directions:
                    direction_count[str(_d['id'])] = 0
                body = {
                    "size": 1000000,
                    "fields": ["_id"],
                    "query": {
                        "filtered": {
                            "filter": {
                                "and": {
                                    "filters": [{
                                        "range": {
                                            "date": {
                                                "lt": self.end.isoformat(),
                                                "gte": self.start.isoformat()
                                            }
                                        }
                                    }] + [query['query']]
                                }
                            }
                        }
                    }
                }
                news = ElasticSearchModel(doc_type=self.doc_type, body=body).search()
                try:
                    count_all = news['hits']['total']
                except:
                    count_all = 0
                for n in news['hits']['hits']:
                    __dir = get_direction(n['_id'])

                    if __dir is not False:
                        direction_count[str(__dir)] += 1
                    else:
                        direction_count['no_direction'] += 1

                for s in series:
                    try:
                        s['data'].append(direction_count[s['id']])
                    except:
                        s['data'].append(0)
                keywords.append(dict(keyword=query['keyword'], count=count_all))
                categories.append(query['keyword'])

            keywords = sorted(keywords, key=lambda k: k['count'], reverse=False)[:3]
            self.result['value'] = dict(categories=categories, series=series, keywords=keywords)
            self.result['status'] = True
            return self.result

        except:
            Debug.get_exception(send=False)
            return self.result