-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfediverse.py
134 lines (119 loc) · 5.16 KB
/
fediverse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Post new articles on Mastodon
"""
import sys
import string
from lxml import html
import os.path
import logging
logger = logging.getLogger(__name__)
from pelican import signals
# This plugin needs Mastodon.py (https://github.com/halcy/Mastodon.py)
from mastodon import Mastodon
# Collect the list of articles already published
def read_articleslist():
try:
with open('posted_on_Mastodon.txt', 'r') as f:
result = list(map(str.rstrip, f))
except IOError:
result = []
return result
# Write articles URL list
def write_articleslist(articleslist):
articleslist.sort()
with open('posted_on_Mastodon.txt', 'w') as f:
for article in articleslist:
f.write("%s\n" % article)
# Collect config info and start the main procedure
def post_on_mastodon(settings, new_posts):
global mt_base_url
mt_base_url = settings.get('MASTODON_BASE_URL', '')
global mt_username
mt_username = settings.get('MASTODON_USERNAME', '')
global mt_password
mt_password = settings.get('MASTODON_PASSWORD', '')
# check if config file has been duly filled or print an error message and exit
if mt_base_url == '' or mt_username == '' or mt_password == '':
logger.warning('Pelican_fediverse: Mastodon access credentials not configured...')
sys.exit(9)
# if pelicantoot_clientcred.secret does not exist it means we have to create the app on Mastodon
if os.path.exists('pelicanfediverse_clientcred.secret') == False:
Mastodon.create_app(
'PelicanFediverse',
api_base_url = mt_base_url,
to_file = 'pelicanfediverse_clientcred.secret'
)
# Advise the user with an on-screen message. We are ready to publish!
build_message = 'Publishing on Mastodon: %s (%s)'
for article in new_posts:
url = article.get_siteurl() + article.url
title = article.title
htmltext = build_message % (title.replace(' ',' '), url)
cleantext = html.fromstring(htmltext)
finaltext = cleantext.text_content().strip()
print(finaltext)
return True
# Extract the list of new posts
def post_updates(generator, writer):
articleslist = read_articleslist()
new_posts = []
for article in generator.articles:
if article.url not in articleslist:
new_posts.append(article)
# we only write the newly found sites to disk if posting them worked. that way we can retry later
if new_posts:
if post_on_mastodon(generator.settings, new_posts):
mastodon = Mastodon(
client_id = 'pelicanfediverse_clientcred.secret',
api_base_url = mt_base_url
)
mastodon.log_in(
mt_username,
mt_password,
to_file = 'pelicanfediverse_usercred.secret'
)
mastodon = Mastodon(
access_token = 'pelicanfediverse_usercred.secret',
api_base_url = mt_base_url
)
# Actually build the post structure
# First set a maximum length for the final post
toot_maxlength = 490 # Actually 500 but let's keep a safety gap for miscalculation...
for article in new_posts:
articleslist.append(article.url)
titlehtmltext = article.title
titlecleantext = html.fromstring(titlehtmltext)
title_to_publish = titlecleantext.text_content().strip() + '\n\n'
articlehtmltext = article.summary
articlecleantext = html.fromstring(articlehtmltext)
summary_to_publish = articlecleantext.text_content().strip() + '\n'
read_more = 'Read more... ' + article.get_siteurl() + '/' + article.url + '\n\n'
if hasattr(article, 'tags'):
taglist = article.tags
new_taglist = []
for i in taglist:
new_taglist.append('#' + str(i))
tags_to_publish = ', '.join(str(x).replace(" ", "") for x in new_taglist)
toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more) + len(tags_to_publish)
if toot_length > toot_maxlength:
truncate = toot_maxlength - len(title_to_publish) - len(tags_to_publish) - len(read_more) - 4
summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n'
mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish
else:
mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish
else:
toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more)
if toot_length > toot_maxlength:
truncate = toot_maxlength - len(title_to_publish) - len(read_more) - 4
summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n'
mastodon_toot = title_to_publish + summary_to_publish + read_more
else:
mastodon_toot = title_to_publish + summary_to_publish + read_more
mastodon.toot(mastodon_toot)
write_articleslist(articleslist)
def register():
try:
signals.article_writer_finalized.connect(post_updates)
except AttributeError:
pass