776db0f0f3 | ||
---|---|---|
README.md | ||
go.mod | ||
go.sum | ||
main.go | ||
packages.txt | ||
popularities.txt | ||
trending-pkgs.go | ||
trending.txt |
README.md
The script is designed to fetch package popularity data from the https://pkgstats.archlinux.de/api/packages
API, calculate different metrics based on the popularity data, and then sort the packages according to the chosen metric. The sorting methods available are:
- Total Gain: Sort packages based on the increase in popularity.
- Relative Gain: Sort packages based on the percentage increase in popularity.
- Logarithmic Gain: Sort packages based on the ratio of the current popularity to the popularity from a while back.
- Combined Score: Sort packages based on a score that combines the total gain and relative gain.
- Tiered: First, sort packages based on the total gain. For packages with the same total gain, sort them based on their relative gain.
Here's a step-by-step explanation of how you can implement the script yourself:
-
Define the necessary data structures: Define structs to represent the package popularity data and the API response. For example, you might have a
PackagePopularity
struct to hold information about a single package, and aPackageData
struct to hold the API response. -
Fetch package data from the API: Write a function to fetch package data from the
https://pkgstats.archlinux.de/api/packages
API. This function should make an HTTP GET request to the API endpoint, passing the necessary query parameters (e.g.,limit
andoffset
) to fetch the data in batches. The API response should be unmarshaled into thePackageData
struct you defined earlier. -
Calculate gain metrics: Define functions to calculate the different gain metrics (total gain, relative gain, logarithmic gain, and combined score) based on the package popularity data. These functions should take a
PackagePopularity
struct as input and return the calculated metric. -
Sort packages based on the chosen metric: Write a function to sort the packages based on the chosen metric. This function should take a slice of
PackagePopularity
structs and the sorting method as input. It should create a slice ofPackageWithScore
structs (or a similar struct that holds the package name and the corresponding score) and sort this slice based on the chosen metric. -
Handle command-line arguments: Use the
flag
package to handle command-line arguments. This will allow users to specify the sorting method, whether to use cached package data, and the file path for caching/loading package data. -
Load or fetch package data: If the user chooses to use cached package data, load the package data from the specified file. Otherwise, fetch the package data from the API using the function you wrote in step 2.
-
Sort and print the packages: Call the sorting function you wrote in step 4, passing the package data and the chosen sorting method. Then, print the sorted package names to the console.
-
Implement caching (optional): If desired, you can implement caching functionality to save the fetched package data to a file and load it from the file on subsequent runs.
Throughout the implementation, you'll need to handle errors, parse JSON data, and potentially use third-party libraries (e.g., for handling command-line arguments or making HTTP requests).
Remember, this is a high-level explanation, and you'll need to write the actual code to implement the functionality. If you encounter any specific issues or have questions during the implementation process, feel free to ask for clarification or additional guidance.