Exemplo n.º 1
0
 def _get_resources(self):
     topic = self.request.GET.get('topic')
     content = self.request.GET.get('content')
     feed_items = {}
     if topic or content:
         if not content:
             category_id = str(self.GROUPS[topic]['id'])
             api_url = ('{api_url}/posts?_embed'
                        '&group={category_id}&{resource_filter}')
             api_url = api_url.format(
                 api_url=self.API_URL,
                 category_id=category_id,
                 resource_filter=self.RESOURCE_FILTER,
             )
             feed_items[topic] = {}
             feed_items[topic]['items'] = get_json_feed_content(api_url)
             feed_items[topic]['group_name'] = self.GROUPS[topic]['name']
         elif not topic:
             content_id = str(self.CATEGORIES[content]['id'])
             api_url = ('{api_url}/posts?_embed&categories={content_id}')
             api_url = api_url.format(
                 api_url=self.API_URL,
                 content_id=content_id,
             )
             feed_items[content] = {}
             feed_items[content]['items'] = get_json_feed_content(api_url)
             feed_items[content]['group_name'] = 'All {name}'.format(
                 name=self.CATEGORIES[content]['name'])
         else:
             category_id = str(self.GROUPS[topic]['id'])
             content_id = str(self.CATEGORIES[content]['id'])
             api_url = ('{api_url}/posts?_embed'
                        '&group={category_id}&categories={content_id}')
             api_url = api_url.format(
                 api_url=self.API_URL,
                 category_id=category_id,
                 content_id=content_id,
             )
             feed_items[topic] = {}
             feed_items[topic]['items'] = get_json_feed_content(api_url)
             feed_items[topic]['group_name'] = '{topic} {content}'.format(
                 topic=self.GROUPS[topic]['name'],
                 content=self.CATEGORIES[content]['name'],
             )
     else:
         for group_name, group in self.GROUPS.items():
             api_url = ('{api_url}/posts'
                        '?group={group_id}&{resource_filter}').format(
                            api_url=self.API_URL,
                            group_id=group['id'],
                            resource_filter=self.RESOURCE_FILTER,
                        )
             feed_items[group_name] = {}
             feed_items[group_name]['items'] = get_json_feed_content(
                 api_url,
                 limit=3,
             )
             feed_items[group_name]['group_name'] = group['name']
     return feed_items
Exemplo n.º 2
0
def tutorial_cards(feed_config, limit=3):
    base_feed_url = "https://tutorials.ubuntu.com/api/tutorials/{type}.json"

    # Check for categories list, otherwise create an empty list to cycle
    if feed_config is not True and "categories" in feed_config:
        categories = feed_config["categories"]
    else:
        categories = [""]

    # Cycle through requested categories and aggregate results
    feed_data = []
    for category in categories:
        if not category:
            feed_type = "recent"
        else:
            feed_type = "-".join(["recent", category])
        feed_url = base_feed_url.format(type=feed_type)

        category_feed = get_json_feed_content(feed_url, limit=limit)
        if category_feed:
            feed_data += category_feed
        else:
            return {"feed": False}

    # Sort tutorials by published, descending. Grab limit from aggregated items
    feed_data = sorted(feed_data, key=itemgetter("published"), reverse=True)
    feed_data = feed_data[:limit]

    for item in feed_data:
        item["published_datetime"] = dateutil.parser.parse(item["published"])

    return {"feed": feed_data}
Exemplo n.º 3
0
def tutorial_cards(feed_config, limit=3):
    base_feed_url = ("https://tutorials.ubuntu.com/api/tutorials/{type}.json")

    # Check for categories list, otherwise create an empty list to cycle
    if feed_config is not True and 'categories' in feed_config:
        categories = feed_config['categories']
    else:
        categories = ['']

    # Cycle through requested categories and aggregate results
    feed_data = []
    for category in categories:
        if not category:
            feed_type = 'recent'
        else:
            feed_type = '-'.join(['recent', category])
        feed_url = base_feed_url.format(type=feed_type)

        category_feed = get_json_feed_content(feed_url, limit=limit)
        if category_feed:
            feed_data += category_feed
        else:
            return {'feed': False}

    # Sort tutorials by published, descending. Grab limit from aggregated items
    feed_data = sorted(feed_data, key=itemgetter('published'), reverse=True)
    feed_data = feed_data[:limit]

    for item in feed_data:
        item['published_datetime'] = dateutil.parser.parse(item['published'])

    return {
        'feed': feed_data,
    }
Exemplo n.º 4
0
 def _get_categories_by_slug(self, slugs=[]):
     if slugs:
         if isinstance(slugs, list):
             slugs = ','.join(slugs)
     api_url = '{api_url}/posts?category={slug}'.format(
         api_url=self.API_URL,
         slug=slugs,
     )
     response = get_json_feed_content(api_url)
     return response
Exemplo n.º 5
0
    def _get_resources(self):
        topic = self.request.GET.get('topic')
        content = self.request.GET.get('content')
        page = int(self.request.GET.get('page', 1))
        offset = (page - 1) * self.PER_PAGE
        search = self.request.GET.get('q')
        feed_items = {}
        posts_length = 0
        if search:
            api_url = (
                '{api_url}/posts?_embed'
                '&search={search}&{resource_filter}'
            ).format(
                api_url=self.API_URL,
                search=search,
                resource_filter=self.RESOURCE_FILTER,
            )
            group_name = 'search'
            feed_items['posts'] = {}
            feed_items['posts'][group_name] = {}
            feed_items['posts'][group_name]['posts'] = get_json_feed_content(
                api_url,
            )
            group_title = 'Search results for "{search}"'.format(
                search=search,
            )
            feed_items['posts'][group_name]['group_name'] = group_title
            return feed_items
        if topic or content:
            if not content:
                category_id = str(self.GROUPS[topic]['id'])
                api_url = (
                    '{api_url}/posts?_embed'
                    '&group={category_id}'
                    '&{resource_filter}'
                    '&per_page={per_page}'
                    '&offset={offset}'
                ).format(
                    api_url=self.API_URL,
                    category_id=category_id,
                    resource_filter=self.RESOURCE_FILTER,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError('Failed to get feed: ' + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                name = self.GROUPS[topic]['name']
                slug = topic
                feed_items['topic'] = {'name': name, 'slug': slug}
                feed_items['posts'] = {}
                feed_items['posts'][topic] = {}
                feed_items['posts'][topic]['posts'] = posts[:self.PER_PAGE]
                feed_items['posts'][topic]['group_name'] = name
            elif not topic:
                content_id = str(self.CATEGORIES[content]['id'])
                api_url = (
                    '{api_url}/posts?_embed'
                    '&categories={content_id}'
                    '&per_page={per_page}'
                    '&offset={offset}'
                ).format(
                    api_url=self.API_URL,
                    content_id=content_id,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError('Failed to get feed: ' + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = 'All {name}'.format(
                    name=self.CATEGORIES[content]['name']
                )
                feed_items['posts'] = {}
                feed_items['posts'][content] = {}
                feed_items['posts'][content]['posts'] = posts[:self.PER_PAGE]
                feed_items['posts'][content]['group_name'] = title
            else:
                category_id = str(self.GROUPS[topic]['id'])
                content_id = str(self.CATEGORIES[content]['id'])
                api_url = (
                    '{api_url}/posts?_embed'
                    '&group={category_id}'
                    '&categories={content_id}'
                    '&per_page={per_page}'
                    '&offset={offset}'
                ).format(
                    api_url=self.API_URL,
                    category_id=category_id,
                    content_id=content_id,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError('Failed to get feed: ' + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = '{topic} {content}'.format(
                    topic=self.GROUPS[topic]['name'],
                    content=self.CATEGORIES[content]['name'],
                )
                name = self.GROUPS[topic]['name']
                slug = topic
                feed_items['topic'] = {'name': name, 'slug': slug}
                feed_items['posts'] = {}
                feed_items['posts'][topic] = {}
                feed_items['posts'][topic]['posts'] = posts[:self.PER_PAGE]
                feed_items['posts'][topic]['group_name'] = title
        else:
            api_url = (
                '{api_url}/posts?_embed'
                '&{resource_filter}'
                '&per_page={per_page}'
                '&offset={offset}'
            ).format(
                api_url=self.API_URL,
                resource_filter=self.RESOURCE_FILTER,
                per_page=self.PER_PAGE + 1,
                offset=offset,
            )
            feed_content = get_json_feed_content(api_url)

            if feed_content is False:
                raise IOError('Failed to get feed: ' + api_url)

            posts = self._normalise_resources(feed_content)

            if posts:
                posts_length = len(posts)
                if 'posts' not in feed_items.keys():
                    feed_items['posts'] = OrderedDict()
                topic = 'All topics'
                feed_items['posts'][topic] = {}
                feed_items['posts'][topic]['posts'] = posts[:self.PER_PAGE]
                feed_items['posts'][topic]['group_name'] = topic
        feed_items['posts_length'] = posts_length
        feed_items['pagination'] = self._generate_pagination_queries(
            page,
            posts_length
        )
        return feed_items
Exemplo n.º 6
0
    def _get_resources(self):
        topic = self.request.GET.get("topic")
        content = self.request.GET.get("content")
        page = int(self.request.GET.get("page", 1))
        offset = (page - 1) * self.PER_PAGE
        search = self.request.GET.get("q")
        feed_items = {}
        posts_length = 0
        if search:
            api_url = ("{api_url}/posts?_embed"
                       "&search={search}&{resource_filter}").format(
                           api_url=self.API_URL,
                           search=search,
                           resource_filter=self.RESOURCE_FILTER,
                       )
            group_name = "search"
            feed_items["posts"] = {}
            feed_items["posts"][group_name] = {}
            feed_items["posts"][group_name]["posts"] = get_json_feed_content(
                api_url)
            group_title = 'Search results for "{search}"'.format(search=search)
            feed_items["posts"][group_name]["group_name"] = group_title
            return feed_items
        if topic or content:
            if not content:
                category_id = str(self.GROUPS[topic]["id"])
                api_url = ("{api_url}/posts?_embed"
                           "&group={category_id}"
                           "&{resource_filter}"
                           "&per_page={per_page}"
                           "&offset={offset}").format(
                               api_url=self.API_URL,
                               category_id=category_id,
                               resource_filter=self.RESOURCE_FILTER,
                               per_page=self.PER_PAGE + 1,
                               offset=offset,
                           )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                name = self.GROUPS[topic]["name"]
                slug = topic
                feed_items["topic"] = {"name": name, "slug": slug}
                feed_items["posts"] = {}
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[:self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = name
            elif not topic:
                content_id = str(self.CATEGORIES[content]["id"])
                api_url = ("{api_url}/posts?_embed"
                           "&categories={content_id}"
                           "&per_page={per_page}"
                           "&offset={offset}").format(
                               api_url=self.API_URL,
                               content_id=content_id,
                               per_page=self.PER_PAGE + 1,
                               offset=offset,
                           )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = "All {name}".format(
                    name=self.CATEGORIES[content]["name"])
                feed_items["posts"] = {}
                feed_items["posts"][content] = {}
                feed_items["posts"][content]["posts"] = posts[:self.PER_PAGE]
                feed_items["posts"][content]["group_name"] = title
            else:
                category_id = str(self.GROUPS[topic]["id"])
                content_id = str(self.CATEGORIES[content]["id"])
                api_url = ("{api_url}/posts?_embed"
                           "&group={category_id}"
                           "&categories={content_id}"
                           "&per_page={per_page}"
                           "&offset={offset}").format(
                               api_url=self.API_URL,
                               category_id=category_id,
                               content_id=content_id,
                               per_page=self.PER_PAGE + 1,
                               offset=offset,
                           )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = "{topic} {content}".format(
                    topic=self.GROUPS[topic]["name"],
                    content=self.CATEGORIES[content]["name"],
                )
                name = self.GROUPS[topic]["name"]
                slug = topic
                feed_items["topic"] = {"name": name, "slug": slug}
                feed_items["posts"] = {}
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[:self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = title
        else:
            api_url = ("{api_url}/posts?_embed"
                       "&{resource_filter}"
                       "&per_page={per_page}"
                       "&offset={offset}").format(
                           api_url=self.API_URL,
                           resource_filter=self.RESOURCE_FILTER,
                           per_page=self.PER_PAGE + 1,
                           offset=offset,
                       )
            feed_content = get_json_feed_content(api_url)

            if feed_content is False:
                raise IOError("Failed to get feed: " + api_url)

            posts = self._normalise_resources(feed_content)

            if posts:
                posts_length = len(posts)
                if "posts" not in feed_items.keys():
                    feed_items["posts"] = OrderedDict()
                topic = "All topics"
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[:self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = topic
        feed_items["posts_length"] = posts_length
        feed_items["pagination"] = self._generate_pagination_queries(
            page, posts_length)
        return feed_items
Exemplo n.º 7
0
    def _get_resources(self):
        topic = self.request.GET.get("topic")
        content = self.request.GET.get("content")
        page = int(self.request.GET.get("page", 1))
        offset = (page - 1) * self.PER_PAGE
        search = self.request.GET.get("q")
        feed_items = {}
        posts_length = 0
        if search:
            api_url = (
                "{api_url}/posts?_embed" "&search={search}&{resource_filter}"
            ).format(
                api_url=self.API_URL,
                search=search,
                resource_filter=self.RESOURCE_FILTER,
            )
            group_name = "search"
            feed_items["posts"] = {}
            feed_items["posts"][group_name] = {}
            feed_items["posts"][group_name]["posts"] = get_json_feed_content(
                api_url
            )
            group_title = 'Search results for "{search}"'.format(search=search)
            feed_items["posts"][group_name]["group_name"] = group_title
            return feed_items
        if topic or content:
            if not content:
                category_id = str(self.GROUPS[topic]["id"])
                api_url = (
                    "{api_url}/posts?_embed"
                    "&group={category_id}"
                    "&{resource_filter}"
                    "&per_page={per_page}"
                    "&offset={offset}"
                ).format(
                    api_url=self.API_URL,
                    category_id=category_id,
                    resource_filter=self.RESOURCE_FILTER,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                name = self.GROUPS[topic]["name"]
                slug = topic
                feed_items["topic"] = {"name": name, "slug": slug}
                feed_items["posts"] = {}
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[: self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = name
            elif not topic:
                content_id = str(self.CATEGORIES[content]["id"])
                api_url = (
                    "{api_url}/posts?_embed"
                    "&categories={content_id}"
                    "&per_page={per_page}"
                    "&offset={offset}"
                ).format(
                    api_url=self.API_URL,
                    content_id=content_id,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = "All {name}".format(
                    name=self.CATEGORIES[content]["name"]
                )
                feed_items["posts"] = {}
                feed_items["posts"][content] = {}
                feed_items["posts"][content]["posts"] = posts[: self.PER_PAGE]
                feed_items["posts"][content]["group_name"] = title
            else:
                category_id = str(self.GROUPS[topic]["id"])
                content_id = str(self.CATEGORIES[content]["id"])
                api_url = (
                    "{api_url}/posts?_embed"
                    "&group={category_id}"
                    "&categories={content_id}"
                    "&per_page={per_page}"
                    "&offset={offset}"
                ).format(
                    api_url=self.API_URL,
                    category_id=category_id,
                    content_id=content_id,
                    per_page=self.PER_PAGE + 1,
                    offset=offset,
                )
                feed_content = get_json_feed_content(api_url)

                if feed_content is False:
                    raise IOError("Failed to get feed: " + api_url)

                posts = self._normalise_resources(feed_content)
                posts_length = len(posts)
                title = "{topic} {content}".format(
                    topic=self.GROUPS[topic]["name"],
                    content=self.CATEGORIES[content]["name"],
                )
                name = self.GROUPS[topic]["name"]
                slug = topic
                feed_items["topic"] = {"name": name, "slug": slug}
                feed_items["posts"] = {}
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[: self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = title
        else:
            api_url = (
                "{api_url}/posts?_embed"
                "&{resource_filter}"
                "&per_page={per_page}"
                "&offset={offset}"
            ).format(
                api_url=self.API_URL,
                resource_filter=self.RESOURCE_FILTER,
                per_page=self.PER_PAGE + 1,
                offset=offset,
            )
            feed_content = get_json_feed_content(api_url)

            if feed_content is False:
                raise IOError("Failed to get feed: " + api_url)

            posts = self._normalise_resources(feed_content)

            if posts:
                posts_length = len(posts)
                if "posts" not in feed_items.keys():
                    feed_items["posts"] = OrderedDict()
                topic = "All topics"
                feed_items["posts"][topic] = {}
                feed_items["posts"][topic]["posts"] = posts[: self.PER_PAGE]
                feed_items["posts"][topic]["group_name"] = topic
        feed_items["posts_length"] = posts_length
        feed_items["pagination"] = self._generate_pagination_queries(
            page, posts_length
        )
        return feed_items