Added image retrieval & posting from RSS.
This commit is contained in:
parent
734d9450fb
commit
13b427942f
|
@ -23,6 +23,8 @@ import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import re
|
||||||
|
import urllib3
|
||||||
|
|
||||||
# 3rd party libraries imports
|
# 3rd party libraries imports
|
||||||
import feedparser
|
import feedparser
|
||||||
|
@ -61,6 +63,10 @@ class Main(object):
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
"""The main function."""
|
"""The main function."""
|
||||||
|
# regex to retrieve urls in <img> tags
|
||||||
|
regex = re.compile(r"<img[^>]+src=\"([^\">]+)\"")
|
||||||
|
http = urllib3.PoolManager()
|
||||||
|
|
||||||
clip = CliParse()
|
clip = CliParse()
|
||||||
clioptions = clip.options
|
clioptions = clip.options
|
||||||
self.setup_logging(clioptions)
|
self.setup_logging(clioptions)
|
||||||
|
@ -129,6 +135,16 @@ class Main(object):
|
||||||
'id': entry['link'],
|
'id': entry['link'],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# get images contained in the entry
|
||||||
|
images = []
|
||||||
|
if 'summary' in entry:
|
||||||
|
list_img_urls = regex.findall(entry['summary'])
|
||||||
|
if len(list_img_urls) > 0:
|
||||||
|
for img_url in list_img_urls:
|
||||||
|
resp = http.request('GET', img_url, preload_content=False)
|
||||||
|
images.append(resp)
|
||||||
|
resp.release_conn()
|
||||||
|
|
||||||
severalwordsinhashtag = False
|
severalwordsinhashtag = False
|
||||||
# lets see if the rss feed has hashtag
|
# lets see if the rss feed has hashtag
|
||||||
if 'tags' in entry:
|
if 'tags' in entry:
|
||||||
|
@ -186,7 +202,6 @@ class Main(object):
|
||||||
finaltweet = addtag.finaltweet
|
finaltweet = addtag.finaltweet
|
||||||
else:
|
else:
|
||||||
finaltweet = dedup.finaltweet
|
finaltweet = dedup.finaltweet
|
||||||
|
|
||||||
if clioptions.dryrun:
|
if clioptions.dryrun:
|
||||||
if entrytosend:
|
if entrytosend:
|
||||||
logging.warning('Would toot with visibility "{visibility}": {toot}'.format(
|
logging.warning('Would toot with visibility "{visibility}": {toot}'.format(
|
||||||
|
@ -204,7 +219,7 @@ class Main(object):
|
||||||
visibility=config.get(
|
visibility=config.get(
|
||||||
'mastodon', 'toot_visibility',
|
'mastodon', 'toot_visibility',
|
||||||
fallback='public')))
|
fallback='public')))
|
||||||
twp = TootPost(config, finaltweet)
|
twp = TootPost(config, finaltweet, images)
|
||||||
storeit = twp.storeit()
|
storeit = twp.storeit()
|
||||||
else:
|
else:
|
||||||
logging.debug('populating RSS entry {}'.format(rss['id']))
|
logging.debug('populating RSS entry {}'.format(rss['id']))
|
||||||
|
@ -219,7 +234,7 @@ class Main(object):
|
||||||
pluginmodulename = 'feed2toot.plugins.{pluginmodule}'.format(pluginmodule=pluginclassname.lower())
|
pluginmodulename = 'feed2toot.plugins.{pluginmodule}'.format(pluginmodule=pluginclassname.lower())
|
||||||
try:
|
try:
|
||||||
pluginmodule = importlib.import_module(pluginmodulename)
|
pluginmodule = importlib.import_module(pluginmodulename)
|
||||||
pluginclass = getattr(pluginmodule, pluginclassname)
|
pluginclass = getattr(pluginmodule, pluginclassname)
|
||||||
pluginclass(plugins[plugin], finaltweet)
|
pluginclass(plugins[plugin], finaltweet)
|
||||||
except ImportError as err:
|
except ImportError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
|
|
@ -30,11 +30,12 @@ from mastodon import Mastodon
|
||||||
class TootPost:
|
class TootPost:
|
||||||
'''TootPost class'''
|
'''TootPost class'''
|
||||||
|
|
||||||
def __init__(self, config, toot):
|
def __init__(self, config, toot, img_list):
|
||||||
'''Constructore of the TootPost class'''
|
'''Constructore of the TootPost class'''
|
||||||
self.config = config
|
self.config = config
|
||||||
self.store = True
|
self.store = True
|
||||||
self.toot = toot
|
self.toot = toot
|
||||||
|
self.img_list = img_list
|
||||||
self.main()
|
self.main()
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
|
@ -44,9 +45,16 @@ class TootPost:
|
||||||
access_token = self.config.get('mastodon', 'user_credentials'),
|
access_token = self.config.get('mastodon', 'user_credentials'),
|
||||||
api_base_url = self.config.get('mastodon', 'instance_url')
|
api_base_url = self.config.get('mastodon', 'instance_url')
|
||||||
)
|
)
|
||||||
|
medias_dicts = []
|
||||||
|
if len(self.img_list) > 0:
|
||||||
|
for img in self.img_list:
|
||||||
|
medias_dicts.append(mastodon.media_post(img.data,
|
||||||
|
img.headers['content-type']))
|
||||||
|
|
||||||
mastodon.status_post(self.toot,
|
mastodon.status_post(self.toot,
|
||||||
visibility=self.config.get(
|
visibility=self.config.get(
|
||||||
'mastodon', 'toot_visibility', fallback='public'))
|
'mastodon', 'toot_visibility', fallback='public'),
|
||||||
|
media_ids=medias_dicts)
|
||||||
|
|
||||||
def storeit(self):
|
def storeit(self):
|
||||||
'''Indicate if the tweet should be stored or not'''
|
'''Indicate if the tweet should be stored or not'''
|
||||||
|
|
Loading…
Reference in a new issue