Added script to get statistics from https://cause-commune.fm/ website.

This commit is contained in:
Christian P. MOMON 2019-04-10 22:49:25 +02:00 committed by Christian P. MOMON
parent 2f7bd74b05
commit 0feb10ba0f

141
ccfmstat/ccfmstat.sh Executable file
View File

@ -0,0 +1,141 @@
#!/bin/bash
#
# Copyright (C) 2019 Christian Pierre MOMON <cmomon@april.org>
#
# This file is part of ccfmstat from "April/Libre à vous !"
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Configuration.
login="april-cc"
tmpPrefix="/tmp/ccfmstat-"
# ############################################
help()
{
echo "Usage:"
echo " ccfmstat [ -h | -help | --help ]"
echo "If no parameter set then password is requested."
}
# ############################################
extractValue()
{
local line="$1"
local token="$2"
local result=
local regexp="$token: <b>([0-9]+)</b>"
if [[ $line =~ $regexp ]]; then
result=${BASH_REMATCH[1]}
else
result=
fi
echo "$result"
}
# ############################################
getInfoFromId()
{
local id="$1"
local title="$2"
local cookieFile="$3"
#<p class="episode-stat-data total-downloads">Total listens: <b>109</b></p><p class="episode-stat-data total-listeners">Total listeners: <b>92</b></p><p class="episode-stat-data sources">Listening sources:</p><ul class="sources-list"><li class="itunes">iTunes: <b>7</b></li><li class="overcast">Overcast: <b>1</b></li><li class="direct">Direct download: <b>9</b></li><li class="new_window">Played in new window: <b>6</b></li><li class="player">Audio player: <b>23</b></li><li class="podcast_addict">Podcast Addict: <b>44</b></li><li class="playerfm">Player FM: <b>44</b></li><li class="unknown">Other: <b>63</b></li></ul><p><a href="https://cause-commune.fm/wp-admin/edit.php?post_type=podcast&page=podcast_stats&filter=episode&episode=10001">See more detail &raquo;</a><p></div>
local pageUrl="https://cause-commune.fm/wp-admin/post.php?post=$id&action=edit"
local pageFile="$tmpPrefix-pageid$id"
curl -s -b "$cookieFile" "$pageUrl" | grep "Total listens: " > "$pageFile"
line=$(<"$pageFile")
#echo "TRACE $line\n"
if [ -f "$pageFile" ]; then
rm -f "$pageFile"
fi
local totalListens=$(extractValue "$line" "Total listens")
local totalListeners=$(extractValue "$line" "Total listeners")
local iTunes=$(extractValue "$line" "iTunes")
local pocketCasts=$(extractValue "$line" "Pocket Casts")
local overcast=$(extractValue "$line" "Overcast")
local directDownload=$(extractValue "$line" "Direct download")
local playedInNewWindow=$(extractValue "$line" "Played in new window")
local audioPlayer=$(extractValue "$line" "Audio player")
local podcastAddict=$(extractValue "$line" "Podcast Addict")
local playerFM=$(extractValue "$line" "Player FM")
local other=$(extractValue "$line" "Other")
echo -e "$id\t$totalListens\t$totalListeners\t$iTunes\t$pocketCasts\t$overcast\t$directDownload\t$playedInNewWindow\t$audioPlayer\t$podcastAddict\t$playerFM\t$other\t$title"
}
# ############################################
run()
{
local pass="$1"
# Connect.
local loginUrl="https://cause-commune.fm/wp-login.php"
local redirect="https%3A%2F%2Fcause-commune.fm%2Fwp-admin%2F"
local cookieFile="$tmpPrefix-cookies.txt"
curl -s -c "$cookieFile" -d "log=$login&pwd=$pass&rememberme=forever&wp-submit=Se%20connecter&redirect_to=$redirect&testcookie=1" "$loginUrl" > /dev/null
# Get page "Les miens" data.
pageUrl="https://cause-commune.fm/wp-admin/edit.php?post_type=podcast&author=5"
idsFile="$tmpPrefix-listmiens"
curl -s -b "$cookieFile" "$pageUrl" | grep row-title > "$idsFile"
# <strong><a class="row-title" href="https://cause-commune.fm/wp-admin/post.php?post=10518&amp;action=edit" aria-label="« Wikipédia &#8211; chronique « Partager est bon » sur écoles et logiciels libres » (Modifier)">Wikipédia &#8211; chronique « Partager est bon » sur écoles et logiciels libres</a></strong>
echo -e "id\ttotalListens\ttotalListeners\tiTunes\tpocketcasts\tovercast\tdirectDownload\tplayedInNewWindow\taudioPlayer\tpodcastAddict\tplayerFM\tother\ttitle"
while read line; do
#echo $line
[[ $line =~ post=([0-9]+)\&amp ]] && id=${BASH_REMATCH[1]}
#echo "TRACE id=$id"
[[ $line =~ aria-label=\".+\"\>(.+)\</a\> ]] && title=${BASH_REMATCH[1]}
#echo "TRACE title=$title"
getInfoFromId "$id" "$title" "$cookieFile"
done < "$idsFile"
if [ -f "$cookieFile" ]; then
rm -f "$cookieFile"
fi
if [ -f "$idsFile" ]; then
rm -f "$idsFile"
fi
}
# ###########################################
# MAIN
# ###########################################
if [ "$#" = 1 ] && ([ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]); then
help
else
(>&2 echo "Password for $login account on https://cause-commune.fm/ website:")
read -s pass
# Encode as URL characters.
pass="${pass//&/%26}"
run "$pass"
fi