Browse Source

add logging

master
hfte 2 years ago
parent
commit
7b6482f75b
  1. 155
      mastaface.py

155
mastaface.py

@ -1,3 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Public Facebook-Page to Mastodon Cross-Poster
# Copyright (C) 2020 André Menrath (andre.menrath@posteo.de)
#
@ -14,85 +17,111 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from facebook_scraper import get_posts
from mastodon import Mastodon
import json
import pickle
from os import path
import subprocess
import logging
from datetime import datetime
from os import path
from facebook_scraper import get_posts
from mastodon import Mastodon
from vendor import split_tweet
logging.basicConfig(filename='mastaface.log',level=logging.ERROR)
# Global variables
database_filename = 'database.pkl'
mastodon_character_limit = 500
config_filename = 'config.json'
mastodon_character_limit_default = 500
# Open config file
with open('config.json', 'r') as f:
config = json.load(f)
logging.info('Execute mastaface at', datetime.now())
# Generate Mastodon Python API Object
mastodon = Mastodon(
access_token = config['mastodon_access_token'],
api_base_url = config['mastodon_api_base_url']
)
try: # Open config file and verify it is valid json
with open(config_filename, 'r') as f:
logging.debug('found config:', config_filename)
config = json.load(f)
except Exception as e:
logging.error(e)
exit
logging.info('Loaded config from', config_filename)
# Get history-database
# Check if Database file exists
# Check if database file exists
if path.exists(database_filename):
database = pickle.load( open( database_filename, "rb" ) )
logging.info('found briding-history-database at', database_filename)
try:
database = pickle.load( open( database_filename, "rb" ) )
except Exception as e:
logging.error(e)
logging.info('Loaded bridging-history-database')
else:
logging.warning('No bridging-history-database found at', database_filename)
# initialize database
database = {}
database[config['facebook_page']] = {'last_post_id': None,
'last_edited': None,
'mastodon_statuses': None }
# Get facebook page
posts = get_posts(config['facebook_page'], pages=1)
# Extract last post
posts_list = list(posts)
post = posts_list[0]
# Split-up facebook post into tweets according to character limit
toots_text = split_tweet.split_tweet(post['post_text'], mastodon_character_limit - 30)
# Check if there is a new post, or the last one has been edited
if post['time'] != database[config['facebook_page']]['last_edited']:
if post['post_id'] == database[config['facebook_page']]['last_post_id']:
# Last post has been updated, so first delete the old toot
for status in database[config['facebook_page']]['mastodon_statuses']:
Mastodon.status_delete(status['id'])
statuses = None
statuses = []
# Upload image if it exists
if post['image']:
url = post['image']
subprocess.run(['curl', url, '--output', 'tmp.jpg',
"-H", 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0',
"-H", 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
"-H", 'Accept-Language: de,en-US;q=0.7,en;q=0.3', "--compressed" ,
"-H", 'DNT: 1',
"-H", 'Connection: keep-alive', "-H", 'Upgrade-Insecure-Requests: 1'])
media = mastodon.media_post("tmp.jpg")
# Toot!
# If post is too long, split it into main-toot and non-listed replies.
is_main_toot = True
for toot_text in toots_text:
if post['link'] and is_main_toot:
toot = toot_text + '\n\n' + post['link']
for bridge in config['bridges']:
try: # Generate Mastodon Python API Object
mastodon = Mastodon(
access_token = bridge['mastodon_access_token'],
api_base_url = bridge['mastodon_api_base_url']
)
# Get first=last facebook page
posts = get_posts(bridge['facebook_page'], pages=1)
# Extract very last post
post = list(posts)[0]
# Split-up facebook post into tweets according to character limit, and reserve room for links
if post['link']:
mastodon_character_limit = mastodon_character_limit_default - 30
else:
toot = toot_text
if is_main_toot:
mastodon_character_limit = mastodon_character_limit_default
toots_text = split_tweet.split_tweet(post['post_text'], mastodon_character_limit)
# Check if there is a new post, or the last one has been edited
if post['time'] != database[config['facebook_page']]['last_edited']:
if post['post_id'] == database[config['facebook_page']]['last_post_id']:
# Last post has been updated, so first delete the old toot
for status in database[config['facebook_page']]['mastodon_statuses']:
Mastodon.status_delete(status['id'])
logging.debug('Deleted post', status['id'], 'on mastodon because of facebook edit')
statuses = None
statuses = []
# Upload image if it exists
if post['image']:
statuses.append(mastodon.status_post(toot, media_ids=media))
else:
statuses.append(mastodon.status_post(toot))
is_main_toot = False
url = post['image']
subprocess.run(['curl', url, '--output', 'tmp.jpg',
"-H", 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0',
"-H", 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
"-H", 'Accept-Language: de,en-US;q=0.7,en;q=0.3', "--compressed" ,
"-H", 'DNT: 1',
"-H", 'Connection: keep-alive', "-H", 'Upgrade-Insecure-Requests: 1'])
media = mastodon.media_post("tmp.jpg")
# Toot
# If post is too long, split it into main-toot and non-listed replies.
is_main_toot = True
for toot_text in toots_text:
if post['link'] and is_main_toot:
toot = toot_text + '\n\n' + post['link']
else:
toot = toot_text
if is_main_toot:
if post['image']:
statuses.append(mastodon.status_post(toot, media_ids=media))
else:
statuses.append(mastodon.status_post(toot))
is_main_toot = False
logging.info('Bridged post', post['post_id'], 'to', statuses[-1]['id'] )
else:
statuses.append(mastodon.status_post(toot), in_reply_to_id = statuses[-1], visibility = 'unlisted' )
else:
statuses.append(mastodon.status_post(toot), in_reply_to_id = statuses[-1], visibility = 'unlisted' )
# Update database of bridging history
database[config['facebook_page']]['last_post_id'] = post['post_id']
database[config['facebook_page']]['last_edited'] = post['time']
database[config['facebook_page']]['mastodon_statuses'] = statuses
logging.info('No updates to bridge for', ['facebook_page'] )
except Exception as e:
logging.error('Error bridging', bridge['facebook_page'])
logging.debug(e)
continue
# After bridging was successfull update database of bridging history
database[config['facebook_page']]['last_post_id'] = post['post_id']
database[config['facebook_page']]['last_edited'] = post['time']
database[config['facebook_page']]['mastodon_statuses'] = statuses
# Save database to file
pickle.dump(database, open(database_filename, "wb" ))
logging.debug('updated bridging-history-database at', database_filename)
Loading…
Cancel
Save