mirror of
https://github.com/oceanprotocol/webtasks
synced 2025-01-07 20:35:30 +01:00
add videos querying by channel ID
This commit is contained in:
parent
bac7e7c694
commit
650aae46dc
30
README.md
30
README.md
@ -43,15 +43,37 @@ https://TASK_URL/TASK_NAME/:medium_username
|
|||||||
|
|
||||||
### YouTube
|
### YouTube
|
||||||
|
|
||||||
**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. For now, only fetches a playlist. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io.
|
**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io.
|
||||||
|
|
||||||
Construct your request url like so, e.g. locally:
|
* `/playlist/:playlistId`: returns all videos in the given playlist.
|
||||||
|
* `/channel/:channelId`: returns latest 10 videos in the given channel, sorted by date.
|
||||||
|
|
||||||
|
Construct your `GET` request urls like so:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
http://localhost:8080/:youtube_playlist_id
|
http://localhost:8080/playlist/:youtube_playlist_id
|
||||||
|
http://localhost:8080/channel/:youtube_channel_id
|
||||||
|
|
||||||
# when published on webtask.io
|
# when published on webtask.io
|
||||||
https://TASK_URL/TASK_NAME/:youtube_playlist_id
|
https://TASK_URL/TASK_NAME/playlist/:youtube_playlist_id
|
||||||
|
https://TASK_URL/TASK_NAME/channel/:youtube_channel_id
|
||||||
|
```
|
||||||
|
|
||||||
|
**Response**
|
||||||
|
|
||||||
|
For all endpoints, response is reconstructed in the same format:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[{
|
||||||
|
"id": "8EMowpNlWPE",
|
||||||
|
"title": "Fang Gong, Researcher - Bringing Privacy to Ocean Protocol",
|
||||||
|
"description": "At Ocean Protocol, we unlock data for AI and use TCRs to maintain a registry of high-quality data sets through voting. Privacy is essential to protecting the ...",
|
||||||
|
"imageUrl": "https://i.ytimg.com/vi/8EMowpNlWPE/mqdefault.jpg",
|
||||||
|
"videoUrl": "https://www.youtube.com/watch?v=8EMowpNlWPE"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Bounties
|
### Bounties
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "wt serve webtask-bounties.js",
|
"start": "wt serve webtask-youtube.js",
|
||||||
"test": "eslint ./*.js"
|
"test": "eslint ./*.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -6,27 +6,57 @@ const webtask = require('webtask-tools')
|
|||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
const makeRequest = (options, cb) => {
|
||||||
res.send('Please specify the playlist ID as parameter.')
|
|
||||||
})
|
|
||||||
|
|
||||||
app.get('/:playlist', (req, res) => {
|
|
||||||
const options = {
|
|
||||||
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
|
|
||||||
headers: { 'referer': req.headers.host }
|
|
||||||
}
|
|
||||||
|
|
||||||
request(options, (error, response, body) => {
|
request(options, (error, response, body) => {
|
||||||
const json = JSON.parse(body)
|
const json = JSON.parse(body)
|
||||||
const videos = json.items
|
const videos = json.items
|
||||||
const parsedPosts = []
|
|
||||||
|
|
||||||
let holder = {}
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cb(videos)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.send('Please specify the playlist ID as parameter.')
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/channel/:channelId', (req, res) => {
|
||||||
|
const options = {
|
||||||
|
url: `https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${req.params.channelId}&maxResults=10&order=date&type=video&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
|
||||||
|
headers: { 'referer': req.headers.host }
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedPosts = []
|
||||||
|
let holder = {}
|
||||||
|
|
||||||
|
makeRequest(options, (videos) => {
|
||||||
|
for (let i = 0; i < videos.length; i++) {
|
||||||
|
holder.id = videos[i].id.videoId
|
||||||
|
holder.title = videos[i].snippet.title
|
||||||
|
holder.description = videos[i].snippet.description
|
||||||
|
holder.imageUrl = videos[i].snippet.thumbnails.medium.url
|
||||||
|
holder.videoUrl = `https://www.youtube.com/watch?v=${videos[i].id.videoId}`
|
||||||
|
parsedPosts.push(holder)
|
||||||
|
holder = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(parsedPosts)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/playlist/:playlistId', (req, res) => {
|
||||||
|
const options = {
|
||||||
|
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlistID}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
|
||||||
|
headers: { 'referer': req.headers.host }
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedPosts = []
|
||||||
|
let holder = {}
|
||||||
|
|
||||||
|
makeRequest(options, (videos) => {
|
||||||
for (let i = 0; i < videos.length; i++) {
|
for (let i = 0; i < videos.length; i++) {
|
||||||
holder.id = videos[i].snippet.resourceId.videoId
|
holder.id = videos[i].snippet.resourceId.videoId
|
||||||
holder.title = videos[i].snippet.title
|
holder.title = videos[i].snippet.title
|
||||||
@ -41,7 +71,7 @@ app.get('/:playlist', (req, res) => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/:playlist/raw', (req, res) => {
|
app.get('/playlist/:playlist/raw', (req, res) => {
|
||||||
const options = {
|
const options = {
|
||||||
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${process.env.YOUTUBE_API_KEY}`,
|
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${process.env.YOUTUBE_API_KEY}`,
|
||||||
headers: { 'referer': req.headers.host }
|
headers: { 'referer': req.headers.host }
|
||||||
|
Loading…
Reference in New Issue
Block a user