Skip to content

Commit eb4ba64

Browse files
committed
add 'tabs' parameter
1 parent a3125c3 commit eb4ba64

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ fake_traffic -c tr -l ku-tr -ca h
2626
fake_traffic -c ru -l ru-ru -ca s -lf
2727
# use none-headless mode
2828
fake_traffic -c en -l en-us -ca t -nh
29+
# limit the number of tabs in the browser to 2
30+
fake_traffic -c en -l en-us -ca t -t 2
2931
```
3032
---
3133
### Simple usage
@@ -46,7 +48,8 @@ ft = FakeTraffic(country='US', language='en-US', category='h', headless=True)
4648
category = сategory of interest of a user (defaults to 'h'):
4749
'all' (all), 'b' (business), 'e' (entertainment),
4850
'm' (health), 's' (sports), 't' (sci/tech), 'h' (top stories);
49-
headless = True/False (defaults to True).
51+
headless = True/False (defaults to True);
52+
tabs = limit the number of tabs in browser (defaults to 3).
5053
"""
5154
ft.crawl()
5255
```
@@ -92,6 +95,6 @@ Russia | English | `FakeTraffic(country="RU", language="en-US", category='b
9295
Russia | Russian | `FakeTraffic(country="RU", language="ru-RU")` |
9396
Brazil | Portuguese | `FakeTraffic(country="BR", language="pt-BR", category='s')` |
9497
United Kingdom | English | `FakeTraffic(country="GB", language="en-GB")` |
95-
United States | English | `FakeTraffic(country="US", language="en-US")` |
98+
United States | English | `FakeTraffic(country="US", language="en-US", tabs=4)` |
9699
United States | Hebrew Israel | `FakeTraffic(country="US", language="he-IL")` |
97100

fake_traffic/cli.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
help="Run the browser in non-headless mode",
3838
required=False,
3939
)
40+
parser.add_argument(
41+
"-t",
42+
"--tabs",
43+
default=3,
44+
type=int,
45+
help="Limit the number of tabs in browser. Defaults to 3",
46+
required=False,
47+
)
4048
parser.add_argument(
4149
"-lf",
4250
"--logfile",
@@ -61,7 +69,7 @@
6169
language_split = args.language.split("-")
6270
language = f"{language_split[0]}-{language_split[1].upper()}"
6371
logging.info(
64-
f"Run crawl with: {country=}, {language=}, category={args.category}, headless={args.headless}, logfile={args.logfile}"
72+
f"Run crawl with: {country=}, {language=}, category={args.category}, headless={args.headless}, tabs={args.tabs}, logfile={args.logfile}"
6573
)
6674

6775

@@ -70,5 +78,6 @@
7078
language=language,
7179
category=args.category,
7280
headless=args.headless,
81+
tabs=args.tabs,
7382
)
7483
fake_traffic.crawl()

fake_traffic/fake_traffic.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from playwright_stealth import stealth_async
77

88
logger = logging.getLogger("__name__")
9-
SEMAPHORE = asyncio.Semaphore(5)
10-
119

1210
# playwright install chromium
1311
res = subprocess.run(
@@ -27,23 +25,27 @@ def __init__(
2725
language="en-US",
2826
category="h",
2927
headless=True,
28+
tabs=3,
29+
3030
):
3131
"""Internet traffic generator. Utilizes real-time google search trends by specified parameters.
3232
country = country code ISO 3166-1 Alpha-2 code (https://www.iso.org/obp/ui/),
3333
language = country-language code ISO-639 and ISO-3166 (https://www.fincher.org/Utilities/CountryLanguageList.shtml),
3434
category = category of interest of a user (defaults to 'h'):
3535
'all' (all), 'b' (business), 'e' (entertainment),
3636
'm' (health), 's' (sports), 't' (sci/tech), 'h' (top stories);
37-
headless = True/False (defaults to True).
37+
headless = True/False (defaults to True);
38+
tabs = limit the number of tabs in browser (defaults to 3).
3839
"""
3940
self.country = country
4041
self.language = language
4142
self.category = category
4243
self.headless = headless
4344
self.browser = None
45+
self.semaphore = asyncio.Semaphore(tabs)
4446

4547
async def abrowse(self, url):
46-
async with SEMAPHORE:
48+
async with self.semaphore:
4749
page = await self.browser.new_page()
4850
await stealth_async(page)
4951
try:
@@ -128,5 +130,6 @@ def crawl(self):
128130
language="en-US",
129131
category="h",
130132
headless=True,
133+
tabs=5,
131134
)
132135
fake_traffic.crawl()

0 commit comments

Comments
 (0)