Skip to content

Commit 005ba5e

Browse files
committed
2
1 parent 844b230 commit 005ba5e

File tree

10 files changed

+225
-345
lines changed

10 files changed

+225
-345
lines changed

README-ja.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
1+
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
22

33

44

@@ -34,10 +34,6 @@
3434

3535
このライブラリは、無料で使用することができます。
3636

37-
### 同期と非同期の両方に対応
38-
39-
同期と非同期の両方に対応しています。
40-
4137

4238
## 機能
4339

@@ -67,6 +63,7 @@ pip install twikit
6763
**クライアントを定義し、アカウントにログインする。**
6864

6965
```python
66+
import asyncio
7067
from twikit import Client
7168

7269
USERNAME = 'example_user'
@@ -76,25 +73,28 @@ PASSWORD = 'password0000'
7673
# Initialize client
7774
client = Client('en-US')
7875

79-
# アカウントにログイン
80-
client.login(
81-
auth_info_1=USERNAME ,
82-
auth_info_2=EMAIL,
83-
password=PASSWORD
84-
)
76+
async def main():
77+
# アカウントにログイン
78+
client.login(
79+
auth_info_1=USERNAME ,
80+
auth_info_2=EMAIL,
81+
password=PASSWORD
82+
)
83+
84+
asyncio.run(main())
8585
```
8686

8787
**メディア付きツイートを作成する。**
8888

8989
```python
9090
# メディアをアップロードし、メディアIDを取得する。
9191
media_ids = [
92-
client.upload_media('media1.jpg'),
93-
client.upload_media('media2.jpg')
92+
await client.upload_media('media1.jpg'),
93+
await client.upload_media('media2.jpg')
9494
]
9595

9696
# ツイートを投稿する
97-
client.create_tweet(
97+
await client.create_tweet(
9898
text='Example Tweet',
9999
media_ids=media_ids
100100
)
@@ -103,7 +103,7 @@ client.create_tweet(
103103

104104
**ツイートを検索する**
105105
```python
106-
tweets = client.search_tweet('python', 'Latest')
106+
tweets = await client.search_tweet('python', 'Latest')
107107

