Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import re | ||
from bootstrap import conf | ||
from urllib.parse import urlencode, urlsplit, urlunsplit, SplitResult | ||
from lib.integrations.abstract import AbstractIntegration | ||
|
||
|
||
class RssBridgeAbstractIntegration(AbstractIntegration): | ||
bridge_type = None | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.split = urlsplit(conf.PLUGINS_RSS_BRIDGE) \ | ||
if conf.PLUGINS_RSS_BRIDGE else None | ||
|
||
def get_u(self, feed): | ||
raise NotImplementedError() | ||
|
||
def match_feed_creation(self, feed): | ||
return bool(self.split and not feed.get('link') and self.get_u(feed)) | ||
|
||
def feed_creation(self, feed): | ||
query = {'action': 'display', 'format': 'AtomFormat', | ||
'bridge': self.bridge_type, 'u': self.get_u(feed)} | ||
|
||
feed['link'] = urlunsplit(SplitResult(scheme=self.split.scheme, | ||
netloc=self.split.netloc, | ||
path=self.split.path or '/', | ||
query=urlencode(query), fragment='')) | ||
return True | ||
|
||
|
||
class RegexRssBridgeAbstractIntegration(RssBridgeAbstractIntegration): | ||
regex = None | ||
|
||
def get_u(self, feed): | ||
split = self.regex.split(feed.get('site_link', ''), 1) | ||
if len(split) > 1: | ||
return split[2] | ||
return False | ||
|
||
|
||
class InstagramIntegration(RegexRssBridgeAbstractIntegration): | ||
regex = re.compile('^https?://(www.)?instagram.com/([^ \t\n\r\f\v/]+)') | ||
bridge_type = 'InstagramBridge' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
|
||
from bootstrap import conf | ||
from urllib.parse import urlsplit, parse_qs | ||
from lib.integrations.abstract import _INTEGRATION_MAPPING | ||
from lib.integrations.rss_bridge import InstagramIntegration | ||
from lib.integrations import dispatch | ||
|
||
|
||
class InstagramIntegrationTest(unittest.TestCase): | ||
site_links = ('http://instagram.com/jaesivsm', | ||
'http://instagram.com/jaesivsm/', | ||
'https://instagram.com/jaesivsm', | ||
'https://instagram.com/jaesivsm/', | ||
'http://www.instagram.com/jaesivsm/', | ||
'https://www.instagram.com/jaesivsm/') | ||
link = 'https://bridge.leslibres.org/?action=display' \ | ||
'&bridge=InstagramBridge&format=AtomFormat&u=jaesivsm' | ||
|
||
def setUp(self): | ||
conf.PLUGINS_RSS_BRIDGE = 'https://bridge.leslibres.org/' | ||
self.inte = InstagramIntegration() | ||
for inte, prio in _INTEGRATION_MAPPING: | ||
if isinstance(inte, InstagramIntegration): | ||
inte.split = urlsplit(conf.PLUGINS_RSS_BRIDGE) | ||
|
||
def test_match_feed_creation(self): | ||
self.assertFalse(self.inte.match_feed_creation({})) | ||
for sl in self.site_links: | ||
self.assertTrue(self.inte.match_feed_creation({'site_link': sl}), | ||
"%s did not match" % sl) | ||
|
||
def test_feed_creation(self): | ||
original = urlsplit(self.link) | ||
qs = parse_qs(original.query) | ||
for sl in self.site_links: | ||
feed = {'site_link': sl} | ||
self.assertTrue(dispatch('feed_creation', feed)) | ||
processed = urlsplit(feed['link']) | ||
self.assertEqual(processed.scheme, original.scheme) | ||
self.assertEqual(processed.netloc, original.netloc) | ||
self.assertEqual(processed.path, original.path) | ||
self.assertEqual(parse_qs(processed.query), qs) |