Skip to content

Commit

Permalink
add 'tabs' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Feb 19, 2024
1 parent a3125c3 commit a9163f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fake_traffic -c tr -l ku-tr -ca h
fake_traffic -c ru -l ru-ru -ca s -lf
# use none-headless mode
fake_traffic -c en -l en-us -ca t -nh
# limit the number of tabs in the browser to 2
fake_traffic -c en -l en-us -ca t -t 2
```
---
### Simple usage
Expand All @@ -46,7 +48,8 @@ ft = FakeTraffic(country='US', language='en-US', category='h', headless=True)
category = сategory of interest of a user (defaults to 'h'):
'all' (all), 'b' (business), 'e' (entertainment),
'm' (health), 's' (sports), 't' (sci/tech), 'h' (top stories);
headless = True/False (defaults to True).
headless = True/False (defaults to True);
tabs = limit the number of tabs in browser (defaults to 5).
"""
ft.crawl()
```
Expand Down Expand Up @@ -92,6 +95,6 @@ Russia | English | `FakeTraffic(country="RU", language="en-US", category='b
Russia | Russian | `FakeTraffic(country="RU", language="ru-RU")` |
Brazil | Portuguese | `FakeTraffic(country="BR", language="pt-BR", category='s')` |
United Kingdom | English | `FakeTraffic(country="GB", language="en-GB")` |
United States | English | `FakeTraffic(country="US", language="en-US")` |
United States | English | `FakeTraffic(country="US", language="en-US", tabs=4)` |
United States | Hebrew Israel | `FakeTraffic(country="US", language="he-IL")` |

11 changes: 10 additions & 1 deletion fake_traffic/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
help="Run the browser in non-headless mode",
required=False,
)
parser.add_argument(
"-t",
"--tabs",
default=5,
type=int,
help="Limit the number of tabs in browser. Defaults to 5",
required=False,
)
parser.add_argument(
"-lf",
"--logfile",
Expand All @@ -61,7 +69,7 @@
language_split = args.language.split("-")
language = f"{language_split[0]}-{language_split[1].upper()}"
logging.info(
f"Run crawl with: {country=}, {language=}, category={args.category}, headless={args.headless}, logfile={args.logfile}"
f"Run crawl with: {country=}, {language=}, category={args.category}, headless={args.headless}, tabs={args.tabs}, logfile={args.logfile}"
)


Expand All @@ -70,5 +78,6 @@
language=language,
category=args.category,
headless=args.headless,
tabs=args.tabs,
)
fake_traffic.crawl()
11 changes: 7 additions & 4 deletions fake_traffic/fake_traffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from playwright_stealth import stealth_async

logger = logging.getLogger("__name__")
SEMAPHORE = asyncio.Semaphore(5)


# playwright install chromium
res = subprocess.run(
Expand All @@ -27,23 +25,27 @@ def __init__(
language="en-US",
category="h",
headless=True,
tabs=5,

):
"""Internet traffic generator. Utilizes real-time google search trends by specified parameters.
country = country code ISO 3166-1 Alpha-2 code (https://www.iso.org/obp/ui/),
language = country-language code ISO-639 and ISO-3166 (https://www.fincher.org/Utilities/CountryLanguageList.shtml),
category = category of interest of a user (defaults to 'h'):
'all' (all), 'b' (business), 'e' (entertainment),
'm' (health), 's' (sports), 't' (sci/tech), 'h' (top stories);
headless = True/False (defaults to True).
headless = True/False (defaults to True);
tabs = limit the number of tabs in browser (defaults to 5).
"""
self.country = country
self.language = language
self.category = category
self.headless = headless
self.browser = None
self.semaphore = asyncio.Semaphore(tabs)

async def abrowse(self, url):
async with SEMAPHORE:
async with self.semaphore:
page = await self.browser.new_page()
await stealth_async(page)
try:
Expand Down Expand Up @@ -128,5 +130,6 @@ def crawl(self):
language="en-US",
category="h",
headless=True,
tabs=5,
)
fake_traffic.crawl()

0 comments on commit a9163f6

Please sign in to comment.