Browse Source

small improvements

master
hfte 2 years ago
parent
commit
5291d8d032
  1. 36
      mastaface.py

36
mastaface.py

@ -21,10 +21,12 @@ import json
import pickle
import subprocess
import logging
# import tempfile
from datetime import datetime
from os import path
from facebook_scraper import get_posts
from mastodon import Mastodon
# from mastodon.Mastodon import MastodonAPIError, MastodonNetworkError, MastodonRatelimitError, MastodonUnauthorizedError
from vendor import split_tweet
logging.basicConfig(filename='mastaface.log',level=logging.INFO)
@ -39,16 +41,19 @@ logging.info('Execute mastaface at ' + datetime.now().strftime("%Y-%m-%d, %H:%M:
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)
try:
config = json.load(f)
except:
logging.error('Config file in ' + config_filename + ' is not valid json')
except:
logging.error('Config file ' + config_filename + ' not found')
exit
logging.info('Loaded config from ' + config_filename)
logging.debug('Loaded config from ' + config_filename)
# Get history-database
# Check if database file exists
if path.exists(database_filename):
logging.info('Found briding-history-database at ' + database_filename)
logging.debug('Found briding-history-database at ' + database_filename)
try:
database = pickle.load( open( database_filename, "rb" ) )
except Exception as e:
@ -74,7 +79,12 @@ for bridge in config['bridges']:
mastodon_character_limit = mastodon_character_limit_default - 30
else:
mastodon_character_limit = mastodon_character_limit_default
toots_text = split_tweet.split_tweet(post['post_text'], mastodon_character_limit)
# Define text to too
if len(post['post_text']) == 0: # for example when only a link is shared
post_text = post['shared_text'] # usally the title of a shared link
else:
post_text = post['post_text']
toots_text = split_tweet.split_tweet(post_text, mastodon_character_limit)
# Check if current bridge already exists in database
if bridge['facebook_page'] not in database:
database[bridge['facebook_page']] = {}
@ -84,15 +94,25 @@ for bridge in config['bridges']:
# Check if there is a new post, or the last one has been edited
if post['time'] != database[bridge['facebook_page']]['last_edited']:
if post['post_id'] == database[bridge['facebook_page']]['last_post_id']:
logging.info('Last bridged post of https://www.facebook.com/' + bridge['facebook_page'] + ' has been updated')
# Last post has been updated, so first delete the old toot
for status in database[bridge['facebook_page']]['mastodon_statuses']:
Mastodon.status_delete(status['id'])
logging.debug('Deleted post ' + str(status['id']) + ' on mastodon because of facebook edit')
logging.info('Deleted post ' + str(status['id']) + ' on mastodon because of facebook edit')
else:
logging.info('New post on https://www.facebook.com/' + bridge['facebook_page'] + ' found')
statuses = None
statuses = []
# Upload image if it exists
if post['image']:
url = post['image']
# TO DO
# fp = tempfile.TemporaryFile()
# fp.write(b'Hello world!')
# fp.seek(0)
# fp.read()
# fp.close()
# Using curl is necessary, because facebook's secure-image is very restrictive
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',
@ -117,7 +137,7 @@ for bridge in config['bridges']:
logging.info('Bridged post' + str(post['post_id']) + ' to ' + str(statuses[-1]['id']) )
else:
statuses.append(mastodon.status_post(toot, in_reply_to_id = statuses[-1], visibility = 'unlisted'))
# After bridging was successfull update database of bridging history
# If database structure for current bridge does not exist yet, initialize it.
database[bridge['facebook_page']] = {}
database[bridge['facebook_page']]['last_post_id'] = post['post_id']
database[bridge['facebook_page']]['last_edited'] = post['time']

Loading…
Cancel
Save