lav-outils/ccfmstat/ccfmstat.sh

157 lines
6.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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."
echo "Number of result per page has to be fixed un web GUI page."
}
# ############################################
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"
# Get web page with info about one audio record.
local pageUrl="https://cause-commune.fm/wp-admin/post.php?post=$id&action=edit"
local pageFile="${tmpPrefix}-pageid$id"
curl -s -b "$cookieFile" "$pageUrl" > "$pageFile"
# Extract file infos.
local pageInfoFile="${tmpPrefix}-pageinfoid$id"
grep "Total listens: " "$pageFile" > "$pageInfoFile"
line=$(<"$pageInfoFile")
#echo "TRACE $line\n"
#<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 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")
# Extract record date.
local pageDateFile="${tmpPrefix}-pagedateid$id"
grep "<input name=\"date_recorded\"" "$pageFile" > "$pageDateFile"
line=$(<"$pageDateFile")
#echo "TRACE $line\n"
#<input name="date_recorded" id="date_recorded" type="hidden" value="19-03-2019">
local recordedDate
if [[ $line =~ value=\"(.+)\" ]]; then
recordedDate=${BASH_REMATCH[1]}
else
recordedDate=
fi
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$recordedDate\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"
if [ $? -eq 0 ]; then
# <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\trecordedDate\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"
else
echo "Connexion error. Bad password?"
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"
# Clean temporary files.
# Clean.
if [ -n "${tmpPrefix}" ]; then
rm -f "${tmpPrefix}-"*
fi
fi