-
Notifications
You must be signed in to change notification settings - Fork 7
/
mastodon-post-last.py
executable file
·117 lines (98 loc) · 3.11 KB
/
mastodon-post-last.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
#! /usr/bin/env python3
from mastodon import Mastodon
import sys
import feedparser
from random import randrange
import requests
import configparser
import os
import time
import random
import argparse
import json
HOME = os.getenv('HOME')
CONFIG = f"{HOME}/.config/toot/config.json"
# old configuration
APICONFIG = f"{HOME}/.twitterc"
def ReverseLink(link):
req = requests.get(link)
if req.status_code != 200:
print("Failed status code:", req.status_code)
return None
return req.url
def ShortMe(link, apikey):
shortened = None
failure_counter = 0
while shortened is None:
try:
url = 'https://hl.eng.br/api.php?url=' + link
if apikey is not None:
url += '&key=' + apikey
print("url=%s" % url)
req = requests.get(url)
if req.status_code == 200:
reverse = ReverseLink(req.text)
print("reverse=%s" % reverse)
if reverse == link:
shortened = req.text
except:
print("Shortening failed.")
sleepMinutes(randomMinutes(5))
print("Trying again")
failure_counter += 1
if failure_counter > 10:
raise Exception("Failed to shortener link.")
return shortened
def sleepMinutes(minutes):
print(f'Sleeping {minutes} minutes')
time.sleep(minutes * 60)
def randomMinutes(limit):
return random.randint(1, limit)
def GetAPIKey():
"""
Configuration from file ~/.twitterc
"""
global apikey
cfg = configparser.ConfigParser()
print(f"Reading configuration: {APICONFIG}")
if not os.path.exists(APICONFIG):
print(f"Failed to find configuration file {APICONFIG}")
exit(1)
cfg.read(APICONFIG)
apikey = cfg.get("SHORTENER", "APIKEY")
return apikey
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='It posts the last published article')
parser.add_argument("--userid", help="Your registered mastodon account at toot configuration")
args = parser.parse_args()
if args.userid is None:
print('ERROR: missing --userid')
parser.print_help()
sys.exit(os.EX_NOINPUT)
userid = args.userid
print("Getting API key for shortener")
apikey = GetAPIKey()
print("Autenticating in Mastodon")
with open(CONFIG) as tootConfig:
config = json.load(tootConfig)
mastodon = Mastodon(
access_token = config['users'][userid]['access_token'],
api_base_url = config['users'][userid]['instance']
)
print('Mastodon login completed')
print("Reading site's RSS")
site = "http://helio.loureiro.eng.br/index.php?format=feed&type=rss"
feed = feedparser.parse(site)
try:
rss = feed['entries'][0]
except IndexError:
print("No entries found in the rss feed")
sys.exit(0)
title = rss.title
link = rss.link
print("Shortening ", link)
shortlink = ShortMe(link, apikey)
print("5 seconds before posting - in case to cancel")
time.sleep(5)
print(f"Publishing: {title} {shortlink}")
mastodon.toot(f"Novo post: {title} {shortlink}")