Exemple #1
0
    def setUp(self):
        super(RedditTest, self).setUp()
        self.reddit = reddit.Reddit('token-here')
        self.api = self.reddit.reddit_api = self.mox.CreateMockAnything(
            praw.Reddit)

        self.submission_selftext = FakeSubmission(FakeRedditor())
        self.comment = FakeComment(self.submission_selftext, FakeRedditor())

        self.submission_link = FakeSubmission(FakeRedditor())
        self.submission_link.selftext = ''
        self.submission_link.url = 'https://reddit.com/ezv3f2'

        reddit.user_cache.clear()
Exemple #2
0
    def new(handler, auth_entity=None, **kwargs):
        """Creates and returns a :class:`Reddit` entity.

    Args:
      handler: the current :class:`webapp2.RequestHandler`
      auth_entity: :class:`oauth_dropins.reddit.RedditAuth`
      kwargs: property values
    """
        user = json_loads(auth_entity.user_json)
        gr_source = gr_reddit.Reddit(auth_entity.refresh_token)
        return Reddit(id=user.get('name'),
                      auth_entity=auth_entity.key,
                      url=gr_source.user_url(user.get('name')),
                      name=user.get('name'),
                      picture=user.get('icon_img'),
                      **kwargs)
Exemple #3
0
  def new(auth_entity=None, **kwargs):
    """Creates and returns a :class:`Reddit` entity.

    Args:
      auth_entity: :class:`oauth_dropins.reddit.RedditAuth`
      kwargs: property values
    """
    assert 'username' not in kwargs
    assert 'id' not in kwargs
    user = json_loads(auth_entity.user_json)
    gr_source = gr_reddit.Reddit(auth_entity.refresh_token)
    return Reddit(username=user.get('name'),
                  auth_entity=auth_entity.key,
                  url=gr_source.user_url(user.get('name')),
                  name=user.get('name'),
                  picture=user.get('icon_img'),
                  **kwargs)
Exemple #4
0
    def get(self):
        """Handles an API GET.

    Request path is of the form /site/user_id/group_id/app_id/activity_id ,
    where each element except site is an optional string object id.
    """
        # parse path
        args = urllib.parse.unquote(self.request.path).strip('/').split('/')
        if not args or len(args) > MAX_PATH_LEN:
            raise exc.HTTPNotFound('Expected 1-%d path elements; found %d' %
                                   (MAX_PATH_LEN, len(args)))

        if len(args) > 1 and args[1] == 'nederland20':
            return self.abort(
                401,
                'To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.'
            )

        # make source instance
        site = args.pop(0)
        if site == 'twitter':
            src = twitter.Twitter(access_token_key=util.get_required_param(
                self, 'access_token_key'),
                                  access_token_secret=util.get_required_param(
                                      self, 'access_token_secret'))
        elif site == 'facebook':
            self.abort(
                400,
                'Sorry, Facebook is no longer available in the REST API. Try the library instead!'
            )
        elif site == 'flickr':
            src = flickr.Flickr(access_token_key=util.get_required_param(
                self, 'access_token_key'),
                                access_token_secret=util.get_required_param(
                                    self, 'access_token_secret'))
        elif site == 'github':
            src = github.GitHub(
                access_token=util.get_required_param(self, 'access_token'))
        elif site == 'instagram':
            if self.request.get('interactive').lower() == 'true':
                src = instagram.Instagram(scrape=True)
            else:
                self.abort(
                    400,
                    'Sorry, Instagram is not currently available in the REST API. Try https://instagram-atom.appspot.com/ instead!'
                )
        elif site == 'mastodon':
            src = mastodon.Mastodon(
                instance=util.get_required_param(self, 'instance'),
                access_token=util.get_required_param(self, 'access_token'),
                user_id=util.get_required_param(self, 'user_id'))
        elif site == 'meetup':
            src = meetup.Meetup(access_token_key=util.get_required_param(
                self, 'access_token_key'),
                                access_token_secret=util.get_required_param(
                                    self, 'access_token_secret'))
        elif site == 'pixelfed':
            src = pixelfed.Pixelfed(
                instance=util.get_required_param(self, 'instance'),
                access_token=util.get_required_param(self, 'access_token'),
                user_id=util.get_required_param(self, 'user_id'))
        elif site == 'reddit':
            src = reddit.Reddit(refresh_token=util.get_required_param(
                self, 'refresh_token'
            ))  # the refresh_roken should be returned but is not appearing
        else:
            src_cls = source.sources.get(site)
            if not src_cls:
                raise exc.HTTPNotFound('Unknown site %r' % site)
            src = src_cls(**self.request.params)

        # decode tag URI ids
        for i, arg in enumerate(args):
            parsed = util.parse_tag_uri(arg)
            if parsed:
                domain, id = parsed
                if domain != src.DOMAIN:
                    raise exc.HTTPBadRequest(
                        'Expected domain %s in tag URI %s, found %s' %
                        (src.DOMAIN, arg, domain))
                args[i] = id

        # handle default path elements
        args = [
            None if a in defaults else a
            for a, defaults in zip(args, PATH_DEFAULTS)
        ]
        user_id = args[0] if args else None

        # get activities (etc)
        try:
            if len(args) >= 2 and args[1] == '@blocks':
                try:
                    response = {'items': src.get_blocklist()}
                except source.RateLimited as e:
                    if not e.partial:
                        self.abort(429, str(e))
                    response = {'items': e.partial}
            else:
                response = src.get_activities_response(*args,
                                                       **self.get_kwargs())
        except (NotImplementedError, ValueError) as e:
            self.abort(400, str(e))
            # other exceptions are handled by webutil.handlers.handle_exception(),
            # which uses interpret_http_exception(), etc.

        # fetch actor if necessary
        actor = response.get('actor')
        if not actor and self.request.get('format') == 'atom':
            # atom needs actor
            actor = src.get_actor(user_id) if src else {}

        self.write_response(response, actor=actor, url=src.BASE_URL)
Exemple #5
0
 def setUp(self):
     super(RedditTest, self).setUp()
     self.reddit = reddit.Reddit('token-here')