Exemple #1
0
def test_dict_filter():
    def filter_(key, val):
        return key == val

    dict_ = {'foo': 'foo', 'bar': 'baz', (): ()}

    assert dict_filter(dict_, filter_) == {'foo': 'foo', (): ()}
Exemple #2
0
def test_dict_filter_too_many_args():
    def filter_(arg, argg, arggg):
        return True

    with pytest.raises(TypeError):
        dict_filter({'': ''}, filter_)
Exemple #3
0
    def update_media_list_entry(  # pylint: disable=R0913
            self,
            id_: int,
            status: Optional[MediaListStatus] = None,
            score: Optional[float] = None,
            progress: Optional[int] = None,
            custom_lists: Optional[Dict[str, bool]] = None,
            private: Optional[bool] = None,
            started_at: Optional[date] = None,
            completed_at: Optional[date] = None,
            repeat: Optional[int] = None) -> Optional[dict]:
        """
        Update a media list entry for the user.

        Args:
            id_: The media ID
            status: The watching status
            score: The score of the entry
            progress: The amount of episodes consumed by the user
            custom_lists: Map of booleans for which lists the entry are in
            private: If the entry should only be visible to authenticated user
            started_at: When the entry was started by the user
            completed_at: When the entry was completed by the user
            repeat: The amount of times the user has re-watched the media

        Returns:
            The updated media entry values.
        """
        query = """\
mutation (
    $mediaId: Int,
    $status: MediaListStatus,
    $score: Float,
    $progress: Int,
    $customLists: Json,
    $private: Boolean,
    $startedAt: FuzzyDate,
    $completedAt: FuzzyDate,
    $repeat: Int
) {
    SaveMediaListEntry (
        mediaId: $mediaId,
        status: $status,
        score: $score,
        progress: $progress,
        customLists: $customLists,
        private: $private,
        startedAt: $startedAt,
        completedAt: $completedAt,
        repeat: $repeat
    ) {
        id
        status
        score
        progress
        customLists
        private
        startedAt
        completedAt
        repeat
    }
}"""
        variables = dict_filter({
            "mediaId": id_,
            "status": status.name if status else None,
            "score": score,
            "progress": progress,
            "customLists": custom_lists,
            "private": private,
            "startedAt": {
                "year": started_at.year,
                "month": started_at.month,
                "day": started_at.day
            } if started_at else None,
            "completedAt": {
                "year": completed_at.year,
                "month": completed_at.month,
                "day": completed_at.day
            } if completed_at else None,
            "repeat": repeat,
        })
        return self.query(query, variables)
Exemple #4
0
def test_dict_filter_none():
    dict_ = {'foo': None, None: 'bar', '': [], (): {}}
    assert dict_filter(dict_) == {'': [], (): {}}
Exemple #5
0
    def browse_anime(  # pylint: disable=R0914
            self,
            page=1,
            *,
            is_adult: bool = None,
            search: str = None,
            format_: MediaFormat = None,
            status: MediaStatus = None,
            season: MediaSeason = None,
            year_range: Tuple[int, int] = None,
            on_list: bool = None,
            licensed_by: List[str] = None,
            included_genres: List[str] = None,
            excluded_genres: List[str] = None,
            included_tags: List[str] = None,
            excluded_tags: List[str] = None,
            sort: List[MediaSort] = None) -> Tuple[List[dict], bool]:
        """
        Browse anime by the given filters.

        Args:
            page: which page of the anime to return
            is_adult: Filter by if the media's intended for 18+ adult audiences
            search: Filter by search query
            format_: Filter by the media's format
            status: Filter by the media's current release status
            season: The season the media was initially released in
            year_range: The year range the media is ranked within
            on_list: Filter by the media on the authenticated user's lists
            licensed_by: Filter media by sites with a online streaming license
            included_genres: Filter by the media's genres
            excluded_genres: Filter by the media's genres
            included_tags: Filter by the media's tags
            excluded_tags: Filter by the media's tags
            sort: The order the results will be returned in

        Returns:
            The page anime returned and if there's a next page
        """
        query = """\
query (
$page: Int = 1,
$isAdult: Boolean = false,
$search: String,
$format: MediaFormat
$status: MediaStatus,
$season: MediaSeason,
$year: String,
$onList: Boolean,
$yearLesser: FuzzyDateInt,
$yearGreater: FuzzyDateInt,
$licensedBy: [String],
$includedGenres: [String],
$excludedGenres: [String],
$includedTags: [String],
$excludedTags: [String],
$sort: [MediaSort] = [SCORE_DESC, POPULARITY_DESC],
$perPage: Int,
) {
    Page (page: $page, perPage: $perPage) {
        pageInfo {
            total
            perPage
            currentPage
            lastPage
            hasNextPage
        }
        media (
            type: ANIME,
            season: $season,
            format: $format,
            status: $status,
            search: $search,
            onList: $onList,
            startDate_like: $year,
            startDate_lesser: $yearLesser,
            startDate_greater: $yearGreater,
            licensedBy_in: $licensedBy,
            genre_in: $includedGenres,
            genre_not_in: $excludedGenres,
            tag_in: $includedTags,
            tag_not_in: $excludedTags,
            sort: $sort,
            isAdult: $isAdult
        ) {
            id
            title {
                userPreferred
            }
            coverImage {
                large
            }
            bannerImage
            startDate {
                year
                month
                day
            }
            endDate {
                year
                month
                day
            }
            season
            description
            type
            format
            status
            genres
            isAdult
            averageScore
            popularity
            mediaListEntry {
                status
            }
            nextAiringEpisode {
                airingAt
                timeUntilAiring
                episode
            }
            studios (isMain: true) {
                edges {
                    isMain
                    node {
                        id
                        name
                    }
                }
            }
        }
    }
}"""
        variables = dict_filter({
            'page': page,
            'isAdult': is_adult,
            'search': search,
            'format': format_.name if format_ else None,
            'status': status.name if status else None,
            'season': season.name if season else None,
            'onList': on_list,
            'licensedBy': licensed_by,
            'includedGenres': included_genres,
            'excludedGenres': excluded_genres,
            'includedTags': included_tags,
            'excludedTags': excluded_tags,
        })
        if sort:
            variables['sort'] = [s.name for s in sort]
        if year_range and year_range[0] and year_range[1]:
            start, fin = year_range
            if start == fin:
                variables['year'] = f'{start}%'
            else:
                variables['yearGreater'] = start * 10000
                variables['yearLesser'] = fin * 10000
        res = self.query(query, variables).get('data', {}).get('Page', {})
        has_next = res.get('pageInfo', {}).get('hasNextPage', False)
        return res.get('media', []), has_next