add address lookup for pinned tweet

This commit is contained in:
Serhii 2023-02-09 12:23:13 +02:00
parent 709c031f28
commit 624036d00c
3 changed files with 25 additions and 7 deletions

View file

@ -44,10 +44,15 @@ class AddressResolver {
if (text.startsWith('@') && !text.substring(1).contains('@')) {
final formattedName = text.substring(1);
final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
final address = extractAddressByType(
final addressFromBio = extractAddressByType(
raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
if (address != null) {
return ParsedAddress.fetchTwitterAddress(address: address, name: text);
final addressFromPinnedTweet = extractAddressByType(
raw: twitterUser.pinnedTweet ?? '', type: CryptoCurrency.fromString(ticker));
if (addressFromBio != null) {
return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
}
if (addressFromPinnedTweet != null) {
return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
}
}
if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {

View file

@ -10,7 +10,7 @@ class TwitterApi {
static const userPath = '/2/users/by/username/';
static Future<TwitterUser> lookupUserByName({required String userName}) async {
final queryParams = {'user.fields': 'description'};
final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'};
final headers = {'authorization': 'Bearer $twitterBearerToken'};
@ -32,6 +32,16 @@ class TwitterApi {
throw Exception(responseJSON['errors'][0]['detail']);
}
return TwitterUser.fromJson(responseJSON['data'] as Map<String, dynamic>);
final user = responseJSON['data'] as Map<String, dynamic>;
try {
if (responseJSON['includes'] != null) {
user['pinnedTweet'] = responseJSON['includes']['tweets'][0]['text'];
}
} catch (e) {
print('responseJSON[includes][tweets][0][text] $e');
}
return TwitterUser.fromJson(user);
}
}

View file

@ -1,16 +1,19 @@
class TwitterUser {
TwitterUser({required this.id, required this.username, required this.name, this.description});
TwitterUser({required this.id, required this.username, required this.name, this.description,
this.pinnedTweet});
final String id;
final String username;
final String name;
final String? description;
final String? pinnedTweet;
factory TwitterUser.fromJson(Map<String, dynamic> json) {
return TwitterUser(
id: json['id'] as String,
username: json['username'] as String,
name: json['name'] as String,
description: json['description'] as String?);
description: json['description'] as String?,
pinnedTweet: json['pinnedTweet'] as String?);
}
}