-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathyoutube.fsx
More file actions
70 lines (61 loc) · 2.35 KB
/
youtube.fsx
File metadata and controls
70 lines (61 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#r "packages/Suave/lib/net40/Suave.dll"
#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#load "common.fsx"
module Youtube =
open Common
open System
open System.Net
open Suave.Successful
open FSharp.Data
let [<Literal>] videosSample = """
{ "videos" : [
{ "title":"talk title",
"date":"2011-01-01T17:00:00",
"thumbnail" : "url string",
"description" : "some video about lots of stuff",
"link" : "http://youtube.some.video"
}
]}
"""
type VideoData = JsonProvider<videosSample, RootName="Root">
let [<Literal>] youTubeSample = """
{
"items": [
{ "kind": "some kind",
"id": {"videoId": "vvkkppll", "kind": "youtube#video"},
"snippet":
{ "publishedAt": "2016-02-24T22:39:21.000Z",
"channelId": "UC6OzdI6-htXE_97zamJRaaA",
"title": "Stealing Time with the .NET ThreadPool",
"description": "Scalable applications something",
"thumbnails":
{ "default": { "url": "https://dV_4/default.jpg", "width": 120, "height": 90 },
"medium" : { "url": "https://mqdefault.jpg", "width": 320, "height": 180 },
"high" : { "url": "https://qdefault.jpg", "width": 480, "height": 360 }
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
}]}
"""
type YouTubeData = JsonProvider<youTubeSample>
let getVideos x =
let query () =
let api_token = "YOUTUBE_API_TOKEN" |> Env.tryGetVar |> Option.get
let channel = "UC6OzdI6-htXE_97zamJRaaA"
let url = sprintf "https://www.googleapis.com/youtube/v3/search?key=%s&channelId=%s&part=snippet,id&order=date&maxResults=10" api_token channel
YouTubeData.Load(url)
let createVideo (i : YouTubeData.Item) =
VideoData.Video(
title=i.Snippet.Title,
date=i.Snippet.PublishedAt,
thumbnail=i.Snippet.Thumbnails.Medium.Url,
description=i.Snippet.Description,
link="https://www.youtube.com/watch?v=" + i.Id.VideoId
)
async {
let onlyVideos (v: YouTubeData.Item) = v.Id.Kind = "youtube#video"
let ytResult = query()
let videos = ytResult.Items |> Array.filter onlyVideos |> Array.map createVideo
return! OK (VideoData.Root(videos = videos).JsonValue.ToString()) x
}