108108
for tweet in tweets:
109109
print(
@@ -115,7 +115,7 @@ for tweet in tweets:
115115

116116
**ユーザーのツイートを取得する**
117117
```python
118-
tweets = client.get_user_tweets('123456', 'Tweet')
118+
tweets = await client.get_user_tweets('123456', 'Tweet')
119119

120120
for tweet in tweets:
121121
print(tweet.text)

README-zh.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
1+
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
22

33

44

@@ -36,10 +36,6 @@
3636

3737
本库无需付费。
3838

39-
### 同步/异步支持
40-
41-
Twikit 同时提供同步和异步的实现。
42-
4339

4440
## 功能
4541

@@ -69,6 +65,7 @@ pip install twikit
6965
**定义一个客户端并登录**
7066

7167
```python
68+
import asyncio
7269
from twikit import Client
7370

7471
USERNAME = 'example_user'
@@ -78,24 +75,27 @@ PASSWORD = 'password0000'
7875
# 初始化客户端
7976
client = Client('en-US')
8077

81-
client.login(
82-
auth_info_1=USERNAME ,
83-
auth_info_2=EMAIL,
84-
password=PASSWORD
85-
)
78+
async def main():
79+
await client.login(
80+
auth_info_1=USERNAME ,
81+
auth_info_2=EMAIL,
82+
password=PASSWORD
83+
)
84+
85+
asyncio.run(main())
8686
```
8787

8888
**创建一条附带媒体的推文**
8989

9090
```python
9191
# 上传媒体文件并获取媒体ID
9292
media_ids = [
93-
client.upload_media('media1.jpg'),
94-
client.upload_media('media2.jpg')
93+
await client.upload_media('media1.jpg'),
94+
await client.upload_media('media2.jpg')
9595
]
9696

9797
# 创建一条带有提供的文本和附加媒体的推文
98-
client.create_tweet(
98+
await client.create_tweet(
9999
text='Example Tweet',
100100
media_ids=media_ids
101101
)
@@ -104,7 +104,7 @@ client.create_tweet(
104104

105105
**搜索推文**
106106
```python
107-
tweets = client.search_tweet('python', 'Latest')
107+
tweets = await client.search_tweet('python', 'Latest')
108108

109109
for tweet in tweets:
110110
print(
@@ -116,7 +116,7 @@ for tweet in tweets:
116116

117117
**检索用户的推文**
118118
```python
119-
tweets = client.get_user_tweets('123456', 'Tweet')
119+
tweets = await client.get_user_tweets('123456', 'Tweet')
120120

121121
for tweet in tweets:
122122
print(tweet.text)

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
1+
<img src="https://i.imgur.com/iJe6rsZ.png" width="500">
22

33

44

@@ -43,10 +43,6 @@ This library uses scraping and does not require an API key.
4343

4444
This library is free to use.
4545

46-
### Both Synchronous and Asynchronous Support
47-
48-
Twikit supports both synchronous and asynchronous.
49-
5046

5147
## Functionality
5248

@@ -77,6 +73,7 @@ pip install twikit
7773
**Define a client and log in to the account.**
7874

7975
```python
76+
import asyncio
8077
from twikit import Client
8178

8279
USERNAME = 'example_user'
@@ -86,24 +83,27 @@ PASSWORD = 'password0000'
8683
# Initialize client
8784
client = Client('en-US')
8885

89-
client.login(
90-
auth_info_1=USERNAME ,
91-
auth_info_2=EMAIL,
92-
password=PASSWORD
93-
)
86+
async def main():
87+
await client.login(
88+
auth_info_1=USERNAME ,
89+
auth_info_2=EMAIL,
90+
password=PASSWORD
91+
)
92+
93+
asyncio.run(main())
9494
```
9595

9696
**Create a tweet with media attached.**
9797

9898
```python
9999
# Upload media files and obtain media_ids
100100
media_ids = [
101-
client.upload_media('media1.jpg'),
102-
client.upload_media('media2.jpg')
101+
await client.upload_media('media1.jpg'),
102+
await client.upload_media('media2.jpg')
103103
]
104104

105105
# Create a tweet with the provided text and attached media
106-
client.create_tweet(
106+
await client.create_tweet(
107107
text='Example Tweet',
108108
media_ids=media_ids
109109
)
@@ -112,7 +112,7 @@ client.create_tweet(
112112

113113
**Search the latest tweets based on a keyword**
114114
```python
115-
tweets = client.search_tweet('python', 'Latest')
115+
tweets = await client.search_tweet('python', 'Latest')
116116

117117
for tweet in tweets:
118118
print(
@@ -124,7 +124,7 @@ for tweet in tweets:
124124

125125
**Retrieve user tweets**
126126
```python
127-
tweets = client.get_user_tweets('123456', 'Tweet')
127+
tweets = await client.get_user_tweets('123456', 'Tweet')
128128

129129
for tweet in tweets:
130130
print(tweet.text)

examples/delete_all_tweets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import time
33

4-
from twikit.twikit_async import Client
4+
from twikit import Client
55

66
AUTH_INFO_1 = '...'
77
AUTH_INFO_2 = '...'

examples/dm_auto_reply.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import os
23

34
from twikit import Client
@@ -9,27 +10,32 @@
910

1011
client = Client()
1112

12-
if os.path.exists('cookies.json'):
13-
client.load_cookies('cookies.json')
14-
else:
15-
client.login(
16-
auth_info_1=AUTH_INFO_1,
17-
auth_info_2=AUTH_INFO_2,
18-
password=PASSWORD
19-
)
20-
client.save_cookies('cookies.json')
21-
22-
23-
user_id = '1752362966203469824' # User ID of the DM partner to stream.
24-
reply_message = 'Hello'
25-
26-
topics = {
27-
Topic.dm_update(f'{client.user_id()}-{user_id}')
28-
}
29-
streaming_session = client.get_streaming_session(topics)
30-
31-
for topic, payload in streaming_session:
32-
if payload.dm_update:
33-
if client.user_id() == payload.dm_update.user_id:
34-
continue
35-
client.send_dm(payload.dm_update.user_id, reply_message)
13+
14+
async def main():
15+
if os.path.exists('cookies.json'):
16+
client.load_cookies('cookies.json')
17+
else:
18+
await client.login(
19+
auth_info_1=AUTH_INFO_1,
20+
auth_info_2=AUTH_INFO_2,
21+
password=PASSWORD
22+
)
23+
client.save_cookies('cookies.json')
24+
25+
26+
user_id = '1752362966203469824' # User ID of the DM partner to stream.
27+
reply_message = 'Hello'
28+
29+
topics = {
30+
Topic.dm_update(f'{client.user_id()}-{user_id}')
31+
}
32+
streaming_session = await client.get_streaming_session(topics)
33+
34+
async for topic, payload in streaming_session:
35+
if payload.dm_update:
36+
if client.user_id() == payload.dm_update.user_id:
37+
continue
38+
await client.send_dm(payload.dm_update.user_id, reply_message)
39+
40+
asyncio.run(main())
41+

examples/download_tweet_media.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from twikit.twikit_async import Client
1+
import asyncio
2+
from twikit import Client
23

34
AUTH_INFO_1 = '...'
45
AUTH_INFO_2 = '...'
@@ -7,13 +8,16 @@
78
client = Client('en-US')
89

910

10-
tweet = client.get_tweet_by_id('...')
11+
async def main():
12+
tweet = await client.get_tweet_by_id('...')
1113

12-
for i, media in enumerate(tweet.media):
13-
media_url = media.get('media_url_https')
14-
extension = media_url.rsplit('.', 1)[-1]
14+
for i, media in enumerate(tweet.media):
15+
media_url = media.get('media_url_https')
16+
extension = media_url.rsplit('.', 1)[-1]
1517

16-
response = client.get_media(media_url)
18+
response = await client.get(media_url, headers=client._base_headers)
1719

18-
with open(f'media_{i}.{extension}', 'wb') as f:
19-
f.write(response.content)
20+
with open(f'media_{i}.{extension}', 'wb') as f:
21+
f.write(response.content)
22+
23+
asyncio.run(main())

0 commit comments

Comments
 (0)