Cron job and english translation. #2

Closed
mwoolweaver wants to merge 30 commits from cron-job into master
6 changed files with 78 additions and 26 deletions

View File

@ -2,16 +2,16 @@
Send a daily tweet with your Pi-Hole statistics!
# How to use
1. Install Python 3
2. `pip3 install -U -r requirements.txt`
3. Copy config.ini.example to config.ini and adjust it
* `api_path` = Path to your /admin/api.php of Pi-Hole
* Tokens: Create an application [here](https://apps.twitter.com/)
4. Run it!
1. Create an application via Twitter [here](https://apps.twitter.com/). You will need the tokens for this to work.
# Cronjob
This will tweet your stats at 23:59 everyday and redirects output to /dev/null:
2. ```curl -sSL https://raw.githubusercontent.com/mwoolweaver/Pi-Hole-stats-tweeter/master/install.sh | bash```
the install script will ask if you want to install as a cronjob
upon successful cron completion you will find ~/pihole_tweeter/twitter_bot.txt
#Credits
original install script was found [here](https://github.com/deathbybandaid/Pi-Hole-stats-tweeter/blob/master/install.sh)
```
59 23 * * * python3 /path/to/pihole_tweeter.py >/dev/null 2>&1
```

6
config.ini Normal file
View File

@ -0,0 +1,6 @@
[DEFAULT]
api_path = http://localhost/admin/api.php
consumer_key = VALUE1
consumer_secret = VALUE2
access_token = VALUE3
access_token_secret = VALUE4

View File

@ -1,6 +0,0 @@
[DEFAULT]
api_path = http://localhost/admin/api.php
consumer_key = CONSUMERKEY
consumer_secret = CONSUMERSECRET
access_token = ACCESS-TOKEN
access_token_secret = ACCESSTOKENSECRET

32
install.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/sh
## pihole tweeter
NAMEOFAPP="Pi-hole Stats Tweeter"
WHATITDOES="This script will install Pi-Hole-stats-tweeter and setup a cronjob that runs every day at 23:55"
{ if
(whiptail --title "$NAMEOFAPP" --yes-button "Skip" --no-button "Proceed" --yesno "Do you want to setup $NAMEOFAPP? $WHATITDOES" 8 78)
then
echo "Declined $NAMEOFAPP"
else
sudo apt-get install -y python3-pip
pip3 install -U -r https://raw.githubusercontent.com/mwoolweaver/Pi-Hole-stats-tweeter/master/requirements.txt
wget https://raw.githubusercontent.com/mwoolweaver/Pi-Hole-stats-tweeter/master/pihole_tweeter.py -P ~/pihole_tweeter/
wget https://raw.githubusercontent.com/mwoolweaver/Pi-Hole-stats-tweeter/master/config.ini -P ~/pihole_tweeter/
CONSUMER_KEY=$(whiptail --inputbox "Consumer Key" 20 60 "" 3>&1 1>&2 2>&3)
CONSUMER_SECRET=$(whiptail --inputbox "Consumer Secret" 20 60 "" 3>&1 1>&2 2>&3)
ACCESS_TOKEN=$(whiptail --inputbox "Access Token" 20 60 "" 3>&1 1>&2 2>&3)
ACCESS_TOKEN_SECRET=$(whiptail --inputbox "Access Token Secret" 20 60 "" 3>&1 1>&2 2>&3)
sed -ie "s/VALUE1/$CONSUMER_KEY/" ~/pihole_tweeter/config.ini
sed -ie "s/VALUE2/$CONSUMER_SECRET/" ~/pihole_tweeter/config.ini
sed -ie "s/VALUE3/$ACCESS_TOKEN/" ~/pihole_tweeter/config.ini
sed -ie "s/VALUE4/$ACCESS_TOKEN_SECRET/" ~/pihole_tweeter/config.ini
fi }
{ if
(whiptail --title "$NAMEOFAPP" --yes-button "Skip" --no-button "Proceed" --yesno "Do you want to setup $NAMEOFAPP as a cronjob?" 8 78)
then
rm ~/pihole_tweeter/config.inie
echo "did not install cronjob"
else
sudo wget http://raw.githubusercontent.com/mwoolweaver/Pi-Hole-stats-tweeter/master/pi-hole-tweeter-cron -P /etc/cron.d/
rm ~/pihole_tweeter/config.inie
fi }

1
pi-hole-tweeter-cron Normal file
View File

@ -0,0 +1 @@
55 23 * * * root cd ~/pihole_tweeter/ && python3 pihole_tweeter.py >> ~/pihole_tweeter/twitter_bot.txt

View File

@ -3,8 +3,12 @@
import sys
from configparser import ConfigParser
from datetime import datetime
import psutil
import os
from hurry.filesize import size
from uptime import uptime
import tweepy
import platform
from requests import get
config = ConfigParser()
@ -28,10 +32,23 @@ if not (api_path, consumer_key, consumer_key, consumer_secret, access_token, acc
print('2 Please check your config.ini.')
sys.exit(1)
def pretty_time_delta(seconds):
seconds = int(seconds)
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
if days > 0:
return '%dd %dh %dm %ds' % (days, hours, minutes, seconds)
elif hours > 0:
return '%dh %dm %ds' % (hours, minutes, seconds)
elif minutes > 0:
return '%dm %ds' % (minutes, seconds)
else:
return '%ds' % (seconds,)
def comma_value(num):
"""Helper function for thousand separators"""
return "{:,}".format(int(num)).replace(',', '.')
return "{:,}".format(int(num)).replace(',', ',')
def get_api():
@ -64,13 +81,15 @@ def get_pihole_data():
return data
def construct_tweet(data):
today = datetime.today().strftime("%d.%m.%Y")
tweet = 'Pi-Hole-Statistik für den {date}:\n'.format(date=today)
tweet += 'Blockierte Werbung: ' + str(comma_value(data['ads_blocked_today']))
tweet += ' (' + str(round(data['ads_percentage_today'], 2)).replace('.', ',') + ' %)\n'
tweet += 'DNS-Abfragen: ' + str(comma_value(data['dns_queries_today'])) + '\n'
tweet += 'Domains auf der Blacklist: ' + str(comma_value(data['domains_being_blocked']))
tweet = '#ComputeHole: The @The_Pi_Hole on @GoogleCompute\n'
tweet += 'Ads Blocked: ' + str(comma_value(data['ads_blocked_today']))
tweet += ' (' + str(round(data['ads_percentage_today'], 2)).replace('.', '.') + '%)\n'
tweet += 'Total DNS Queries: ' + str(comma_value(data['dns_queries_today'])) + '\n'
tweet += 'Domains on Blocklist: ' + str(comma_value(data['domains_being_blocked'])) + '\n'
tweet += 'Uptime: ' + pretty_time_delta(uptime()) + '\n' + 'Load Avg: ' + str(os.getloadavg()) + '\n' + 'Ram Usage: ' + str(psutil.virtual_memory()[2]) + '% ' + str(size(psutil.virtual_memory()[3])) + '/' + str(size(psutil.virtual_memory()[1])) + '\n'
tweet += 'Kernel/OS: ' + str(platform.platform())
return tweet
@ -95,7 +114,7 @@ def main():
except tweepy.error.TweepError:
print('Status could not be posted.')
return
print('Status posted! https://twitter.com/' + status.author.screen_name + '/status/' + status.id_str)
print(status.id_str)
if __name__ == '__main__':