Esempio n. 1
0
        def _cache_group(group_id, content, gpath=gpath):
            if not user_id:
                if INS.get('put_group_in_session'):
                    INS['groups'][group_id] = content

                INS.get('temp_groups', {}).pop(group_id, None)
                file_dump(gpath, content)
            return content
Esempio n. 2
0
    def getGroupPhotos(self, group_id, page=1, user_id=None, per_page=None):
        Logger().info("getGroupPhotos %s %s %s" % (group_id, page, user_id))
        if not user_id and INS.get('put_group_in_session'):
            group_data = INS['groups'].get(group_id)
            if group_data:
                return group_data

        gpath = os.path.join(OPT.groups_full_content_dir, group_id)
        if not user_id and OPT.group_from_cache:
            if os.path.exists(gpath):
                l_photos = file_load(gpath)
                if l_photos:
                    Logger().debug("%s loaded from cache" % group_id)

                    if INS.get('put_group_in_session'):
                        INS['groups'][group_id] = l_photos

                    return l_photos

        if not per_page:
            if not user_id and INS.get('put_group_in_session') and \
                    not OPT.group_from_cache:
                per_page = DEFAULT_PERPAGE
            else:
                per_page = 500

        rsp_json = self.request('groups.pools.getPhotos',
            "photos from group %s for user %s, page %i",
            [group_id, user_id, page], page=page, per_page=per_page,
            group_id=group_id, user_id=user_id, content_type=7)
        if not rsp_json: return []

        content = rsp_json['photos']['photo']
        g_size = int(rsp_json['photos']['total'])
        total = len(content) + (page - 1) * per_page

        Logger().debug("%s has %d results" % (group_id, g_size))

        def _cache_group(group_id, content, gpath=gpath):
            if not user_id:
                if INS.get('put_group_in_session'):
                    INS['groups'][group_id] = content

                INS.get('temp_groups', {}).pop(group_id, None)
                file_dump(gpath, content)
            return content

        temp_groups_key = "%s%s" % (group_id, user_id or '')
        l_photos = INS.get('temp_groups', {}).get(temp_groups_key, [])

        if not user_id and not l_photos and os.path.exists(gpath):
            Logger().debug("load file %s" % gpath)
            l_photos = file_load(gpath) or []

        if len(l_photos) >= g_size:
            return _cache_group(group_id, l_photos)

        ids = [x['id'] for x in l_photos]
        for i in content:
            if i['id'] not in ids:
                l_photos.append(i)

        # remove duplicates on id
        l_photos = dict([(x['id'], x) for x in l_photos]).values()

        Logger().debug("getGroupPhotos %d %d %d" % (
            len(l_photos), g_size, len(content)))

        if len(l_photos) >= g_size or not content:
            return _cache_group(group_id, l_photos)

        content = l_photos
        total = len(l_photos)

        if total < g_size:
            if g_size - total < 500:
                per_page = DEFAULT_PERPAGE

            INS['temp_groups'][temp_groups_key] = content
            content = self.getGroupPhotos(group_id, page + 1, user_id,
                per_page=per_page)

        return content