|
|
@@ -4,6 +4,9 @@ import os |
|
|
|
import sys |
|
|
|
|
|
|
|
import json |
|
|
|
import re |
|
|
|
import tempfile |
|
|
|
from urllib.parse import urlparse |
|
|
|
|
|
|
|
import requests |
|
|
|
from mastodon import Mastodon |
|
|
@@ -27,26 +30,50 @@ def make_masto(config): |
|
|
|
baseurl = 'https://%s' % config['hostname'] |
|
|
|
return Mastodon(access_token=config['token'], api_base_url=baseurl) |
|
|
|
|
|
|
|
def download_file(url, destdir): |
|
|
|
resp = requests.get(url) |
|
|
|
resp.raise_for_status() |
|
|
|
|
|
|
|
fname = None |
|
|
|
if 'content-disposition' in resp.headers: |
|
|
|
# Taken from: https://stackoverflow.com/questions/31804799/ |
|
|
|
d = resp.headers['content-disposition'] |
|
|
|
fname = re.findall("filename=(.+)", d)[0] |
|
|
|
else: |
|
|
|
u = urlparse(url) |
|
|
|
fname = u.path.rsplit('/', 1)[-1] |
|
|
|
|
|
|
|
destpath = os.path.join(destdir, fname) |
|
|
|
|
|
|
|
with open(destpath, 'wb') as f: |
|
|
|
f.write(resp.content) |
|
|
|
|
|
|
|
return destpath |
|
|
|
|
|
|
|
def make_post(masto, data): |
|
|
|
body = '%s\n\nby /u/%s at https://old.reddit.com/%s' % (data['title'], data['username'], data['rid']) |
|
|
|
with tempfile.TemporaryDirectory(prefix='mastoreddit.', dir='/tmp') as td: |
|
|
|
os.makedirs(td, exist_ok=True) |
|
|
|
|
|
|
|
#if len(data['attachments']) > 4: |
|
|
|
# data['attachments'] = data['attachments'][:4] |
|
|
|
if len(data['media']) > 4: |
|
|
|
data['media'] = data['media'][:4] |
|
|
|
|
|
|
|
image_resps = None |
|
|
|
#if data['attachments'] is not None: |
|
|
|
# for a in data['attachments']: |
|
|
|
# res = masto.media_post(a) |
|
|
|
# res.raise_for_status() |
|
|
|
# image_resps.append(res) |
|
|
|
#else: |
|
|
|
# image_resps = None |
|
|
|
image_resps = None |
|
|
|
if data['media'] is not None and len(data['media']) > 0: |
|
|
|
image_resps = [] |
|
|
|
for a in data['media']: |
|
|
|
dlpath = download_file(a, td) |
|
|
|
res = masto.media_post(dlpath) |
|
|
|
image_resps.append(res) |
|
|
|
|
|
|
|
masto.status_post(body, media_ids=image_resps) |
|
|
|
body = '%s\n\nby /u/%s at https://old.reddit.com/%s' % (data['title'], data['username'], data['rid']) |
|
|
|
masto.status_post(body, media_ids=image_resps) |
|
|
|
|
|
|
|
def extract_media_urls(data): |
|
|
|
|
|
|
|
if 'selftext' in data: |
|
|
|
return [] |
|
|
|
st = data['selftext'] |
|
|
|
if st is not None and len(st) > 0: |
|
|
|
return [] |
|
|
|
|
|
|
|
if 'post_hint' not in data: |
|
|
|
if 'gallery_data' in data: |
|
|
@@ -86,6 +113,23 @@ def extract_media_urls(data): |
|
|
|
|
|
|
|
raise ValueError('unknown hint type ' + hint) |
|
|
|
|
|
|
|
def process_post(post): |
|
|
|
d = post['data'] |
|
|
|
|
|
|
|
media = [] |
|
|
|
try: |
|
|
|
media = extract_media_urls(d) |
|
|
|
except Exception as e: |
|
|
|
print('error processing ' + d['id'] + ': ' + str(e)) |
|
|
|
|
|
|
|
return { |
|
|
|
'rid': d['id'], |
|
|
|
'title': d['title'], |
|
|
|
'username': d['author'], |
|
|
|
'score': d['ups'], |
|
|
|
'media': media |
|
|
|
} |
|
|
|
|
|
|
|
def query_tops(config): |
|
|
|
url = 'https://www.reddit.com/r/%s/top.json?sort=top&t=day' % config['subreddit'] |
|
|
|
resp = requests.get(url, headers = {'User-agent': 'mastoreddit'}) |
|
|
@@ -97,21 +141,7 @@ def query_tops(config): |
|
|
|
for post in j['data']['children']: |
|
|
|
if post['kind'] != 't3': |
|
|
|
print('found post of kind', post['kind']) |
|
|
|
d = post['data'] |
|
|
|
|
|
|
|
media = None |
|
|
|
try: |
|
|
|
media = extract_media_urls(d) |
|
|
|
except Exception as e: |
|
|
|
print('error processing ' + d['id'] + ': ' + str(e)) |
|
|
|
|
|
|
|
posts.append({ |
|
|
|
'rid': d['id'], |
|
|
|
'title': d['title'], |
|
|
|
'username': d['author'], |
|
|
|
'score': d['ups'], |
|
|
|
'media': media |
|
|
|
}) |
|
|
|
posts.append(process_post(post)) |
|
|
|
|
|
|
|
return posts |
|
|
|
|
|
|
@@ -124,8 +154,8 @@ def main(): |
|
|
|
masto = make_masto(config) |
|
|
|
# make_post(masto, {'title': sys.argv[1], 'username': 'nobody', 'rid': 'none', 'attachments': [sys.argv[2]]}) |
|
|
|
res = query_tops(config) |
|
|
|
for r in res[:5]: |
|
|
|
print('posting', r['rid'], '-', r['title']) |
|
|
|
for r in res: |
|
|
|
print('posting', r['rid'], '-', r['title'], '=====', r) |
|
|
|
make_post(masto, r) |
|
|
|
|
|
|
|
if __name__ == '__main__': |