forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): daily.dev sources (DIYgod#17464)
- Loading branch information
Showing
15 changed files
with
281 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import { Route } from '@/types'; | ||
import { baseUrl, getBuildId, getData, getList } from './utils'; | ||
import ofetch from '@/utils/ofetch'; | ||
import cache from '@/utils/cache'; | ||
import { config } from '@/config'; | ||
|
||
interface Source { | ||
id: string; | ||
name: string; | ||
handle: string; | ||
image: string; | ||
permalink: string; | ||
description: string; | ||
type: string; | ||
} | ||
|
||
const sourceFeedQuery = ` | ||
query SourceFeed($source: ID!, $loggedIn: Boolean! = false, $first: Int, $after: String, $ranking: Ranking, $supportedTypes: [String!]) { | ||
page: sourceFeed( | ||
source: $source | ||
first: $first | ||
after: $after | ||
ranking: $ranking | ||
supportedTypes: $supportedTypes | ||
) { | ||
...FeedPostConnection | ||
} | ||
} | ||
fragment FeedPostConnection on PostConnection { | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
edges { | ||
node { | ||
...FeedPost | ||
pinnedAt | ||
contentHtml | ||
...UserPost @include(if: $loggedIn) | ||
} | ||
} | ||
} | ||
fragment FeedPost on Post { | ||
...FeedPostInfo | ||
sharedPost { | ||
id | ||
title | ||
image | ||
readTime | ||
permalink | ||
commentsPermalink | ||
createdAt | ||
type | ||
tags | ||
source { | ||
id | ||
handle | ||
permalink | ||
image | ||
} | ||
slug | ||
} | ||
trending | ||
feedMeta | ||
collectionSources { | ||
handle | ||
image | ||
} | ||
numCollectionSources | ||
updatedAt | ||
slug | ||
} | ||
fragment FeedPostInfo on Post { | ||
id | ||
title | ||
image | ||
readTime | ||
permalink | ||
commentsPermalink | ||
createdAt | ||
commented | ||
bookmarked | ||
views | ||
numUpvotes | ||
numComments | ||
summary | ||
bookmark { | ||
remindAt | ||
} | ||
author { | ||
id | ||
name | ||
image | ||
username | ||
permalink | ||
} | ||
type | ||
tags | ||
source { | ||
id | ||
handle | ||
name | ||
permalink | ||
image | ||
type | ||
} | ||
userState { | ||
vote | ||
flags { | ||
feedbackDismiss | ||
} | ||
} | ||
slug | ||
} | ||
fragment UserPost on Post { | ||
read | ||
upvoted | ||
commented | ||
bookmarked | ||
downvoted | ||
}`; | ||
|
||
export const route: Route = { | ||
path: '/source/:sourceId', | ||
example: '/daily/source/hn', | ||
parameters: { | ||
sourceId: 'The source id', | ||
}, | ||
radar: [ | ||
{ | ||
source: ['app.daily.dev/sources/:sourceId'], | ||
}, | ||
], | ||
name: 'Source Posts', | ||
maintainers: ['TonyRL'], | ||
handler, | ||
url: 'app.daily.dev', | ||
}; | ||
|
||
async function handler(ctx) { | ||
const sourceId = ctx.req.param('sourceId'); | ||
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10; | ||
const link = `${baseUrl}/sources/${sourceId}`; | ||
|
||
const buildId = await getBuildId(); | ||
|
||
const userData = (await cache.tryGet(`daily:source:${sourceId}`, async () => { | ||
const response = await ofetch(`${baseUrl}/_next/data/${buildId}/en/sources/${sourceId}.json`); | ||
return response.pageProps.source; | ||
})) as Source; | ||
|
||
const items = await cache.tryGet( | ||
`daily:source:${sourceId}:posts`, | ||
async () => { | ||
const edges = await getData({ | ||
query: sourceFeedQuery, | ||
variables: { | ||
source: sourceId, | ||
supportedTypes: ['article', 'video:youtube', 'collection'], | ||
period: 30, | ||
first: limit, | ||
after: '', | ||
loggedIn: false, | ||
}, | ||
}); | ||
return getList(edges); | ||
}, | ||
config.cache.routeExpire, | ||
false | ||
); | ||
|
||
return { | ||
title: `${userData.name} posts on daily.dev`, | ||
description: userData.description, | ||
link, | ||
item: items, | ||
image: userData.image, | ||
logo: userData.image, | ||
icon: userData.image, | ||
language: 'en-us', | ||
}; | ||
} |
Oops, something went wrong.