Skip to content

Commit 326ae7c

Browse files
authored
Merge pull request #164 from MerleLiuKun/feat-trends
Feat trends
2 parents d42185e + d3908ad commit 326ae7c

File tree

10 files changed

+86
-9
lines changed

10 files changed

+86
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ env.bak/
8888
venv.bak/
8989

9090
# mkdocs documentation
91-
/site
91+
docs/site
9292

9393
# mypy
9494
.mypy_cache/

README.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,18 @@ Now covers these features:
105105
- Blocks
106106
- Mutes
107107

108+
- Usage
109+
- Tweets
110+
111+
- Trends
112+
108113
- Spaces
109114
- Spaces lookup
110115
- Search Spaces
111116

112-
- Compliance
113-
- Batch compliance
117+
- Direct Messages
118+
- Direct Messages lookup
119+
- Manage Direct Messages
114120

115121
- Lists
116122
- List lookup
@@ -120,12 +126,8 @@ Now covers these features:
120126
- List follows
121127
- Pinned Lists
122128

123-
- Direct Messages
124-
- Direct Messages lookup
125-
- Manage Direct Messages
126-
127-
- Usage
128-
- Tweets
129+
- Compliance
130+
- Batch compliance
129131

130132
- Media Upload
131133
- Media Simple upload

docs/docs/usage/trends/trends.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
The Trends lookup endpoint allow developers to get the Trends for a location, specified using the where-on-earth id (WOEID).
2+
3+
**Note:** WOEID is a legacy identifier created by Yahoo and has been deprecated. X API uses the numeric value to identify town and country trend locations.
4+
Reference our legacy [blog post](https://blog.twitter.com/engineering/en_us/a/2010/woeids-in-twitters-trends.html), or [archived data](https://archive.org/details/geoplanet_data_7.10.0.zip0.)
5+
6+
### Get trends
7+
8+
Get the trends for a location
9+
10+
```python
11+
api.get_trends_by_woeid(woeid=1)
12+
# Response(data=[Trend(trend_name='#QuietOnSet', tweet_count=14060), Trend(trend_name='King Charles', tweet_count=42315), Trend(trend_name='Drake Bell', tweet_count=1234)])
13+
```

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ nav:
5656
- Spaces:
5757
- Spaces Lookup: usage/spaces/spaces-lookup.md
5858
- Search Spaces: usage/spaces/search.md
59+
- Trends:
60+
- Trends: usage/trends/trends.md
5961
- Lists:
6062
- List lookup: usage/lists/list-lookup.md
6163
- Manage Lists: usage/lists/manage-lists.md

pytwitter/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,26 @@ def unmute_user(self, user_id: str, target_user_id: str) -> dict:
20682068
data = self._parse_response(resp)
20692069
return data
20702070

2071+
def get_trends_by_woeid(
2072+
self, woeid: int, return_json: bool = False
2073+
) -> Union[dict, md.Response]:
2074+
"""
2075+
Get the trends for a location.
2076+
2077+
:param woeid: The where-on-earth ID (woeid) for a location.
2078+
:param return_json: Type for returned data. If you set True JSON data will be returned.
2079+
:return:
2080+
- data: data for trends
2081+
"""
2082+
2083+
return self._get(
2084+
url=f"{self.BASE_URL_V2}/trends/by/woeid/{woeid}",
2085+
params=None,
2086+
cls=md.Trend,
2087+
multi=True,
2088+
return_json=return_json,
2089+
)
2090+
20712091
def get_space(
20722092
self,
20732093
space_id: str,

pytwitter/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .space import * # noqa
99
from .list import * # noqa
1010
from .stream import * # noqa
11+
from .trend import * # noqa
1112
from .dm_event import * # noqa
1213
from .usage import * # noqa
1314
from .ext import * # noqa

pytwitter/models/ext.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
StreamRule,
2222
TwitterList,
2323
Usage,
24+
Trend,
2425
)
2526

2627

@@ -113,6 +114,7 @@ class Response:
113114
TwitterList,
114115
DirectMessageEvent,
115116
Usage,
117+
Trend,
116118
List[User],
117119
List[Tweet],
118120
List[Media],
@@ -124,6 +126,7 @@ class Response:
124126
List[Space],
125127
List[ComplianceJob],
126128
List[DirectMessageEvent],
129+
List[Trend],
127130
]
128131
]
129132
includes: Optional[Includes] = field(default=None, repr=False)

pytwitter/models/trend.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Trend Object
3+
Refer: https://developer.twitter.com/en/docs/twitter-api/trends/api-reference/get-trends-by-woeid
4+
"""
5+
6+
from dataclasses import dataclass, field
7+
8+
from .base import BaseModel
9+
10+
11+
@dataclass
12+
class Trend(BaseModel):
13+
"""
14+
A class representing Trend object
15+
"""
16+
17+
trend_name: str = field(default=None)
18+
tweet_count: int = field(default=None)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[{"trend_name":"#TEZOSTUESDAY","tweet_count":14869},{"trend_name":"Copenhagen","tweet_count":13005},{"trend_name":"Roses","tweet_count":32193},{"trend_name":"Heroes","tweet_count":69798},{"trend_name":"Cedric","tweet_count":14259},{"trend_name":"#AskSonic","tweet_count":8908},{"trend_name":"Nelson","tweet_count":29841},{"trend_name":"#PSVARS","tweet_count":4915},{"trend_name":"Eddie","tweet_count":34139},{"trend_name":"Saliba","tweet_count":7191},{"trend_name":"Walters","tweet_count":8095},{"trend_name":"Bakayoko","tweet_count":1809},{"trend_name":"Bibby Stockholm","tweet_count":21021},{"trend_name":"Nwaneri","tweet_count":5783},{"trend_name":"Doncaster","tweet_count":3551},{"trend_name":"Kiwior","tweet_count":1535}]}

tests/apis/test_trends.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
tests for the trends api
3+
"""
4+
5+
import responses
6+
7+
8+
@responses.activate
9+
def test_get_trends_by_woeid(api, helpers):
10+
responses.add(
11+
responses.GET,
12+
url="https://api.twitter.com/2/trends/by/woeid/1",
13+
json=helpers.load_json_data("testdata/apis/trends/trends_resp.json"),
14+
)
15+
16+
resp = api.get_trends_by_woeid(1)
17+
assert resp.data[0].trend_name == "#TEZOSTUESDAY"

0 commit comments

Comments
 (0)