-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrss2mastodon.py
112 lines (106 loc) · 3.79 KB
/
rss2mastodon.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
"""
RSS -> Fediverse gateway
AI6YR - Nov 2022
"""
# imports (obviously)
import configparser
from mastodon import Mastodon
import requests
import time
from datetime import datetime
import dateutil
import json
import feedparser
from bs4 import BeautifulSoup
import re
import tempfile
import shutil
from PIL import Image
# Load the config
config = configparser.ConfigParser()
config.read('config.ini')
feedurl = config['feed']['feed_url']
feedname = config['feed']['feed_name']
feedvisibility = config['feed']['feed_visibility']
feedtags = config['feed']['feed_tags']
feeddelay = int(config['feed']['feed_delay'])
if (feeddelay < 60):
feeddelay = 300
max_image_size = int(config['mastodon']['max_image_size'])
print (feedurl)
print (feedname)
# connect to mastodon
mastodonBot = Mastodon(
access_token=config['mastodon']['access_token'],
api_base_url=config['mastodon']['app_url']
)
print ("Starting RSS watcher:" + feedname)
lastpost = ""
lastspottime = datetime.now().timestamp()
while(1):
data = (feedparser.parse(feedurl))
entries = data["entries"]
# print (entries)
for entry in entries:
#print (entry['summary'])
try:
link = entry['link']
except:
link = ""
clean = re.sub("<.*?>", "", entry['summary'])
clean = clean.replace("&" ,"&")
clean = clean.replace(" " ," ")
spottime = dateutil.parser.parse(entry['published']).timestamp()
firsttwo = clean[:2]
firstthree = clean[:3]
# if (1):
if (spottime > lastspottime):
if (clean == lastpost):
print ("skip: retweet")
elif ("RT" in firsttwo):
print ("skip: retweet")
elif ("Re" in firstthree):
print ("skip: reply")
else:
isposted = False
print (clean)
tootText = clean + feedtags
tootText = tootText[:475]
tootText = tootText + " " + link
soup = BeautifulSoup(entry['summary'], 'html.parser')
medialist = []
for img in soup.findAll('img'):
print("***IMAGE:",img.get('src'))
imgfile = img.get('src')
temp = tempfile.NamedTemporaryFile()
res = requests.get(imgfile, stream = True)
if res.status_code == 200:
shutil.copyfileobj(res.raw, temp)
print('Image sucessfully Downloaded')
print (temp.name)
image = Image.open(temp.name)
if ((image.size[0]>max_image_size) or (image.size[1]>max_image_size)):
origx = image.size[0]
origy = image.size[1]
if (origx>origy):
newx = int(max_image_size)
newy = int(origy * (max_image_size/origx))
else:
newy = int(max_image_size)
newx = int(origx * (max_image_size/origy))
image = image.resize((newx,newy))
print ("new image size",image.size)
image.save(temp, format="png")
mediaid = mastodonBot.media_post(temp.name, mime_type="image/jpeg")
medialist.append(mediaid)
else:
print('Image Couldn\'t be retrieved')
temp.close()
try:
postedToot = mastodonBot.status_post(tootText,None,medialist,False,feedvisibility)
lastpost = clean
except Exception as e:
print(e)
lastspottime = datetime.now().timestamp()
# print ("time:",now)
time.sleep(feeddelay)