Skip to content

Commit 9a76faf

Browse files
committed
微调代码
1 parent b839ff8 commit 9a76faf

File tree

8 files changed

+73
-45
lines changed

8 files changed

+73
-45
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ LINKV SDK for the Python2 programming language.
1212
git clone https://github.com/linkv-io/python2-sdk
1313
```
1414

15+
## Install SetupTools
16+
```sh
17+
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
18+
```
19+
1520
## Install
1621
```sh
22+
23+
1724
cd python2-sdk
1825
python setup.py build
1926
python setup.py install --record log
@@ -59,10 +66,10 @@ def main():
5966
return
6067
golds0 = r1['golds']
6168
print('golds0:%d' % golds0)
62-
unique_id = '1231234'
69+
unique_id = '123123123'
6370
gold = 10
64-
r2 = live.SuccessOrderByLiveOpenID(live_open_id, unique_id, linkv_sdk.OrderTypeAdd, gold, 10, 1,
65-
linkv_sdk.PlatformTypeH5, '')
71+
r2 = live.SuccessOrderByLiveOpenID(live_open_id, unique_id, linkv_sdk.OrderAdd, gold, 10, 1,
72+
linkv_sdk.PlatformH5, '')
6673
if not r2['status']:
6774
print('live.SuccessOrderByLiveOpenID(%s)' % r2['error'])
6875
return
@@ -73,8 +80,8 @@ def main():
7380
print('(golds0+gold) != golds1')
7481
return
7582

76-
unique_id1 = '456456'
77-
ok = live.ChangeGoldByLiveOpenID(live_open_id, unique_id1, linkv_sdk.OrderTypeDel, gold, 1, 'test del')
83+
unique_id1 = '456456456'
84+
ok = live.ChangeGoldByLiveOpenID(live_open_id, unique_id1, linkv_sdk.OrderDel, gold, 1, 'test del')
7885
if not ok:
7986
print('!ok')
8087
return
@@ -94,6 +101,7 @@ def main():
94101

