Skip to content

Commit 9ef147a

Browse files
authored
Merge pull request #27 from hmlendea/monetised_links
Support for link monetisation
2 parents 1443ac2 + 8f8000e commit 9ef147a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

mastodon-rss-bot.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import datetime, timedelta
77
import base64
88
import hashlib
9+
import random
910

1011
import feedparser
1112
from mastodon import Mastodon
@@ -25,6 +26,7 @@
2526
include_link_thumbnail = True
2627
use_privacy_frontends = True
2728
use_shortlink = True
29+
use_monetised_links = True
2830
maximum_toots_count = 1
2931

3032
rss_feed_url = sys.argv[1]
@@ -65,12 +67,24 @@ def determine_content_language(text):
6567

6668
return language
6769

70+
def get_monetised_link(original_link):
71+
userid = '1092341'
72+
73+
if isinstance(original_link, bytes):
74+
buffer = original_link
75+
else:
76+
buffer = original_link.encode('latin-1' if isinstance(original_link, str) else 'utf-8')
77+
78+
encoded_link = base64.b64encode(buffer).decode('utf-8')
79+
monetised_link = f"https://link-to.net/{userid}/{random.random() * 1000}/dynamic?r={encoded_link}"
80+
81+
return monetised_link
82+
6883
if not os.path.isfile("app_" + mastodon_instance + '.secret'):
6984
if Mastodon.create_app(
7085
rss_feed_domain,
7186
api_base_url = 'https://' + mastodon_instance,
72-
to_file = "app_" + mastodon_instance + '.secret'
73-
):
87+
to_file = "app_" + mastodon_instance + '.secret'):
7488
print('Successfully created the application on instance ' + mastodon_instance)
7589
else:
7690
print('Failed to create the application on instance ' + mastodon_instance)
@@ -245,7 +259,7 @@ def determine_content_language(text):
245259
feed_entry_link = re.sub('\?utm.*$', '', feed_entry_link)
246260
feed_entry_link = re.sub('/$', '', feed_entry_link)
247261

248-
toot_body += '\n\n🔗 ' + feed_entry_link
262+
toot_body += '\n\n🔗 ' + get_monetised_link(feed_entry_link)
249263

250264
if include_author and 'authors' in feed_entry:
251265
toot_body += '\nby ' + feed_entry.authors[0].name

0 commit comments

Comments
 (0)