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
}
@OptimusPrime
creator
link
fedilink
18d

The function get_package_names() retrieves a list of package names by repeatedly making requests to an API endpoint with a limit and offset parameter. The total variable is used to determine when to stop making requests, while the offset variable is incremented by the limit after each request. The response from the API is a JSON object that is parsed and the package names are added to the packages 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:

func get_package_names() []string {
    var packages []string
    var total = 50 // get_total_packages()
    var offset, limit int = 0, 100

    for offset < total {
        var pkgs []string
        pkgs, offset = get_pkgs_from_api(offset, limit)
        packages = append(packages, pkgs...)
    }
    return packages
}

func get_pkgs_from_api(offset int, limit int) ([]string, int) {
    relURL := "packages?limit=" + fmt.Sprint(limit) + "&offset=" + fmt.Sprint(offset)
    var response Response
    response = requestJSON(relURL)
    var pkgs []string
    for _, pkg := range response.PackagePopularities {
        pkgs = append(pkgs, pkg.Name)
    }
    offset += limit
    return pkgs, offset
}

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 the get_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.

Pastebin -> sub migrated
!pastebin

    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

    • 0 users online
    • 1 user / day
    • 1 user / week
    • 2 users / month
    • 4 users / 6 months
    • 18 subscribers
    • 12 Posts
    • 2 Comments
    • Modlog