Skip to content

Instantly share code, notes, and snippets.

@sir-deenicus
Last active August 29, 2015 14:01
Show Gist options
  • Save sir-deenicus/9a61fd067a50c59e62bd to your computer and use it in GitHub Desktop.
Save sir-deenicus/9a61fd067a50c59e62bd to your computer and use it in GitHub Desktop.
open System
open FSharp.Data
type GplusPost = FSharp.Data.JsonProvider<"gplusposttemplate.json">
type GplusSearch = FSharp.Data.JsonProvider<"gplusSearchTemplate.json">
type GplusCommentList = FSharp.Data.JsonProvider<"gplusCommentListTemplate.json">
let googlePlusFunc key url parse (mapItems) (next) takeAtATime maxtake query =
let download token choice =
try
Http.RequestString( "https://www.googleapis.com/plus/v1/"+url,
query = (if token <> "" then ("pageToken",token)::choice else choice))
|> parse
|> Some
with
| :? Net.WebException as ex -> printfn "%A\n%s" ex.Response ex.Message; None //probably some rubbish about api limits
| ex -> printfn "%s" ex.Message; None
let rec seek i token =
seq{
let choice = (("key", key)::(if takeAtATime = "" then []
else ["maxResults", takeAtATime])) @ query
let trydata = download token choice
printfn "page %d" i
if trydata.IsSome then
let data = trydata.Value
let nextpage = next data
if (Option.isNone maxtake || i < maxtake.Value) && (nextpage <> "" || i = 0) then
yield! (mapItems data)
yield! seek (i + 1) nextpage
}
seek 0 ""
let getPosts userid =
googlePlusFunc key ("people/" + userid + "/activities/public")
GplusPost.Parse (fun data -> data.Items |> Array.map (fun a -> a.Title,(a.Published, a.Updated),(a.Object.Value.Content, a.Annotation)))
(fun data -> data.NextPageToken)
let searchGplus =
googlePlusFunc key "activities"
GplusSearch.Parse (fun data -> data.Items |> Array.map (fun a -> a.Id, a.Actor.Value.DisplayName, a.Title, a.Url))
(fun data -> data.NextPageToken)
let getComments cid =
googlePlusFunc key
("activities/" + cid + "/comments")
GplusCommentList.Parse
(fun data -> data.Items |> Array.map (fun c -> c.Actor.Value.DisplayName, string c.Published, c.Plusoners.Value.TotalItems, c.Object.Value.Content))
(fun data -> data.NextPageToken) "" None []
searchGplus "20" None ["query", "Deen+Abiola"]
|> Seq.toArray
|> Array.collect (fst4 >> getComments >> Seq.toArray >> Array.filter (fst4 >> strcontains "Deen Abiola"))
{
"kind": "plus#commentFeed",
"etag": "abc",
"nextPageToken": "abc",
"nextLink": "abc",
"title": "abc",
"updated": "2014-05-10T06:18:48.417Z",
"id": "abc",
"items": [
{
"kind": "plus#comment",
"etag": "abc",
"verb": "post",
"id": "abc",
"published": "2014-05-10T06:18:48.417Z",
"updated": "2014-05-10T06:18:48.417Z",
"actor": {
"id": "abc",
"displayName": "abc",
"url": "abc",
"image": {
"url": "abc"
}
},
"object": {
"objectType": "comment",
"content": "abc",
"originalContent": "abc"
},
"selfLink": "abc",
"inReplyTo": [
{
"id": "abc",
"url": "abc"
}
],
"plusoners": {
"totalItems": 2
}
}
]
}
{
"actor" : {
"displayName" : "abc",
"id" : "abc",
"image" : {
"url" : "abc"
},
"name" : {
"familyName" : "abc",
"givenName" : "abc"
},
"url" : "abc"
},
"etag" : "a",
"id" : "Abc",
"items" : [
{
"access" : {
"description" : "Public",
"items" : [
{
"type" : "public"
}
],
"kind" : "plus#acl"
},
"actor" : {
"displayName" : "abc",
"id" : "abc",
"image" : {
"url" : "abc"
},
"url" : "abc"
},
"annotation" : "abc",
"etag" : "abc",
"id" : "abc",
"kind" : "plus#activity",
"object" : {
"content" : "abc",
"objectType" : "note",
"plusoners" : {
"selfLink" : "abc",
"totalItems" : 3
},
"replies" : {
"selfLink" : "abc",
"totalItems" : 4
},
"resharers" : {
"selfLink" : "abc",
"totalItems" : 0
},
"url" : "abc"
},
"provider" : {
"title" : "Google+"
},
"published" : "2014-05-03T15:47:06.311Z",
"title" : "abc",
"updated" : "2014-05-03T16:45:13.000Z",
"url" : "abc",
"verb" : "post"
}
],
"kind" : "plus#activityFeed",
"nextPageToken" : "abc",
"title" : "abc",
"updated" : "2014-03-09T21:16:36.000Z"
}
{
"etag" : "abc",
"id" : "abc",
"items" : [
{
"access" : {
"description" : "Public",
"items" : [
{
"type" : "public"
}
],
"kind" : "plus#acl"
},
"actor" : {
"displayName" : "abc",
"id" : "abc",
"image" : {
"url" : "abc"
},
"name" : {
"familyName" : "abc",
"givenName" : "abc"
},
"url" : "abc"
},
"annotation" : "abc",
"etag" : "abc",
"id" : "Abc",
"kind" : "plus#activityFeed",
"nextPageToken" : "abc",
"object" : {
"content" : "abc",
"objectType" : "note",
"plusoners" : {
"selfLink" : "abc",
"totalItems" : 3
},
"replies" : {
"selfLink" : "abc",
"totalItems" : 4
},
"resharers" : {
"selfLink" : "abc",
"totalItems" : 0
},
"url" : "abc"
},
"provider" : {
"title" : "Google+"
},
"published" : "2014-05-03T15:47:06.311Z",
"title" : "abcd",
"updated" : "2014-03-09T21:16:36.000Z",
"url" : "abc",
"verb" : "post"
}
],
"kind" : "plus#activityFeed",
"nextLink" : "abc",
"nextPageToken" : "abc",
"selfLink" : "abc",
"title" : "abc",
"updated" : "2014-05-10T06:18:48.417Z"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment