Update pihole_tweeter.py

This commit is contained in:
Michael Woolweaver 2019-04-23 05:20:08 -05:00 committed by GitHub
parent 108573cbd0
commit 7c16fbf119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,12 @@
import sys import sys
from configparser import ConfigParser from configparser import ConfigParser
from datetime import datetime from datetime import datetime
import psutil
import os
from hurry.filesize import size
from uptime import uptime
import tweepy import tweepy
import platform
from requests import get from requests import get
config = ConfigParser() config = ConfigParser()
@ -28,6 +32,19 @@ if not (api_path, consumer_key, consumer_key, consumer_secret, access_token, acc
print('2 Please check your config.ini.') print('2 Please check your config.ini.')
sys.exit(1) 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): def comma_value(num):
"""Helper function for thousand separators""" """Helper function for thousand separators"""
@ -63,15 +80,16 @@ def get_pihole_data():
return data return data
print(platform.system())
def construct_tweet(data): def construct_tweet(data):
today = datetime.today().strftime("%m/%d/%Y") tweet = '#ComputeHole: The @The_Pi_Hole on @GoogleCompute\n'
tweet = 'Pi-hole statistics for {date}:\n'.format(date=today)
tweet += 'Ads Blocked: ' + str(comma_value(data['ads_blocked_today'])) tweet += 'Ads Blocked: ' + str(comma_value(data['ads_blocked_today']))
tweet += ' (' + str(round(data['ads_percentage_today'], 2)).replace('.', '.') + ' %)\n' tweet += ' (' + str(round(data['ads_percentage_today'], 2)).replace('.', '.') + '%)\n'
tweet += 'Total DNS Queries: ' + str(comma_value(data['dns_queries_today'])) + '\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 += 'Domains on Blocklist: ' + str(comma_value(data['domains_being_blocked'])) + '\n'
tweet += '#SkyHole: The @The_Pi_Hole on @GoogleCompute.' 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 return tweet