Tweet.place to Place dataclass

This commit is contained in:
NoeCampos22
2021-04-19 15:13:33 -05:00
parent dbf2a2f689
commit 8f1c470061

View File

@@ -42,11 +42,7 @@ class Tweet(snscrape.base.Item):
quotedTweet: typing.Optional['Tweet'] = None
mentionedUsers: typing.Optional[typing.List['User']] = None
coordinates: typing.Optional['Coordinates'] = None
place: typing.Optional[str] = None
place_name: typing.Optional[str] = None
place_type: typing.Optional[str] = None
country: typing.Optional[str] = None
country_code: typing.Optional[str] = None
place: typing.Optional['Place'] = None
username = snscrape.base._DeprecatedProperty('username', lambda self: self.user.username, 'user.username')
outlinksss = snscrape.base._DeprecatedProperty('outlinksss', lambda self: ' '.join(self.outlinks), 'outlinks')
@@ -103,6 +99,15 @@ class Coordinates:
latitude: float
@dataclasses.dataclass
class Place:
fullName: str
name: str
type: str
country: str
countryCode: str
@dataclasses.dataclass
class User(snscrape.base.Entity):
# Most fields can be None if they're not known.
@@ -382,12 +387,7 @@ class TwitterAPIScraper(snscrape.base.Scraper):
if (coords := tweet['geo']['coordinates']) and len(coords) == 2:
kwargs['coordinates'] = Coordinates(coords[1], coords[0])
if tweet.get('place'):
# get place data
kwargs['place'] = tweet['place']['full_name']
kwargs['place_name'] = tweet['place']['name']
kwargs['place_type'] = tweet['place']['place_type']
kwargs['country'] = tweet['place']['country']
kwargs['country_code'] = tweet['place']['country_code']
kwargs['place'] = Place(tweet['place']['full_name'], tweet['place']['name'], tweet['place']['place_type'], tweet['place']['country'], tweet['place']['country_code'])
if 'coordinates' not in kwargs and tweet['place']['bounding_box'] and (coords := tweet['place']['bounding_box']['coordinates']) and coords[0] and len(coords[0][0]) == 2:
# Take the first (longitude, latitude) couple of the "place square"
kwargs['coordinates'] = Coordinates(coords[0][0][0], coords[0][0][1])