This repository has been archived on 2023-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
Pi-Hole-stats-tweeter/pihole_tweeter.py

98 lines
2.7 KiB
Python
Raw Normal View History

2017-11-22 19:57:35 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from configparser import ConfigParser
from datetime import datetime
import tweepy
from requests import get
config = ConfigParser()
try:
config.read_file(open('config.ini'))
except FileNotFoundError:
print('config.ini not found.')
sys.exit(1)
# API path
try:
api_path = config['DEFAULT']['api_path']
consumer_key = config['DEFAULT']['consumer_key']
consumer_secret = config['DEFAULT']['consumer_secret']
access_token = config['DEFAULT']['access_token']
access_token_secret = config['DEFAULT']['access_token_secret']
except KeyError as exception:
print('Please check your config.ini.')
sys.exit(1)
if not (api_path, consumer_key, consumer_key, consumer_secret, access_token, access_token_secret):
print('2 Please check your config.ini.')
sys.exit(1)
2019-04-16 05:40:04 +02:00
2017-11-22 19:57:35 +01:00
def get_api():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
def get_pihole_data():
try:
res = get(api_path)
except Exception as exception:
print('Could not contact API: ' + str(exception))
return
if res.status_code != 200:
print('Could not get data from Pi-Hole API.')
return
try:
data = res.json()
except:
print('Got no or invalid JSON.')
return
if not all(k in data for k in
('ads_blocked_today', 'ads_percentage_today', 'dns_queries_today', 'domains_being_blocked')):
print('This is not Pi-Hole JSON...')
return
return data
def construct_tweet(data):
2019-04-16 05:40:04 +02:00
today = datetime.today().strftime("%m.%d.%Y")
2019-04-16 05:41:39 +02:00
tweet = 'Pi-hole Stats for {date}:\n'.format(date=today)
2019-04-16 05:40:04 +02:00
tweet += 'Total Ads Blocked: ' + (data['ads_blocked_today'])
2019-04-16 05:41:39 +02:00
tweet += ' (' + (data['ads_percentage_today']) + ' %)\n'
2019-04-16 05:40:04 +02:00
tweet += 'Total DNS Queries: ' + (data['dns_queries_today']) + '\n'
tweet += 'Domains on Blacklist: ' + (data['domains_being_blocked'])
2017-11-22 19:57:35 +01:00
return tweet
def main():
# Twitter login
api = get_api()
try:
print('Logged in as @' + api.me().screen_name)
except tweepy.error.TweepError:
print('Error while logging in - check your credentials.')
return
# Get Pi-Hole info from API
data = get_pihole_data()
if not data:
return
# Tweet it!
tweet = construct_tweet(data)
try:
status = api.update_status(status=tweet)
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)
if __name__ == '__main__':
main()