Skip to content

Commit

Permalink
add fake user agent for request
Browse files Browse the repository at this point in the history
  • Loading branch information
leVirve committed Nov 27, 2018
1 parent daf127c commit b150540
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"

before_install:
- pip install pytest-cov
- pip install coveralls

install:
- pip install -r requirements.txt
- python setup.py install

script:
- py.test --cov=dcard tests/
Expand Down
2 changes: 1 addition & 1 deletion dcard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dcard.posts import Post # noqa


__version__ = '0.3.0'
__version__ = '0.3.1'
__author__ = 'Salas leVirve'


Expand Down
10 changes: 7 additions & 3 deletions dcard/prequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

try:
from requests import Session
from fake_useragent import UserAgent
except ImportError:
raise RuntimeError('requests is required for prequests')
raise RuntimeError('`requests` and `fake_useragent` are required.')


__all__ = (
'map', 'imap', 'imap_unordered',
'get', 'options', 'head', 'post', 'put', 'patch', 'delete', 'request'
)

ua = UserAgent()


class AsyncRequest(object):
""" Asynchronous request.
Expand All @@ -44,6 +47,7 @@ def __init__(self, method, url, **kwargs):
self.session = kwargs.pop('session', None)
if self.session is None:
self.session = Session()
self.session.headers['User-Agent'] = ua.random

callback = kwargs.pop('callback', None)
if callback:
Expand All @@ -56,8 +60,8 @@ def __init__(self, method, url, **kwargs):

def send(self, **kwargs):
"""
Prepares request based on parameter passed to constructor and optional ``kwargs```.
Then sends request and saves response to :attr:`response`
Prepares request based on parameter passed to constructor and optional
``kwargs```. Then sends request and saves response to :attr:`response`.
:returns: ``Response``
"""
Expand Down
1 change: 1 addition & 0 deletions dcard/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, workers=8):
status_forcelist=[500, 502, 503, 504])
session = requests.Session()
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers['User-Agent'] = prequests.ua.random
self.session = session
self.pool = Pool(workers)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
six
requests
fake_useragent
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ def version():
},
install_requires=[
'six',
'requests'
],
dependency_links=[
'https://github.com/leVirve/prequests/tarball/master#egg=prequests-0.1.1'
'requests',
'fake_useragent'
],
classifiers=[
'Development Status :: 3 - Alpha',
Expand All @@ -54,6 +52,7 @@ def version():
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
]
)
20 changes: 15 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import requests
from dcard.api import route

try:
from fake_useragent import UserAgent
USER_AGENT = UserAgent().random
except ImportError:
USER_AGENT = (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) '
'Gecko/20100101 Firefox/64.0'
)


_post_id = 230004779
headers = {'User-Agent': USER_AGENT}


def test_valid_api_forums():
url = route.forums()
assert requests.get(url).ok
assert requests.get(url, headers=headers).ok


def test_valid_api_forum():
url = route.posts_meta(forum='funny')
assert requests.get(url).ok
assert requests.get(url, headers=headers).ok


def test_valid_api_article():
url = route.post(post_id=_post_id)
assert requests.get(url).ok
assert requests.get(url, headers=headers).ok


def test_valid_api_article_links():
url = route.post(post_id=_post_id, addition='links')
assert requests.get(url).ok
assert requests.get(url, headers=headers).ok


def test_valid_api_article_comments():
url = route.post(post_id=_post_id, addition='comments')
assert requests.get(url).ok
assert requests.get(url, headers=headers).ok
2 changes: 1 addition & 1 deletion tests/test_forums.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_extract_general_forums(self, dcard):

def test_get_metas_with_sort_param(self, dcard):
forum = dcard.forums(_forum_name)
assert len(forum.get_metas(sort='popular')) == 30
assert len(forum.get_metas(sort='popular')) <= 30
assert len(forum.get_metas(sort='new')) == 30

def test_get_metas_with_num_param(self, dcard):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_parse_resourses_in_content(self, dcard):
def test_parse_resourses_in_content_and_comments(self, dcard):
posts = dcard.posts(_post_id).get(links=False)
resources = posts.parse_resources()
assert len(resources) > 0
assert len(resources) >= 0

def test_download_resourses(self, dcard):
posts = dcard.posts(_post_id).get(comments=False, links=False)
Expand Down

0 comments on commit b150540

Please sign in to comment.