我正在使用mastodon python api,我正在尝试从输出字典中访问用户名。但是它无法访问

时间:2018-05-20 13:01:59

标签: python

我使用以下mastodon python api获取用户的所有信息,我想从中访问用户名。我在下面解释我的输入和输出。我使用mastodon.account ['username']来访问用户名,但它给了我错误。

from mastodon import Mastodon
Mastodon.create_app(
     'pytooterapp',
     api_base_url = 'https://mastodon.social',
     to_file = 'pytooter_clientcred.secret'
)
mastodon = Mastodon(
    client_id = 'pytooter_clientcred.secret',
    api_base_url = 'https://mastodon.social'
)
mastodon.log_in(
    'xyz@gmail.com',
    'xyz',
    to_file = 'pytooter_usercred.secret'
) 
mastodon = Mastodon(
    client_id = 'pytooter_clientcred.secret',
    access_token = 'pytooter_usercred.secret',
    api_base_url = 'https://mastodon.social'
)

当我在python shell

中触发代码时,我得到以下输出

输出:

{u'account': {u'acct': u'xyz',
  u'avatar': u'https://mastodon.social/avatars/original/missing.png',
  u'avatar_static': u'https://mastodon.social/avatars/original/missing.png',
  u'bot': False,
  u'created_at': datetime.datetime(2018, 1, 31, 17, 14, 53, 985000, tzinfo=tzutc()),
  u'display_name': u'',
  u'emojis': [],
  u'fields': [],
  u'followers_count': 1,
  u'following_count': 3,
  u'header': u'https://mastodon.social/headers/original/missing.png',
  u'header_static': u'https://mastodon.social/headers/original/missing.png',
  u'id': 2871,
  u'locked': False,
  u'note': u'<p></p>',
  u'statuses_count': 6,
  u'url': u'https://mastodon.social/@xyz',
  u'username': u'xyz'},
 u'application': {u'name': u'pytooterapp', u'website': None},
 u'content': u'<p>Tooting from python using <a href="https://mastodon.social/tags/mastodonpy" class="mention hashtag" rel="tag">#<span>mastodonpy</span></a> !</p>',
 u'created_at': datetime.datetime(2018, 5, 20, 12, 32, 56, 757000, tzinfo=tzutc()),
 u'emojis': [],
 u'favourited': False,
 u'favourites_count': 0,
 u'id': 100061647780173,
 u'in_reply_to_account_id': None,
 u'in_reply_to_id': None,
 u'language': u'en',
 u'media_attachments': [],
 u'mentions': [],
 u'muted': False,
 u'pinned': False,
 u'reblog': None,
 u'reblogged': False,
 u'reblogs_count': 0,
 u'sensitive': False,
 u'spoiler_text': u'',
 u'tags': [{u'name': u'mastodonpy',
   u'url': u'https://mastodon.social/tags/mastodonpy'}],
 u'uri': u'https://mastodon.social/users/xyz/statuses/1000616477685173',
 u'url': u'https://mastodon.social/@xyz/100061640685173',
 u'visibility': u'public'}

Now I want to access 'username' from above dictionary but I am not able to access. When I am trying to access username using "mastodon.account['username']" it gives me following error
"TypeError: 'instancemethod' object has no attribute '__getitem__'" 

1 个答案:

答案 0 :(得分:0)

您需要使用account_verify_credentials()

例如

- (UIImage*) rotatedImage:(UIImage*)image withAngle:(CGFloat)angle
{
    float radians = degreesToRadians(angle);

    CGAffineTransform xfrm = CGAffineTransformMakeRotation(radians);
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGRect rotatedImageBoundingRect = CGRectApplyAffineTransform (imageRect, xfrm);


    UIGraphicsBeginImageContext(rotatedImageBoundingRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM (ctx, rotatedImageBoundingRect.size.width/2., rotatedImageBoundingRect.size.height/2.);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextRotateCTM (ctx, radians);
    CGContextDrawImage (ctx, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), image.CGImage);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

会给你

from mastodon import Mastodon

#   Set up Mastodon
mastodon = Mastodon(
    access_token = 'usercred.secret',
    api_base_url = 'https://botsin.space/'
)

mastodon.account_verify_credentials()

因此,要获取用户名,请致电

{'id': 58380, 'username': 'openbenches', 'acct': 'openbenches', 'display_name': 'OpenBenches.org', ...