Skip to content

Commit

Permalink
change way last youtube fetch is set
Browse files Browse the repository at this point in the history
  • Loading branch information
travv0 committed Apr 5, 2024
1 parent d5d9776 commit ea3aaf4
Showing 1 changed file with 86 additions and 64 deletions.
150 changes: 86 additions & 64 deletions Youtube.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,9 @@ let getYoutubeUpdates () : string option list =
MaxResults = 50L,
PublishedAfter =
(Map.tryFind channelId (getDb ()).LastYoutubeFetch
|> Option.defaultValue (DateTime.Now.AddHours(-1)))
|> Option.defaultValue (DateTime.UtcNow.AddHours(-1)))
)

updateDb
{ (getDb ()) with
LastYoutubeFetch =
Map.add channelId DateTime.Now (getDb ()).LastYoutubeFetch }

let response = listRequest.ExecuteAsync().Result

let urlByResourceIdKind (resourceId: ResourceId) =
Expand All @@ -118,64 +113,91 @@ let getYoutubeUpdates () : string option list =
| Some title -> title
| None -> channelId

for item in response.Items do
match item.Snippet.Type with
| "channelItem" ->
yield
Some(
$"**{channelTitle}** has a new channel item, whatever that is: "
+ urlByResourceIdKind
item.ContentDetails.ChannelItem.ResourceId
)
| "favorite" ->
yield
Some(
"$**{channelTitle}** has a new favorite! I wonder what kind of cool thing it could be? "
+ urlByResourceIdKind
item.ContentDetails.Favorite.ResourceId
)
| "like" ->
yield
Some(
$"**{channelTitle}** liked something. I bet it's not weird at all: "
+ urlByResourceIdKind
item.ContentDetails.Like.ResourceId
)
| "playlistItem" ->
yield
Some(
$"**{channelTitle}** added a new video to their playlist: "
+ urlByResourceIdKind
item.ContentDetails.PlaylistItem.ResourceId
)
| "recommendation" ->
yield
Some(
$"**{channelTitle}** has a new recommendation for you: "
+ urlByResourceIdKind
item.ContentDetails.Recommendation.ResourceId
)
| "social" ->
yield
Some(
$"**{channelTitle}** has a new social thing, that social butterfly: "
+ urlByResourceIdKind
item.ContentDetails.Social.ResourceId
)
| "subscription" ->
yield
Some(
$"**{channelTitle}** has subscribed to a user. More quality content in their future! "
+ urlByResourceIdKind
item.ContentDetails.Subscription.ResourceId
)
| "upload" ->
yield
Some(
$"**{channelTitle}** has uploaded a new video: "
+ $"https://www.youtube.com/watch?v=%s{item.ContentDetails.Upload.VideoId}"
)
| _ -> yield None ]
let result =
[ for item in response.Items do
match item.Snippet.Type with
| "channelItem" ->
yield
Some(
$"**{channelTitle}** has a new channel item, whatever that is: "
+ urlByResourceIdKind
item.ContentDetails.ChannelItem.ResourceId
)
| "favorite" ->
yield
Some(
"$**{channelTitle}** has a new favorite! I wonder what kind of cool thing it could be? "
+ urlByResourceIdKind
item.ContentDetails.Favorite.ResourceId
)
| "like" ->
yield
Some(
$"**{channelTitle}** liked something. I bet it's not weird at all: "
+ urlByResourceIdKind
item.ContentDetails.Like.ResourceId
)
| "playlistItem" ->
yield
Some(
$"**{channelTitle}** added a new video to their playlist: "
+ urlByResourceIdKind
item.ContentDetails.PlaylistItem.ResourceId
)
| "recommendation" ->
yield
Some(
$"**{channelTitle}** has a new recommendation for you: "
+ urlByResourceIdKind
item.ContentDetails.Recommendation.ResourceId
)
| "social" ->
yield
Some(
$"**{channelTitle}** has a new social thing, that social butterfly: "
+ urlByResourceIdKind
item.ContentDetails.Social.ResourceId
)
| "subscription" ->
yield
Some(
$"**{channelTitle}** has subscribed to a user. More quality content in their future! "
+ urlByResourceIdKind
item.ContentDetails.Subscription.ResourceId
)
| "upload" ->
yield
Some(
$"**{channelTitle}** has uploaded a new video: "
+ $"https://www.youtube.com/watch?v=%s{item.ContentDetails.Upload.VideoId}"
)
| _ -> yield None ]

let latestItem =
if response.Items.Count = 0 then
None
else
response.Items
|> Seq.maxBy (fun i ->
match DateTime.TryParse(i.Snippet.PublishedAtRaw) with
| true, dt -> dt
| _ -> DateTime.MinValue)
|> Option.ofObj

match latestItem with
| Some item ->
updateDb
{ (getDb ()) with
LastYoutubeFetch =
Map.add
channelId
(DateTime
.Parse(item.Snippet.PublishedAtRaw)
.AddMilliseconds(1))
(getDb ()).LastYoutubeFetch }
| None -> ()

yield! result ]

type ContentText =
{ text: string }
Expand Down

0 comments on commit ea3aaf4

Please sign in to comment.