-
Notifications
You must be signed in to change notification settings - Fork 6
/
rss2single.py
112 lines (102 loc) · 3.7 KB
/
rss2single.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
rss2single.py allows for selectively pushing RSS entries into Mastodon, one at a time.
AI6YR Benjamin Kuo - Dec 2023
"""
# 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']
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()
if(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[:465] + " " + link
spottime = dateutil.parser.parse(entry['published']).timestamp()
firsttwo = clean[:2]
firstthree = clean[:3]
# if (1):
print (clean)
value = input ("Toot this? Y/N/Q?")
if (value.lower() == "q"):
quit()
elif (value.lower() == "y"):
print ("Tooting")
isposted = False
print (clean)
tootText = clean + feedtags
tootText = tootText[:499]
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 = spottime
now = datetime.now().timestamp()
# print ("time:",now)
time.sleep(60)