diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9809436..9d1aacd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,6 +20,7 @@ env: PYTHONPATH: ./ VK_USERNAME: ${{ secrets.VK_USERNAME }} VK_PASSWORD: ${{ secrets.VK_PASSWORD }} + VK_TOKEN: ${{ secrets.VK_TOKEN }} CAPTCHA_HANDLE_URL: ${{ secrets.CAPTCHA_HANDLE_URL }} jobs: diff --git a/tests/scraper_test.py b/tests/scraper_test.py index d758022..c12a261 100644 --- a/tests/scraper_test.py +++ b/tests/scraper_test.py @@ -17,7 +17,7 @@ vks = None def test_login_success(): global vks - vks = VkScraper(os.environ["VK_USERNAME"], os.environ["VK_PASSWORD"]) + vks = VkScraper(os.environ["VK_USERNAME"], os.environ["VK_PASSWORD"], os.environ.get("VK_TOKEN")) def test_scrape_empty_urll(): diff --git a/vk_url_scraper/scraper.py b/vk_url_scraper/scraper.py index b6e3651..ba6353b 100644 --- a/vk_url_scraper/scraper.py +++ b/vk_url_scraper/scraper.py @@ -38,10 +38,11 @@ class VkScraper: PHOTO_PATTERN = re.compile(r"(photo.{0,1}\d+_\d+)") VIDEO_PATTERN = re.compile(r"(video.{0,1}\d+_\d+)") - def __init__(self, username: str, password: str, captcha_handler=captcha_handler) -> None: + def __init__(self, username: str, password: str, token: str = None, captcha_handler=captcha_handler) -> None: """Initializes the scraper. - This function receives a username and password and performs authentication on vk.com to then call api endpoints + This function receives a username and password (or access token) and performs + authentication on vk.com to then call api endpoints. If token is passed, authentication will not be performed again. Parameters ---------- @@ -49,9 +50,12 @@ class VkScraper: Username on vk.com, can be a phone number or email password : str Matching password on vk.com + token : str + Access token received after authenticating, can be found in the vl_config.v2.json file """ - self.session = vk_api.VkApi(username, password, captcha_handler=captcha_handler) - self.session.auth(token_only=True) + self.session = vk_api.VkApi(username, password, token=token, captcha_handler=captcha_handler) + if token is None or len(token) == 0: + self.session.auth(token_only=True) def scrape(self, url: str) -> List: """Scrapes a URL for multiple possibilities of inner links such as wall, video, photo, ...