Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active March 9, 2022 00:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfan5/7132921 to your computer and use it in GitHub Desktop.
Save sfan5/7132921 to your computer and use it in GitHub Desktop.
Minetest Forum statistics
Missing Things:
* jquery.min.js ( http://jquery.com/download/ )
* highstock.js ( http://www.highcharts.com/download )
* A cronjob for executing getvals.py
* A webserver with PHP [with SQLite3]
<html>
<head>
<title>Minetest Forum Statistics</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="highstock.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$.ajax("forumstats.php").done(function(data) {
var d = JSON.parse(data);
$('#users').highcharts('StockChart', {
rangeSelector: {
selected: 1,
},
title: {
text: 'Total number of registered users'
},
series: [{
name: 'Users',
data: d.users,
}]
});
$('#topics').highcharts('StockChart', {
rangeSelector: {
selected: 1,
},
title: {
text: 'Total number of topics'
},
series: [{
name: 'Topics',
data: d.topics,
}]
});
$('#posts').highcharts('StockChart', {
rangeSelector: {
selected: 1,
},
title: {
text: 'Total number of posts'
},
series: [{
name: 'Posts',
data: d.posts,
}]
});
$('#newposts').highcharts('StockChart', {
rangeSelector: {
selected: 1,
},
title: {
text: 'New posts'
},
series: [{
name: 'Posts',
data: d.newposts,
}]
});
$('#newusers').highcharts('StockChart', {
rangeSelector: {
selected: 1,
},
title: {
text: 'New users'
},
series: [{
name: 'Users',
data: d.newusers,
}]
});
});
});
</script>
</head>
<body>
<h1>Minetest Forum Statistics</h1>
<h4>by sfan5</h4>
<div id="users"></div>
<div id="topics"></div>
<div id="posts"></div>
<div id="newusers"></div>
<div id="newposts"></div>
<div style="font-size: x-small">
Powered by <a href="http://jquery.com">JQuery</a>, <a href="http://www.highcharts.com/products/highstock">Highstock</a>, <a href="http://php.net">PHP</a>, <a href="http://python.org/">Python</a> and <a href="http://sqlite.org">SQLite</a>
</div>
</body>
</html>
<?php
$db = new SQLite3('/home/user/forumstats/forumstats.sqlite');
$stmt = $db->prepare("SELECT * FROM forumstats");
$r = $stmt->execute();
$users = "";
$topics = "";
$posts = "";
$newusers = "";
$newposts = "";
$lastrow = FALSE;
while($row = $r->fetchArray())
{
$users .= "[" . $row["time"] . "000," . $row["totalregd"] . "],";
$topics .= "[" . $row["time"] . "000," . $row["totaltopics"] . "],";
$posts .= "[" . $row["time"] . "000," . $row["totalposts"] . "],";
if($lastrow === FALSE) {
$newusers .= "[" . $row["time"] . "000,0],";
$newposts .= "[" . $row["time"] . "000,0],";
} else {
$newusers .= "[" . $row["time"] . "000," . ($row["totalregd"] - $lastrow["totalregd"]) . "],";
$newposts .= "[" . $row["time"] . "000," . ($row["totalposts"] - $lastrow["totalposts"]) . "],";
}
$lastrow = $row;
}
$users = substr($users, 0, -1);
$topics = substr($topics, 0, -1);
$posts = substr($posts, 0, -1);
$newusers = substr($newusers, 0, -1);
$newposts = substr($newposts, 0, -1);
echo '{"users": [' . $users . '], "topics": [' . $topics . '], "posts": [' . $posts . '], "newusers": [' . $newusers .'], "newposts": [' . $newposts .']}';
?>
import sqlite3, urllib, re, time
totalregd_r = re.compile(r"Total members <strong>([0-9]+)</strong>")
totaltopics_r = re.compile(r"Total topics <strong>([0-9]+)</strong>")
totalusers_r = re.compile(r"Total posts <strong>([0-9]+)</strong>")
db = sqlite3.connect("forumstats.sqlite")
c = db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS forumstats (time int, totalregd int, totaltopics int, totalposts int)")
u = urllib.urlopen("https://forum.minetest.net/index.php")
content = u.read()
m = totalregd_r.search(content)
if m:
totalregd = int(m.group(1).replace(",", ""))
else:
totalregd = -1
m = totaltopics_r.search(content)
if m:
totaltopics = int(m.group(1).replace(",", ""))
else:
totaltopics = -1
m = totalusers_r.search(content)
if m:
totalusers = int(m.group(1).replace(",", ""))
else:
totalusers = -1
v = (int(time.time()), totalregd, totaltopics, totalusers)
c.execute("INSERT INTO forumstats VALUES (?,?,?,?)", v)
c.close()
db.commit()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment