Skip to content

Instantly share code, notes, and snippets.

@Alexander01998
Created September 6, 2015 07:11
Show Gist options
  • Save Alexander01998/d5bda1558cfbc20a8ae3 to your computer and use it in GitHub Desktop.
Save Alexander01998/d5bda1558cfbc20a8ae3 to your computer and use it in GitHub Desktop.
Counter for all Downloads of all Releases in one Repository
var root = DriveApp.getFolderById("FOLDER ID HERE");
// The ID of the folder where you placed the script
var filename = "badge.svg";
// The SVG file holding the badge.
function update()
{
root.getFilesByName(filename)
.next()
.setContent(
HtmlService.createTemplateFromFile("index.html")
.evaluate()
.getContent());
}
function createFile()
{
root.createFile(filename, "", "image/svg+xml");
}
  1. Create a folder in Google Drive

  2. Create a new Apps Script in the folder

  3. Copy Code.gs and index.html into the script

  4. Replace FOLDER ID HERE in Code.gs with the ID of the folder containing the script.

  5. Replace Wurst-Imperium/Wurst-Client in index.html with the name of your repo.

  6. Run createFile() once.

  7. Add a trigger for update() that fires once per minute.

  8. Place this code where you want the badge to appear:

[![downloads](https://drive.google.com/uc?id=FOLDER_ID_HERE)](https://github.com/REPO_NAME_HERE/releases/latest)

(Make sure to replace FOLDER_ID_HERE and REPO_NAME_HERE with the folder ID and repo name.)

<?
var json = JSON.parse(UrlFetchApp.fetch("https://api.github.com/repos/Wurst-Imperium/Wurst-Client/releases"));
// Replace 'Wurst-Imperium/Wurst-Client' with the name of your repo
var downloads = 0;
for(release in json)
for(file in json[release].assets)
downloads += json[release].assets[file].download_count;
Logger.log("Downloads: " + downloads);
var text = downloads + "";
var regex = /(\d+)(\d{3})/;
while (regex.test(text))
{
text = text.replace(regex, '$1' + ' ' + '$2');
}
//text = Math.floor(downloads / 1e3) + " 000";
//rounding to thousands
var w = 138;
wb1 = 70;
var wb2 = w - wb1;
var xt2 = wb2 / 2 + wb1;
?>
<svg xmlns="http://www.w3.org/2000/svg" width="<?= w ?>" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="<?= w ?>" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h<?= wb1 ?>v20H0z"/>
<path fill="#4c1" d="M70 0h<?= wb2 ?>v20H70z"/>
<path fill="url(#b)" d="M0 0h<?= w ?>v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="36" y="15" fill="#010101" fill-opacity=".3">
downloads
</text>
<text x="36" y="14">
downloads
</text>
<text x="<?= xt2 ?>" y="15" fill="#010101" fill-opacity=".3">
<?= text ?>
</text>
<text x="<?= xt2 ?>" y="14">
<?= text ?>
</text>
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment