Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import copy
import socket
from oauth_dropins.webutil import testutil
from oauth_dropins.webutil.util import json_dumps, json_loads
from granary import instagram
from granary import source
from granary.tests import test_facebook
from granary.tests import test_instagram
from granary.tests import test_twitter
import api, app
class FakeSource(source.Source):
NAME = 'Fake'
DOMAIN = 'fa.ke'
BASE_URL = 'http://fa.ke/'
def __init__(self, **kwargs):
pass
class HandlerTest(testutil.HandlerTest):
activities = [{'foo': '☕ bar'}]
def setUp(self):
super(HandlerTest, self).setUp()
self.mox.StubOutWithMock(FakeSource, 'get_activities_response')
api.Handler.get.cache_clear()
elif type == 'tag': # add label
if not (len(path) == 4 and path[2] in ('issues', 'pull')):
return source.creation_result(
abort=True, error_plain='GitHub tag post requires tag-of issue or PR URL.')
tags = set(util.trim_nulls(t.get('displayName', '').strip()
for t in util.get_list(obj, 'object')))
if not tags:
return source.creation_result(
abort=True, error_plain='No tags found in tag post!')
existing_labels = self.existing_labels(owner, repo)
labels = sorted(tags & existing_labels)
issue_link = '<a href="%s">%s/%s#%s</a>' % (base_url, owner, repo, number)
if not labels:
return source.creation_result(
abort=True,
error_html="No tags in [%s] matched %s's existing labels [%s]." %
(', '.join(sorted(tags)), issue_link, ', '.join(sorted(existing_labels))))
if preview:
return source.creation_result(
description='add label%s <span class="verb">%s</span> to %s.' % (
('s' if len(labels) > 1 else ''), ', '.join(labels), issue_link))
else:
resp = self.rest(REST_API_ISSUE_LABELS % (owner, repo, number), labels)
return source.creation_result({
'url': base_url,
'type': 'tag',
'tags': labels,
})
_prepare_activity(a, reader=reader)
updated = (util.get_first(activities[0], 'object', default={}).get('published', '')
if activities else '')
if actor is None:
actor = {}
return jinja_env.get_template(FEED_TEMPLATE).render(
actor=Defaulter(actor),
host_url=host_url,
items=[Defaulter(a) for a in activities],
mimetypes=mimetypes,
rels=rels or {},
request_url=request_url,
title=title or 'User feed for ' + source.Source.actor_name(actor),
updated=updated,
VERBS_WITH_OBJECT=source.VERBS_WITH_OBJECT,
xml_base=xml_base,
)
def _activity_or_object(activity):
"""Returns the base item we care about, activity or activity['object'].
Used in :func:`activity_to_json()` and :func:`activities_to_html()`.
"""
if activity.get('object') and activity.get('verb') not in source.VERBS_WITH_OBJECT:
return activity['object']
return activity
'WOW': '😮',
'HAHA': '😆',
'SAD': '😢',
'ANGRY': '😡',
'THANKFUL': '🌼', # https://github.com/snarfed/bridgy/issues/748
'PRIDE': '🏳️🌈',
# nothing for LIKE (it's a like :P) or for NONE
}
FacebookId = collections.namedtuple('FacebookId', ['user', 'post', 'comment'])
# alias allows unit tests to mock the function
now_fn = datetime.now
class Facebook(source.Source):
"""Facebook source class. See file docstring and Source class for details.
Attributes:
access_token: string, optional, OAuth access token
user_id: string, optional, current user's id (either global or app-scoped)
scrape: boolean, whether to scrape m.facebook.com's HTML (True) or use
the API (False)
cookie_c_user: string, optional c_user cookie to use when scraping
cookie_xs: string, optional xs cookie to use when scraping
"""
DOMAIN = 'facebook.com'
BASE_URL = 'https://www.facebook.com/'
NAME = 'Facebook'
FRONT_PAGE_TEMPLATE = 'templates/facebook_index.html'
POST_ID_RE = re.compile('^[0-9_:]+$') # see parse_id() for gory details
def preview_create(self, obj, include_link=source.OMIT_LINK,
ignore_formatting=False):
"""Preview creation of a photo, comment, or favorite.
Args:
obj: ActivityStreams object
include_link: string
ignore_formatting: boolean
Returns:
a CreationResult whose description will be an HTML summary of
what publishing will do, and whose content will be an HTML preview
of the result (or None)
"""
return self._create(obj, preview=True, include_link=include_link,
ignore_formatting=ignore_formatting)
def delete(self, id):
"""Deletes a toot. The authenticated user must have authored it.
Args:
id: int or string, toot id (on local instance) to delete
Returns: CreationResult, content is dict with url and id fields
"""
self._delete(API_STATUS % id)
return source.creation_result({'url': self.status_url(id)})