Created
March 12, 2021 02:40
-
-
Save rosalogia/b3994cabda4bafa1fde63ccffc5634ba to your computer and use it in GitHub Desktop.
F# script for assessing the popularity of the forks of a repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: FSharp.Data" | |
open FSharp.Data | |
let args = System.Environment.GetCommandLineArgs() | |
let flags = | |
if args.Length > 3 then | |
args.[2..(args.Length - 2)] | |
else | |
[||] | |
let repositoryUrl = | |
if args.Length >= 3 then | |
sprintf "%s/network/members" args.[args.Length - 1] | |
else | |
failwith "Repository URL must be provided as command-line argument" | |
let page = HtmlDocument.Load(repositoryUrl) | |
let forks = | |
page.CssSelect("div.repo") | |
|> List.tail // Discard the first repository, which is the original | |
|> List.map (fun n -> // Map the forks to pairs of repository names and repository pages | |
let forkName = n.InnerText() | |
( | |
forkName, | |
forkName | |
|> sprintf "https://github.com/%s" | |
|> (HtmlDocument.Load) | |
) | |
) | |
let stars (forkName, forkPage: HtmlDocument) = | |
let starCount = | |
forkPage.CssSelect("a.social-count") | |
|> List.head | |
|> fun n -> n.InnerText().Trim() |> int | |
(forkName, starCount) | |
let results = | |
forks | |
|> List.map stars | |
if Array.contains "--sort" flags then | |
results | |
|> List.sortBy (fun r -> snd r * -1) | |
|> List.iter (fun (name, stars) -> printfn "%s %i" name stars) | |
else if Array.contains "--top" flags then | |
results | |
|> List.maxBy snd | |
|> (fun (name, stars) -> printfn "%s %i" name stars) | |
else | |
results | |
|> List.iter (fun (name, stars) -> printfn "%s %i" name stars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment