Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active June 1, 2024 11:03
Show Gist options
  • Save fabiolimace/d52af1191b124ada253f21dea31c3170 to your computer and use it in GitHub Desktop.
Save fabiolimace/d52af1191b124ada253f21dea31c3170 to your computer and use it in GitHub Desktop.
Github Stars Job
#!/bin/bash
#
# Job que atualiza diariamente series temporais de estrelas de repositorios do Github.
#
# Resumo do que este programa faz:
# 1. Os historicos das estrelas sao guardado nos arquivos *.log. Nunca os apague, pois assim perderá o histórico para sempre.
# 2. As estatisticas sao calculadas a partir dos historicos e guardadas nos arquivos *.tsv.
# 3. Os graficos gerados a partir das estatisticas sao guardadas nos arquivos *.png.
# 4. O email eh gerado e enviado com os arquivos *.log, *.tsv e *.png anexados.
#
# Como usar:
# # m h dom mon dow command
# 0 0 * * * /bin/bash /home/<USER>/github-stars-job.sh 2>&1 /dev/null
#
# Instalar dependencias:
# apk add bash
# apk add file
# apk add ssmtp
# apk add gnuplot
# wget https://gist.githubusercontent.com/fabiolimace/7208285193731291699203c724473caa/raw/9d3c48164badf187edd49ba8cd8b19a9e44d0125/compose-email.sh
#
# Gist: https://gist.github.com/fabiolimace/d52af1191b124ada253f21dea31c3170
#
email_to="email@gmail.com"
email_from="email@gmail.com"
email_subject="Github Stars Job"
email_body="Resultado do processamento do Github Stars Job em anexo."
function today {
date +"%Y-%m-%d"
}
function home_directory {
[[ "$(whoami)" == "root" ]] && echo /root || echo /home/$(whoami);
}
function github_stars {
local repo=${1}
wget -O- "${repo}" 2>&1 \
| grep -E '<span[^<]+<\/span>' \
| grep -E 'id="repo-stars-counter-star"' \
| grep -E -o 'title="[0-9,.]+"' \
| grep -E -o '[0-9,.]+' \
| tr -d ',.';
}
function updte_timeseries {
local repo=${1}
local file1="$(home_directory)/github-stars-job/${repo##*/}.log"
local file2="$(home_directory)/github-stars-job/${repo##*/}.tsv"
mkdir --parents $(home_directory)/github-stars-job
[[ -f "${file1}" ]] && sed -i "/$(today)/d" "${file1}";
printf "%s\t%s\t%s\n" $(today) $(github_stars "${repo}") >> "${file1}";
cat "${file1}" | sort | awk 'BEGIN { OFS="\t" } NR == 1 { $3 = 0; $4 = 0; acc = 0 } NR > 1 { $3 = $2 - prev; $4 = $3 + acc } {print $1, $2, $3, $4} { prev = $2; acc = $4 }' > "${file2}";
}
# --------------------------------
# Preparacao do diretorio
# --------------------------------
working_directory=$(home_directory)/github-stars-job
temporary_directory=$(mktemp -d)
mkdir --parents ${working_directory}
cd ${working_directory}
# --------------------------------
# Atualizar dados dos dados
# --------------------------------
updte_timeseries "https://github.com/f4b6a3/uuid-creator"
updte_timeseries "https://github.com/f4b6a3/ulid-creator"
updte_timeseries "https://github.com/f4b6a3/tsid-creator"
updte_timeseries "https://github.com/cowtowncoder/java-uuid-generator"
# --------------------------------
# Geracao dos graficos
# --------------------------------
gnuplot <<- EOF
set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y-%m-%d"
set xlabel "Date"
set ylabel "Count"
set terminal png size 1280,720
set xtics center rotate by 90 right
set output "github-stars-count.png"
plot "uuid-creator.tsv" using 1:2 with lines, "ulid-creator.tsv" using 1:2 with lines, "tsid-creator.tsv" using 1:2 with lines, "java-uuid-generator.tsv" using 1:2 with lines
set output "github-stars-growth.png"
plot "uuid-creator.tsv" using 1:4 with lines, "ulid-creator.tsv" using 1:4 with lines, "tsid-creator.tsv" using 1:4 with lines, "java-uuid-generator.tsv" using 1:4 with lines
EOF
# --------------------------------
# Envio do email
# --------------------------------
cat > ${temporary_directory}/email_headers.txt <<- EOF
To: ${email_to}
From: ${email_from}
Subject: ${email_subject}
EOF
cat > ${temporary_directory}/email_body.txt <<- EOF
${email_body}
EOF
$(home_directory)/compose-email.sh \
${temporary_directory}/email_headers.txt \
${temporary_directory}/email_body.txt \
*.log *.tsv *.png \
> ${temporary_directory}/email.eml
ssmtp "${email_to}" < ${temporary_directory}/email.eml
# remove temporary directory
rm -rf ${temporary_directory};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment