Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_update_instagram_picture_profile_404s(self):
self.setup_instagram(batch_size=1)
auth_entity = indieauth.IndieAuth(id='http://foo.com/', user_json='{}')
source = Instagram.new(
None, auth_entity=auth_entity, features=['listen'],
actor={'username': 'x', 'image': {'url': 'http://old/pic'}})
source.put()
super(HandlerTest, self).expect_requests_get(
gr_instagram.HTML_BASE_URL + 'x/', status_code=404, allow_redirects=False)
self.mox.ReplayAll()
resp = tasks.application.get_response('/cron/update_instagram_pictures')
self.assertEqual(200, resp.status_int)
self.assertEqual('http://old/pic', source.key.get().picture)
def test_instagram_scrape_with_cookie(self):
self.expect_requests_get(
instagram.HTML_BASE_URL, test_instagram.HTML_FEED_COMPLETE,
allow_redirects=False, headers={'Cookie': 'sessionid=c00k1e'})
self.mox.ReplayAll()
resp = app.application.get_response(
'/instagram/@me/@friends/@app/?cookie=c00k1e&interactive=true')
self.assertEqual(200, resp.status_int, resp.body)
self.assertEqual('application/json; charset=utf-8',
resp.headers['Content-Type'])
self.assert_equals(test_instagram.HTML_ACTIVITIES_FULL,
json_loads(resp.body)['items'])
def expect_instagram_fetch(self, body=gr_test_instagram.HTML_PROFILE_COMPLETE,
**kwargs):
return TestCase.expect_requests_get(
self, gr_instagram.HTML_BASE_URL + 'snarfed/', body,
allow_redirects=False, **kwargs)
def test_update_instagram_picture_profile_404s(self):
self.setup_instagram(batch_size=1)
auth_entity = indieauth.IndieAuth(id='http://foo.com/', user_json='{}')
source = Instagram.new(
None, auth_entity=auth_entity, features=['listen'],
actor={'username': 'x', 'image': {'url': 'http://old/pic'}})
source.put()
super(HandlerTest, self).expect_requests_get(
gr_instagram.HTML_BASE_URL + 'x/', status_code=404, allow_redirects=False)
self.mox.ReplayAll()
resp = cron.application.get_response('/cron/update_instagram_pictures')
self.assertEqual(200, resp.status_int)
self.assertEquals('http://old/pic', source.key.get().picture)
def expect_instagram_profile_fetch(self, username):
profile = copy.deepcopy(test_instagram.HTML_PROFILE)
profile['entry_data']['ProfilePage'][0]['graphql']['user'].update({
'username': username,
'profile_pic_url': 'http://new/pic',
})
super(HandlerTest, self).expect_requests_get(
gr_instagram.HTML_BASE_URL + '%s/' % username,
test_instagram.HTML_HEADER + json_dumps(profile) + test_instagram.HTML_FOOTER,
allow_redirects=False)
def expect_instagram_profile_fetch(self, username):
profile = copy.deepcopy(test_instagram.HTML_PROFILE)
profile['entry_data']['ProfilePage'][0]['graphql']['user'].update({
'username': username,
'profile_pic_url': 'http://new/pic',
})
super(HandlerTest, self).expect_requests_get(
gr_instagram.HTML_BASE_URL + '%s/' % username,
test_instagram.HTML_HEADER + json_dumps(profile) + test_instagram.HTML_FOOTER,
allow_redirects=False)
def get(self):
cookie = 'sessionid=%s' % urllib.parse.quote(
util.get_required_param(self, 'sessionid').encode('utf-8'))
logging.info('Fetching with Cookie: %s', cookie)
host_url = self.request.host_url + '/'
ig = instagram.Instagram()
try:
resp = ig.get_activities_response(group_id=source.FRIENDS, scrape=True,
cookie=cookie)
except Exception as e:
status, text = util.interpret_http_exception(e)
if status in ('403',):
self.response.headers['Content-Type'] = 'application/atom+xml'
self.response.out.write(atom.activities_to_atom([{
'object': {
'url': self.request.url,
'content': 'Your instagram-atom cookie isn\'t working. <a href="%s">Click here to regenerate your feed!</a>' % host_url,
},
}], {}, title='instagram-atom', host_url=host_url,
request_url=self.request.path_url))
return
elif status == '401':
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'))
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)
handler: the current :class:`webapp2.RequestHandler`
auth_entity: :class:`oauth_dropins.instagram.InstagramAuth`
"""
user = json_loads(auth_entity.user_json)
user['actor'] = actor
auth_entity.user_json = json_dumps(user)
auth_entity.put()
username = actor['username']
if not kwargs.get('features'):
kwargs['features'] = ['listen']
return Instagram(id=username,
auth_entity=auth_entity.key,
name=actor.get('displayName'),
picture=actor.get('image', {}).get('url'),
url=gr_instagram.Instagram.user_url(username),
**kwargs)
def get(self):
host_url = self.request.host_url + "/"
ig = instagram.Instagram(access_token=util.get_required_param(self, 'access_token'))
activities = ig.get_activities(count=50)
actor = ig.get_actor()
title = 'instagram-atom feed for %s' % ig.actor_name(actor)
# switchover warning
activities.append({
'verb': 'post',
'published': '2016-01-19T00:00:00',
'id': 'tag:instagram-atom.appspot.com,2016:0',
'url': 'https://instagram-atom.appspot.com/',
'title': 'ATTENTION: Please update your instagram-atom feed!',
'actor': {
'displayName': 'Ryan Barrett',
'id': 'https://snarfed.org/',
'url': 'https://snarfed.org/',
},