95102
if __name__ == "__main__":
96103
main()
104+
97105
```
98106

99107
## License

example/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def main():
3131
print('golds0:%d' % golds0)
3232
unique_id = '123123123'
3333
gold = 10
34-
r2 = live.SuccessOrderByLiveOpenID(live_open_id, unique_id, linkv_sdk.OrderTypeAdd, gold, 10, 1,
35-
linkv_sdk.PlatformTypeH5, '')
34+
r2 = live.SuccessOrderByLiveOpenID(live_open_id, unique_id, linkv_sdk.OrderAdd, gold, 10, 1,
35+
linkv_sdk.PlatformH5, '')
3636
if not r2['status']:
3737
print('live.SuccessOrderByLiveOpenID(%s)' % r2['error'])
3838
return
@@ -44,7 +44,7 @@ def main():
4444
return
4545

4646
unique_id1 = '456456456'
47-
ok = live.ChangeGoldByLiveOpenID(live_open_id, unique_id1, linkv_sdk.OrderTypeDel, gold, 1, 'test del')
47+
ok = live.ChangeGoldByLiveOpenID(live_open_id, unique_id1, linkv_sdk.OrderDel, gold, 1, 'test del')
4848
if not ok:
4949
print('!ok')
5050
return

linkv_sdk/config/bindings/binding.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: UTF-8 -*-
22

3-
from ffi import dlopen_platform_specific, download
3+
from .ffi import dlopen_platform_specific, download
44
from ctypes import c_char_p, c_void_p, cast
55

66
VERSION = '0.0.4'
@@ -17,15 +17,15 @@ def decrypt(self, app_id, app_secret):
1717
self.core.decrypt.argtypes = [c_char_p, c_char_p]
1818
self.core.decrypt.restype = c_void_p
1919
ptr = self.core.decrypt(app_id.encode('utf-8'), app_secret.encode('utf-8'))
20-
app_config = cast(ptr, c_char_p).value
20+
app_config = cast(ptr, c_char_p).value.encode(encoding='utf8')
2121
self.core.release.argtypes = c_void_p,
2222
self.core.release.restype = None
2323
self.core.release(ptr)
2424
return app_config
2525

2626

2727
def download_library():
28-
return download(FILE, "", VERSION)
28+
return download(FILE, '', VERSION)
2929

3030

3131
_binding = None

linkv_sdk/config/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
from linkv_sdk.rtc.config import dict_config as dict_rtc_config
55
from linkv_sdk.live.config import dict_config as dict_live_config
66
from linkv_sdk.http.http import http
7-
from bindings.binding import download_library, binding
7+
from .bindings.binding import download_library, binding
8+
from linkv_sdk import __version__
89
from json import loads
910

1011

11-
def init(app_id, app_secret, version):
12+
def init(app_id, app_secret, version=__version__):
1213
http(version)
1314
if not download_library():
1415
return False

linkv_sdk/http/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ def post(self, uri='', params={}, headers={}):
3434

3535

3636
def http(version=''):
37-
return _http if _http is not None else _HTTP(version)
37+
global _http
38+
if _http:
39+
return _http
40+
_http = _HTTP(version)
41+
return _http

linkv_sdk/linkv_sdk.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
# -*- coding: UTF-8 -*-
22

3-
import config.config as config
4-
from . import __version__
5-
from im.im import LvIM
6-
from rtc.rtc import LvRTC
7-
from live.live import LvLIVE
3+
from .config.config import init
4+
from .im.im import LvIM
5+
from .rtc.rtc import LvRTC
6+
from .live.live import LvLIVE, OrderAdd, OrderDel, PlatformH5, PlatformANDROID, PlatformIOS
87

9-
OrderTypeAdd = 1
10-
OrderTypeDel = 2
11-
12-
PlatformTypeH5 = 'h5'
13-
PlatformTypeANDROID = 'android'
14-
PlatformTypeIOS = 'ios'
15-
16-
17-
def init(app_id, app_secret):
18-
return config.init(app_id, app_secret, __version__)

linkv_sdk/live/live.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
from linkv_sdk.http.http import http
1111

1212

13+
class OrderType(object):
14+
__slots__ = ['idx']
15+
16+
def __init__(self, idx):
17+
self.idx = idx
18+
19+
def string(self):
20+
return str(self.idx)
21+
22+
23+
class PlatformType(object):
24+
__slots__ = ['value']
25+
26+
def __init__(self, value):
27+
self.value = value
28+
29+
def string(self):
30+
return self.value
31+
32+
33+
OrderAdd = OrderType(1)
34+
OrderDel = OrderType(2)
35+
36+
PlatformH5 = PlatformType('h5')
37+
PlatformANDROID = PlatformType('android')
38+
PlatformIOS = PlatformType('ios')
39+
40+
1341
class Live(object):
1442
def __init__(self):
1543
pass
@@ -75,12 +103,12 @@ def SuccessOrderByLiveOpenID(live_open_id, unique_id, order_type, gold, money, e
75103
'app_id': config().app_key,
76104
'uid': live_open_id,
77105
'request_id': unique_id,
78-
'type': str(order_type),
106+
'type': order_type.string(),
79107
'value': str(gold),
80108
'money': str(money),
81109
'expriation': str(time.mktime(datetime.now().timetuple()) + expr * 86400),
82110
'channel': config().alias,
83-
'platform': platform_type,
111+
'platform': platform_type.string(),
84112
}
85113

86114
if len(order_id) > 0:
@@ -119,7 +147,7 @@ def ChangeGoldByLiveOpenID(live_open_id, unique_id, order_type, gold, expr, opti
119147
'app_id': config().app_key,
120148
'uid': live_open_id,
121149
'request_id': unique_id,
122-
'type': str(order_type),
150+
'type': order_type.string(),
123151
'value': str(gold),
124152
}
125153
if expr > 0:
@@ -142,7 +170,10 @@ def ChangeGoldByLiveOpenID(live_open_id, unique_id, order_type, gold, expr, opti
142170

143171
result = json.loads(response.read())
144172

145-
return int(result['status']) == 200
173+
return {
174+
'status': False,
175+
'ok': int(result['status']) == 200,
176+
}
146177

147178
@staticmethod
148179
def GetGoldByLiveOpenID(live_open_id):
@@ -199,22 +230,21 @@ def GetGoldByLiveOpenID(self, live_open_id):
199230

200231

201232
def genRandomString():
202-
container = ''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 8))
203-
container += str(int(time.mktime(datetime.now().timetuple())))
204-
container += ''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 8))
205-
return container
233+
return '{}{}{}'.format(
234+
''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 8)),
235+
str(int(time.mktime(datetime.now().timetuple()))),
236+
''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 8)))
206237

207238

208239
def genSign(params, md5_secret):
209240
data = __encode(params) + "&key=" + md5_secret
210241
obj = hashlib.new('md5')
211-
obj.update(data)
242+
obj.update(data.decode(encoding='utf8'))
212243
return obj.hexdigest().lower()
213244

214245

215246
def __encode(params):
216-
keys = params.keys()
217-
keys.sort()
247+
keys = sorted(params.keys())
218248
container = ''
219249
for k in keys:
220250
if len(container) > 0:

setup.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
tests_require = []
66
extras_require = {}
77
install_requires = [
8-
'requests'
8+
'requests',
99
]
1010

1111
setup(
@@ -37,8 +37,4 @@
3737
'License :: OSI Approved :: Apache-2.0 License',
3838
'Topic :: Utilities'
3939
],
40-
project_urls={
41-
'Documentation': 'https://doc.linkv.sg',
42-
'Source': 'https://github.com/linkv-io/python-sdk',
43-
},
4440
)

0 commit comments

Comments
 (0)