forked from ping/instagram_private_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
144 lines (120 loc) · 4.65 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# flake8: noqa
import unittest
import time
import codecs
try:
import unittest.mock as compat_mock
except ImportError:
import mock as compat_mock
import sys
import os
try:
from instagram_private_api import (
__version__, Client, ClientError, ClientLoginError,
ClientCookieExpiredError, ClientThrottledError, ClientCompatPatch,
ClientLoginRequiredError, MediaTypes,
ClientSentryBlockError, ClientCheckpointRequiredError,
ClientChallengeRequiredError)
from instagram_private_api.utils import (
InstagramID, gen_user_breadcrumb,
max_chunk_size_generator, max_chunk_count_generator, get_file_size,
ig_chunk_generator
) # noqa
from instagram_private_api.constants import Constants
from instagram_private_api.compat import compat_urllib_parse
except ImportError:
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from instagram_private_api import (
__version__, Client, ClientError, ClientLoginError,
ClientCookieExpiredError, ClientThrottledError, ClientCompatPatch,
ClientLoginRequiredError, MediaTypes,
ClientSentryBlockError, ClientCheckpointRequiredError,
ClientChallengeRequiredError)
from instagram_private_api.utils import (
InstagramID, gen_user_breadcrumb,
max_chunk_size_generator, max_chunk_count_generator, get_file_size,
ig_chunk_generator
) # noqa
from instagram_private_api.constants import Constants
from instagram_private_api.compat import compat_urllib_parse
try:
from instagram_web_api import (
__version__ as __webversion__,
Client as WebClient,
ClientError as WebClientError,
ClientLoginError as WebClientLoginError,
ClientCookieExpiredError as WebClientCookieExpiredError,
ClientCompatPatch as WebClientCompatPatch)
from instagram_web_api.compat import compat_urllib_error
except ImportError:
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from instagram_web_api import (
__version__ as __webversion__,
Client as WebClient,
ClientError as WebClientError,
ClientLoginError as WebClientLoginError,
ClientCookieExpiredError as WebClientCookieExpiredError,
ClientCompatPatch as WebClientCompatPatch)
from instagram_web_api.compat import compat_urllib_error
def to_json(python_object):
if isinstance(python_object, bytes):
return {'__class__': 'bytes',
'__value__': codecs.encode(python_object, 'base64').decode()}
raise TypeError(repr(python_object) + ' is not JSON serializable')
def from_json(json_object):
if '__class__' in json_object and json_object['__class__'] == 'bytes':
return codecs.decode(json_object['__value__'].encode(), 'base64')
return json_object
class ApiTestBase(unittest.TestCase):
"""Main base class for private api tests."""
def __init__(self, testname, api, user_id=None, media_id=None):
super(ApiTestBase, self).__init__(testname)
self.api = api
self.test_user_id = user_id
self.test_media_id = media_id
self.sleep_interval = 2.5
if testname.endswith('_mock'):
self.sleep_interval = 0 # sleep a bit between tests to avoid HTTP429 errors
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
def tearDown(self):
time.sleep(self.sleep_interval)
class WebApiTestBase(unittest.TestCase):
"""Main base class for web api tests."""
def __init__(self, testname, api):
super(WebApiTestBase, self).__init__(testname)
self.api = api
self.sleep_interval = 2.5
if testname.endswith('_mock'):
self.sleep_interval = 0 # sleep a bit between tests to avoid HTTP429 errors
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
self.test_user_id = '25025320'
self.test_user_name = 'instagram'
self.test_media_shortcode = 'BJL-gjsDyo1'
self.test_media_shortcode2 = 'BVRqQxmj2TA'
self.test_media_id = '1009392755603152985'
self.test_comment_id = '1234567890'
def tearDown(self):
time.sleep(self.sleep_interval)
class MockResponse(object):
"""A mock class to emulate api responses."""
def __init__(self, code=200, content_type='', body=''):
self.code = 200
self.content_type = content_type
self.body = body
def info(self):
return {'Content-Type': self.content_type}
def read(self):
return self.body.encode('utf8')