0 people like it.

Fetch GitHub download count for a file in a repo

GitHub has a public JsonApi to expose file download count.

 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: 
//#r "FSharp.Data.dll"
open FSharp.Data
open System

[<Literal>]
let projectrepo = "https://api.github.com/repos/fsprojects/paket/releases"

type RepoData = JsonProvider<projectrepo>

let items = RepoData.Load projectrepo

/// Fetch download count per day:
/// [|("2017-07-21", 6170); ("2017-07-20", 1043); ... |]
let fetchDownloadCountFor filename =
    items 
    |> Array.map(fun pkg ->
        pkg.Assets 
        |> Array.filter(fun a -> a.BrowserDownloadUrl.EndsWith("/"+filename))
        |> Array.map(fun a -> a.CreatedAt.ToString("yyyy-MM-dd"), a.DownloadCount)
    ) |> Array.concat
    |> Array.groupBy fst
    |> Array.map(fun (k,v) -> k, (v |> Array.sumBy snd))
    
/// Data start day and total count.
/// ("2017-07-07", 36158)
let all =
    let daily = fetchDownloadCountFor "paket.exe" 
    let from = daily |> Array.minBy fst |> fst
    let total = daily |> Array.sumBy snd
    from, total
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
namespace System
Multiple items
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

Full name: Microsoft.FSharp.Core.LiteralAttribute

--------------------
new : unit -> LiteralAttribute
val projectrepo : string

Full name: Script.projectrepo
type RepoData = JsonProvider<...>

Full name: Script.RepoData
type JsonProvider

Full name: FSharp.Data.JsonProvider


<summary>Typed representation of a JSON document.</summary>
       <param name='Sample'>Location of a JSON sample file or a string containing a sample JSON document.</param>
       <param name='SampleIsList'>If true, sample should be a list of individual samples for the inference.</param>
       <param name='RootName'>The name to be used to the root type. Defaults to `Root`.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.json'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans.)</param>
val items : JsonProvider<...>.Root []

Full name: Script.items
JsonProvider<...>.Load(uri: string) : JsonProvider<...>.Root []


Loads JSON from the specified uri

JsonProvider<...>.Load(reader: IO.TextReader) : JsonProvider<...>.Root []


Loads JSON from the specified reader

JsonProvider<...>.Load(stream: IO.Stream) : JsonProvider<...>.Root []


Loads JSON from the specified stream
val fetchDownloadCountFor : filename:string -> (string * int) []

Full name: Script.fetchDownloadCountFor


 Fetch download count per day:
 [|("2017-07-21", 6170); ("2017-07-20", 1043); ... |]
val filename : string
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val pkg : JsonProvider<...>.Root
property JsonProvider<...>.Root.Assets: JsonProvider<...>.Asset []
val filter : predicate:('T -> bool) -> array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.filter
val a : JsonProvider<...>.Asset
property JsonProvider<...>.Asset.BrowserDownloadUrl: string
String.EndsWith(value: string) : bool
String.EndsWith(value: string, comparisonType: StringComparison) : bool
String.EndsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
property JsonProvider<...>.Asset.CreatedAt: DateTime
DateTime.ToString() : string
DateTime.ToString(provider: IFormatProvider) : string
DateTime.ToString(format: string) : string
DateTime.ToString(format: string, provider: IFormatProvider) : string
property JsonProvider<...>.Asset.DownloadCount: int
val concat : arrays:seq<'T []> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.concat
val groupBy : projection:('T -> 'Key) -> array:'T [] -> ('Key * 'T []) [] (requires equality)

Full name: Microsoft.FSharp.Collections.Array.groupBy
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val k : string
val v : (string * int) []
val sumBy : projection:('T -> 'U) -> array:'T [] -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Array.sumBy
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val all : string * int

Full name: Script.all


 Data start day and total count.
 ("2017-07-07", 36158)
val daily : (string * int) []
val from : string
val minBy : projection:('T -> 'U) -> array:'T [] -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.minBy
val total : int
Raw view New version

More information

Link:http://fssnip.net/7Te
Posted:3 months ago
Author:Tuomas Hietanen
Tags: jsonprovider