Skip to content

Instantly share code, notes, and snippets.

@gandroz
Last active November 23, 2021 00:46
Show Gist options
  • Save gandroz/c32b967b23bc497ed2df42d337bd413c to your computer and use it in GitHub Desktop.
Save gandroz/c32b967b23bc497ed2df42d337bd413c to your computer and use it in GitHub Desktop.
graphql client query
from python_graphql_client import GraphqlClient
client = GraphqlClient(endpoint="https://medium.com/_/graphql")
query = "query TopicHandler($topicSlug: ID!, $feedPagingOptions: PagingOptions, $sidebarPagingOptions: PagingOptions) {\n topic(slug: $topicSlug) {\n canonicalSlug\n ...TopicScreen_topic\n __typename\n }\n}\n\nfragment PostListingItemFeed_postPreview on PostPreview {\n post {\n ...PostListingItemPreview_post\n ...PostListingItemByline_post\n ...PostListingItemImage_post\n ...PostPresentationTracker_post\n __typename\n }\n __typename\n}\n\nfragment PostListingItemPreview_post on Post {\n id\n mediumUrl\n title\n previewContent {\n subtitle\n isFullContent\n __typename\n }\n isPublished\n creator {\n id\n __typename\n }\n __typename\n}\n\nfragment PostListingItemByline_post on Post {\n id\n creator {\n id\n username\n name\n __typename\n }\n isLocked\n readingTime\n ...BookmarkButton_post\n firstPublishedAt\n updatedAt\n statusForCollection\n collection {\n id\n name\n ...collectionUrl_collection\n __typename\n }\n __typename\n}\n\nfragment BookmarkButton_post on Post {\n ...SusiClickable_post\n ...WithSetReadingList_post\n __typename\n}\n\nfragment SusiClickable_post on Post {\n id\n mediumUrl\n ...SusiContainer_post\n __typename\n}\n\nfragment SusiContainer_post on Post {\n id\n __typename\n}\n\nfragment WithSetReadingList_post on Post {\n ...ReadingList_post\n __typename\n}\n\nfragment ReadingList_post on Post {\n __typename\n id\n readingList\n}\n\nfragment collectionUrl_collection on Collection {\n id\n domain\n slug\n __typename\n}\n\nfragment PostListingItemImage_post on Post {\n id\n mediumUrl\n previewImage {\n id\n focusPercentX\n focusPercentY\n __typename\n }\n __typename\n}\n\nfragment PostPresentationTracker_post on Post {\n id\n visibility\n previewContent {\n isFullContent\n __typename\n }\n collection {\n id\n __typename\n }\n __typename\n}\n\nfragment TopicScreen_topic on Topic {\n id\n ...TopicMetadata_topic\n ...TopicLandingHeader_topic\n ...TopicFeaturedAndLatest_topic\n ...TopicLandingRelatedTopics_topic\n ...TopicLandingPopular_posts\n __typename\n}\n\nfragment TopicMetadata_topic on Topic {\n name\n description\n slug\n image {\n id\n __typename\n }\n __typename\n}\n\nfragment TopicLandingHeader_topic on Topic {\n name\n description\n visibility\n ...TopicFollowButtonSignedIn_topic\n ...TopicFollowButtonSignedOut_topic\n __typename\n}\n\nfragment TopicFollowButtonSignedIn_topic on Topic {\n __typename\n slug\n isFollowing\n}\n\nfragment TopicFollowButtonSignedOut_topic on Topic {\n id\n slug\n ...SusiClickable_topic\n __typename\n}\n\nfragment SusiClickable_topic on Topic {\n ...SusiContainer_topic\n __typename\n}\n\nfragment SusiContainer_topic on Topic {\n ...SignInOptions_topic\n ...SignUpOptions_topic\n __typename\n}\n\nfragment SignInOptions_topic on Topic {\n id\n name\n __typename\n}\n\nfragment SignUpOptions_topic on Topic {\n id\n name\n __typename\n}\n\nfragment TopicFeaturedAndLatest_topic on Topic {\n name\n slug\n featuredPosts {\n postPreviews {\n post {\n id\n ...TopicLandingFeaturedStory_post\n __typename\n }\n __typename\n }\n __typename\n }\n featuredTopicWriters(limit: 1) {\n ...FeaturedWriter_featuredTopicWriter\n __typename\n }\n latestPosts(paging: $feedPagingOptions) {\n postPreviews {\n post {\n id\n __typename\n }\n ...PostListingItemFeed_postPreview\n __typename\n }\n pagingInfo {\n next {\n limit\n to\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment TopicLandingFeaturedStory_post on Post {\n ...FeaturedPostPreview_post\n ...PostListingItemPreview_post\n ...PostListingItemBylineWithAvatar_post\n ...PostListingItemImage_post\n ...PostPresentationTracker_post\n __typename\n}\n\nfragment FeaturedPostPreview_post on Post {\n id\n title\n mediumUrl\n previewContent {\n subtitle\n isFullContent\n __typename\n }\n __typename\n}\n\nfragment PostListingItemBylineWithAvatar_post on Post {\n creator {\n username\n name\n id\n imageId\n mediumMemberAt\n __typename\n }\n isLocked\n readingTime\n updatedAt\n statusForCollection\n collection {\n id\n name\n ...collectionUrl_collection\n __typename\n }\n __typename\n}\n\nfragment FeaturedWriter_featuredTopicWriter on FeaturedTopicWriter {\n user {\n id\n username\n name\n bio\n ...UserAvatar_user\n ...UserFollowButton_user\n __typename\n }\n posts {\n postPreviews {\n ...PostListingItemFeaturedWriter_postPreview\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment UserAvatar_user on User {\n username\n id\n name\n imageId\n mediumMemberAt\n __typename\n}\n\nfragment UserFollowButton_user on User {\n ...UserFollowButtonSignedIn_user\n ...UserFollowButtonSignedOut_user\n __typename\n}\n\nfragment UserFollowButtonSignedIn_user on User {\n id\n isFollowing\n __typename\n}\n\nfragment UserFollowButtonSignedOut_user on User {\n id\n ...SusiClickable_user\n __typename\n}\n\nfragment SusiClickable_user on User {\n ...SusiContainer_user\n __typename\n}\n\nfragment SusiContainer_user on User {\n ...SignInOptions_user\n ...SignUpOptions_user\n __typename\n}\n\nfragment SignInOptions_user on User {\n id\n name\n __typename\n}\n\nfragment SignUpOptions_user on User {\n id\n name\n __typename\n}\n\nfragment PostListingItemFeaturedWriter_postPreview on PostPreview {\n postId\n post {\n readingTime\n id\n mediumUrl\n title\n ...PostListingItemImage_post\n ...PostPresentationTracker_post\n __typename\n }\n __typename\n}\n\nfragment TopicLandingRelatedTopics_topic on Topic {\n relatedTopics {\n topic {\n name\n slug\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment TopicLandingPopular_posts on Topic {\n name\n popularPosts(paging: $sidebarPagingOptions) {\n postPreviews {\n post {\n ...PostListingItemSidebar_post\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n}\n\nfragment PostListingItemSidebar_post on Post {\n id\n mediumUrl\n title\n readingTime\n ...PostListingItemImage_post\n ...PostPresentationTracker_post\n __typename\n}\n"
variables = {"topicSlug": "popular", "feedPagingOptions": {"limit": 25, "to": 1593525608991, "sidebarPagingOptions": {"limit": 5}}
data = client.execute(query=query, variables=variables)
for post in data["data"]["topic"]["featuredPosts"]["postPreviews"]:
print(f"{post['post']['updatedAt']} - {post['post']['title']} - {post['post']['mediumUrl']}")
@GiovanniDiMasi
Copy link

doesnt work for me, it says 400 Client Error: Bad Request for url: https://medium.com/_/graphql

@gandroz
Copy link
Author

gandroz commented Nov 23, 2021

That's possible. It used to work at the time the post was written though. URL may have changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment