package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const API string = "https://pkgstats.archlinux.de/api/"
type PkgPopularity struct {
Name string
Popularities []float64
}
type PackagePopularity struct {
Name string `json:"name"`
Samples int `json:"samples"`
Count int `json:"count"`
Popularity float64 `json:"popularity"`
StartMonth int `json:"startMonth"`
EndMonth int `json:"endMonth"`
}
type Response struct {
Total int `json:"total"`
Count int `json:"count"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Query interface{} `json:"query"`
PackagePopularities []PackagePopularity `json:"packagePopularities"`
}
func main() {
/*browsers := []string{"firefox", "chromium", "google-chrome", "epiphany", "konqueror", "tor-browser", "vivaldi", "opera", "midori"}
fmt.Println(get_popularities(browsers))*/
get_trending_packages()
}
func get_trending_packages() {
fmt.Println(get_popularities(get_package_names()))
}
func get_popularities(pkgs []string) []PkgPopularity {
var popularities []PkgPopularity
for _, pkg := range pkgs {
popularities = append(popularities, PkgPopularity{pkg, get_package_monthly_popularity(pkg)})
}
return popularities
}
func get_total_packages() int {
var response Response
response = requestJSON("packages?limit=1")
return response.Total
}
func get_package_names() []string {
var packages []string
var total = 50 // get_total_packages()
var offset, limit int = 0, 100
for offset < total {
relURL := "packages?limit=" + fmt.Sprint(limit) + "&offset=" + fmt.Sprint(offset)
var response Response
response = requestJSON(relURL)
for _, pkg := range response.PackagePopularities {
packages = append(packages, pkg.Name)
}
offset += limit
}
return packages
}
func get_package_monthly_popularity(pkg string) []float64 {
relURL := "packages/" + pkg + "/series?startMonth=201009"
var response Response
response = requestJSON(relURL)
var popularities []float64
for _, pkg := range response.PackagePopularities {
popularities = append(popularities, pkg.Popularity)
}
return popularities
}
func requestJSON(relURL string) Response {
resp, err := http.Get(API + relURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatal(err)
}
return response
}
Migrating too https://lemmy.ca/c/pastebin
Text storage sublemmy. It can be used to help promote lemmy by linking to c/pastebin over say a commercial pastebin website
The function
get_package_names()
retrieves a list of package names by repeatedly making requests to an API endpoint with alimit
andoffset
parameter. Thetotal
variable is used to determine when to stop making requests, while theoffset
variable is incremented by thelimit
after each request. The response from the API is a JSON object that is parsed and the package names are added to thepackages
slice.One possible way to split this function would be to extract the logic for making the API request and parsing the response into a separate function:
This way, the
get_package_names()
function is now only responsible for handling the looping and appending the package names to the packages slice, while theget_pkgs_from_api()
function is responsible for making the API request and parsing the response. This makes the code easier to understand, maintain and test.