Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added proxy usage #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,46 @@ result = app(
)
```

### If you want to use proxy
### Example of creating BrightDataProxy object and generating session for each request
```python
from google_play_scraper.utils.proxies import Proxy
import random
from dotenv import load_dotenv
import os

class BrightDataProxy(Proxy):
def __init__(self, username: str, password: str, host: str, port: int):
self.username = username
self.password = password
self.host = host
self.port = port

def get_proxy(self) -> dict:
return {
"https": f"http://{self.username}:{self.password}@{self.host}:{self.port}"
}

session_id = ''.join(random.choice('0123456789abcdef') for _ in range(6))

# Creating proxy session string
session = os.getenv("SESSION")
proxy_session = f"{session}-{session_id}"

host=os.getenv("PROXY_HOST")
password = os.getenv("PROXY_PASSWORD")
port=os.getenv("PROXY_PORT")

proxy = BrightDataProxy(username=proxy_session, password=password, host=host, port=port)

result = app(
'com.nianticlabs.pokemongo',
lang='en', # defaults to 'en'
country='us', # defaults to 'us'
proxy=proxy
)
```

Result of `print(result)`:

```python
Expand Down
9 changes: 5 additions & 4 deletions google_play_scraper/features/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import json
from typing import Any, Dict
from typing import Any, Dict, Optional

from google_play_scraper.constants.element import ElementSpecs
from google_play_scraper.constants.regex import Regex
from google_play_scraper.constants.request import Formats
from google_play_scraper.exceptions import NotFoundError
from google_play_scraper.utils.request import get
from google_play_scraper.utils.proxies import Proxy


def app(app_id: str, lang: str = "en", country: str = "us") -> Dict[str, Any]:
def app(app_id: str, lang: str = "en", country: str = "us", proxy: Optional[Proxy] = None) -> Dict[str, Any]:
url = Formats.Detail.build(app_id=app_id, lang=lang, country=country)

try:
dom = get(url)
dom = get(url, proxy)
except NotFoundError:
url = Formats.Detail.fallback_build(app_id=app_id, lang=lang)
dom = get(url)
dom = get(url, proxy)
return parse_dom(dom=dom, app_id=app_id, url=url)


Expand Down
19 changes: 19 additions & 0 deletions google_play_scraper/utils/proxies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from abc import ABC, abstractmethod

class Proxy(ABC):
def __init__(self, username: str, password: str, host: str, port: int):
self.username = username
self.password = password
self.host = host
self.port = port

@abstractmethod
def get_proxy(self) -> dict:
"""
Abstract method to get the proxy information.

Returns:
- proxy (dict): Dictionary containing proxy information.
"""
pass

17 changes: 10 additions & 7 deletions google_play_scraper/utils/request.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
from typing import Union
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from urllib.request import Request
from typing import Optional

from google_play_scraper.exceptions import ExtraHTTPError, NotFoundError
from google_play_scraper.utils.proxies import Proxy

import requests

def _urlopen(obj):
def _urlopen(obj, proxy: Optional[Proxy] = None):
try:
resp = urlopen(obj)
resp = requests.get(obj, proxies=proxy.get_proxy()).text
except HTTPError as e:
if e.code == 404:
raise NotFoundError("App not found(404).")
else:
raise ExtraHTTPError(
"App not found. Status code {} returned.".format(e.code)
)

return resp.read().decode("UTF-8")
print(resp)
return resp


def post(url: str, data: Union[str, bytes], headers: dict) -> str:
return _urlopen(Request(url, data=data, headers=headers))


def get(url: str) -> str:
return _urlopen(url)
def get(url: str, proxy: Optional[Proxy] = None) -> str:
return _urlopen(url, proxy)