FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
async-python/scrape.py at main · 15bubbles/async-python · GitHub
15bubbles
/
async-python
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
async-python
/
scrape.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (71 loc) · 2.49 KB
Breadcrumbs
async-python
/
scrape.py
Copy path
File metadata and controls
102 lines (71 loc) · 2.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import
asyncio
from
asyncio
.
queues
import
Queue
from
typing
import
(
AsyncGenerator
,
Awaitable
,
Callable
,
Iterable
,
Tuple
,
TypeVar
,
)
import
aiofiles
import
aiohttp
# CONSTANTS
MAX_WORKERS
=
50
BASE_URL
=
"https://jsonplaceholder.typicode.com/posts"
URLS
=
[
f"
{
BASE_URL
}
/
{
i
}
"
for
i
in
range
(
0
,
1000
)]
# MAIN REQUEST MAKING AND FILE WRITING LOGIC
async
def
get_url
(
session
:
aiohttp
.
ClientSession
,
url
:
str
)
->
str
:
response
=
await
session
.
request
(
"GET"
,
url
)
data
=
await
response
.
text
()
return
data
async
def
write_to_file
(
filename
:
str
,
data
:
str
)
->
None
:
async
with
aiofiles
.
open
(
filename
,
mode
=
"w"
)
as
file_handle
:
await
file_handle
.
write
(
data
)
async
def
request_and_write_results
(
session
:
aiohttp
.
ClientSession
,
url
:
str
,
filename
:
str
)
->
None
:
data
=
await
get_url
(
session
,
url
)
await
write_to_file
(
filename
,
data
)
# HELPER FUNCTIONS
_IterableItem
=
TypeVar
(
"_IterableItem"
)
async
def
async_enumerate
(
urls
:
Iterable
[
_IterableItem
],
)
->
AsyncGenerator
[
Tuple
[
int
,
_IterableItem
],
None
]:
for
idx
,
url
in
enumerate
(
urls
):
yield
idx
,
url
async
def
queued_worker_wrapper
(
coroutine_function
:
Callable
[[
aiohttp
.
ClientSession
,
str
,
str
],
Awaitable
],
session
:
aiohttp
.
ClientSession
,
queue
:
Queue
,
)
->
None
:
while
True
:
print
(
"Getting item from queue"
)
url
,
filename
=
await
queue
.
get
()
print
(
f"Got
{
url
}
,
{
filename
}
from queue"
)
print
(
f"Running coroutine for
{
url
}
,
{
filename
}
"
)
await
coroutine_function
(
session
,
url
,
filename
)
print
(
f"Coruoutine finished for
{
url
}
,
{
filename
}
"
)
print
(
"Letting queue know that the task is done"
)
queue
.
task_done
()
# MAIN FUNCTION
async
def
main
():
queue
=
Queue
(
MAX_WORKERS
)
async
with
aiohttp
.
ClientSession
()
as
session
:
workers
=
[
asyncio
.
create_task
(
queued_worker_wrapper
(
request_and_write_results
,
session
,
queue
)
)
for
_
in
range
(
MAX_WORKERS
)
]
async
for
idx
,
url
in
async_enumerate
(
URLS
):
filename
=
f"
{
idx
}
.txt"
queue_item
=
(
url
,
filename
)
await
queue
.
put
((
url
,
filename
))
print
(
f"Item
{
queue_item
}
added into the queue"
)
await
queue
.
join
()
print
(
"Queue joined"
)
for
worker
in
workers
:
worker
.
cancel
()
if
__name__
==
"__main__"
:
asyncio
.
run
(
main
())
Back
|
FazBrowse Home
|
New Git URL