shields icon indicating copy to clipboard operation
shields copied to clipboard

Badge Request: PSGallery profile total downloads

Open NorskNoobing opened this issue 1 year ago • 1 comments

📋 Description

This badge would dynamically get the "Total downloads of packages", which is displayed on a PSGallery profile. I'd want it to look something like this static badge mockup, together with a link to the profile when you press the badge:

You can see the total downloads value on any PSGallery profile, but here's a link to mine for reference: https://www.powershellgallery.com/profiles/NorskNoobing

🔗 Data

There's already another badge called PowerShell Gallery Downloads, but it seems to be limited to only a single package, and therefore can't fetch the downloads of all your packages combined. It seems to be using the following API: https://www.powershellgallery.com/api/v2

Alternatively you could also scrape the profile website i.e. my profile on the following XPaths:

//*[@id="skippedToContent"]/section/section/aside/div/div[2]/h2/text()

or

/html/body/div[2]/section/section/aside/div/div[2]/h2/text()

🎤 Motivation

I'd want this to display the total downloads across all my PSGallery modules/packages on my GitHub profile README.

NorskNoobing avatar Oct 09 '24 16:10 NorskNoobing

Hi. Happy to look at a PR for this the information is available via the API.

We don't take new badges that rely on scraping. Keeping them working is too high-maintenance. From https://github.com/badges/shields/blob/master/CONTRIBUTING.md#badge-guidelines

Badges should not obtain data by scraping web pages - these are likely to break frequently. Whereas API publishers are incentivised to maintain a stable platform for their users, authors of web pages have no such incentive.

chris48s avatar Oct 11 '24 18:10 chris48s

Unfortunately the people at psgallery takes about half a year to respond. It seems like they don't really monitor their support mail. Anyways, here's the answer I got from them.

We do not have an API to fetch total PS module downloads for a particular owner, but we can run queries to find this info for you.

I would interpret this as that they can provide me with the number when I ask for it, but no automation. Is it possible to run scripts when fetching the badge numbers? Here are the different implementations I've thought of:

  1. Web scraping. This is what I've used for years, and it still works properly. PSGallery has never updated their website for as long as I've had the script deployed, but I understand the fragility of this implementation.
  2. Use the API endpoint that fetches one module individually, then have a list of modules to check, and sum the download counts together.

I'm not sure if the badge/shields usually do this many calculations, but it's atleast an option.

If none of the options described are wanted, then it'll probably be best to use the 2nd option implementation through something like a GitHub action, and have a static badge/shield to show the value. The problem with this implementation is that the GitHub action would make an insane amount of commits for each time the module downloads update. This could make it difficult to find your last non-automated commit. It also inflates the number of commits you've done per day on your GitHub profile.

NorskNoobing avatar Feb 07 '25 06:02 NorskNoobing

There have been some recent updates in the XML parsing library we use for the dynamic XML badge that improve the handling of HTML. So if you want to do it yourself, you could use the dynamic XML badge to do this. For example:

- https://img.shields.io/badge/dynamic/xml?url=https%3A%2F%2Fwww.powershellgallery.com%2Fprofiles%2FNorskNoobing&query=%2F%2F*%5B%40id%3D%22skippedToContent%22%5D%2Fsection%2Fsection%2Faside%2Fdiv%2Fdiv%5B2%5D%2Fh2&label=downloads

I think that is the best solution here if the only way to get the number is by scraping or making a lot of API calls.

Using a GitHub action to write out a static badge can be a great approach, but I think it works best when the metric changes (or might change) every time there is a commit to the repo or every time there is a release or whatever, rather than when the thing you're measuring changes independently of activity on the repo.

chris48s avatar Feb 07 '25 16:02 chris48s

Thanks! :) the dynamic badge is perfect for my use-case, as the psgallery devs most likely won't add an "all downloads" endpoint any time soon.

NorskNoobing avatar Feb 07 '25 19:02 NorskNoobing