DangQu 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: DangQu
3
+ Version: 0.1.0
4
+ Summary: dangqu sdk
5
+ Author: DMJ-11740
6
+ Author-email: rpa@lumi.com
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.7
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: requests
18
+ Requires-Dist: oauthlib
19
+ Requires-Dist: requests-oauthlib
20
+ Requires-Dist: pillow
21
+ Requires-Dist: numpy
@@ -0,0 +1,10 @@
1
+ setup.py
2
+ DangQu.egg-info/PKG-INFO
3
+ DangQu.egg-info/SOURCES.txt
4
+ DangQu.egg-info/dependency_links.txt
5
+ DangQu.egg-info/requires.txt
6
+ DangQu.egg-info/top_level.txt
7
+ dangqu_sdk/OCR.py
8
+ dangqu_sdk/__init__.py
9
+ dangqu_sdk/client.py
10
+ dangqu_sdk/slider.py
@@ -0,0 +1,5 @@
1
+ requests
2
+ oauthlib
3
+ requests-oauthlib
4
+ pillow
5
+ numpy
@@ -0,0 +1 @@
1
+ dangqu_sdk
dangqu-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.1
2
+ Name: DangQu
3
+ Version: 0.1.0
4
+ Summary: dangqu sdk
5
+ Author: DMJ-11740
6
+ Author-email: rpa@lumi.com
7
+ Classifier: Development Status :: 3 - Alpha
8
+ Classifier: Intended Audience :: Developers
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.7
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: requests
18
+ Requires-Dist: oauthlib
19
+ Requires-Dist: requests-oauthlib
20
+ Requires-Dist: pillow
21
+ Requires-Dist: numpy
@@ -0,0 +1,124 @@
1
+ #! /usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ # @Time : 2024/9/26 11:28
4
+ # @Author : dmj-11740
5
+ # @File : OCR.py
6
+ # @Software: PyCharm
7
+ # @desc :
8
+ import base64
9
+ import json
10
+ from io import BytesIO
11
+
12
+ import requests
13
+ from PIL import Image
14
+
15
+
16
+ class OCRConfig:
17
+ api = "http://192.168.125.115:3344"
18
+
19
+
20
+ class OCR:
21
+
22
+ @staticmethod
23
+ def ocr_file(file_path):
24
+ """
25
+ ocr识别
26
+ :param file_path: 文件路径
27
+ :return:
28
+ """
29
+ api_url = f"{OCRConfig.api}/ocr/file/json"
30
+ file = open(file_path, "rb").read()
31
+ resp = requests.post(api_url, files={"image": file})
32
+ return resp.json()
33
+
34
+ @staticmethod
35
+ def ocr_base64(base64_str):
36
+ """
37
+ ocr识别
38
+ :param base64_str: 图片base64
39
+ :return:
40
+ """
41
+ api_url = f"{OCRConfig.api}/ocr/b64/json"
42
+ resp = requests.post(api_url, data=base64_str)
43
+ return resp.json()
44
+
45
+ @staticmethod
46
+ def det_file(file_path):
47
+ """
48
+ 目标识别
49
+ :param file_path: 文件路径
50
+ :return:
51
+ """
52
+ api_url = f"{OCRConfig.api}/det/file/json"
53
+ file = open(file_path, "rb").read()
54
+ resp = requests.post(api_url, files={"image": file})
55
+ return resp.json()
56
+
57
+ @staticmethod
58
+ def det_base64(base64_str):
59
+ """
60
+ 目标识别
61
+ :param base64_str: 图片base64
62
+ :return:
63
+ """
64
+ api_url = f"{OCRConfig.api}/det/b64/json"
65
+ resp = requests.post(api_url, data=base64_str)
66
+ return resp.json()
67
+
68
+ @staticmethod
69
+ def slide_file(target_file, bg_file, algorithm=1):
70
+ """
71
+ 滑块识别
72
+ :param target_file: 目标图片路径
73
+ :param bg_file: 背景图片路径
74
+ :param algorithm: 算法,1为匹配:滑块图和坑位图匹配,2为比较:坑位图和背景图比较
75
+ :return:
76
+ """
77
+ method = "match"
78
+ if algorithm != 1:
79
+ method = "compare"
80
+ target_file = open(target_file, "rb").read()
81
+ target_file_bytes = OCR.content_to_bytes(target_file)
82
+ bg_file = open(bg_file, "rb").read()
83
+ bg_file_bytes = OCR.content_to_bytes(bg_file)
84
+ api_url = f"{OCRConfig.api}/slide/{method}/file/json"
85
+ resp = requests.post(
86
+ api_url, files={"target_img": target_file_bytes, "bg_img": bg_file_bytes}
87
+ )
88
+ return resp.json()
89
+
90
+ @staticmethod
91
+ def slide_base64(target_b64str, bg_b64str, algorithm=1):
92
+ """
93
+ 滑块识别
94
+ :param target_b64str: 目标图片路径
95
+ :param bg_b64str: 背景图片路径
96
+ :param algorithm: 算法,1为匹配:滑块图和坑位图匹配,2为比较:坑位图和背景图比较
97
+ :return:
98
+ """
99
+ method = "match"
100
+ if algorithm != 1:
101
+ method = "compare"
102
+ target_b64str = OCR.content_to_bytes(target_b64str)
103
+ target_b64str = base64.b64encode(target_b64str).decode()
104
+ bg_b64str = OCR.content_to_bytes(bg_b64str)
105
+ bg_b64str = base64.b64encode(bg_b64str).decode()
106
+ json_str = json.dumps({"target_img": target_b64str, "bg_img": bg_b64str})
107
+ api_url = f"{OCRConfig.api}/slide/{method}/b64/json"
108
+ resp = requests.post(api_url, data=base64.b64encode(json_str.encode()).decode())
109
+ return resp.json()
110
+
111
+ @staticmethod
112
+ def content_to_bytes(image_data):
113
+ if isinstance(image_data, bytes):
114
+ image = Image.open(BytesIO(image_data))
115
+ else:
116
+ image_data = base64.b64decode(image_data)
117
+ image = Image.open(BytesIO(image_data))
118
+ # 8位图片转换为32位图片,否则ddddocr无法处理
119
+ if image.mode == "P":
120
+ image = image.convert("RGBA")
121
+ img_byte_arr = BytesIO()
122
+ image.save(img_byte_arr, format="PNG")
123
+
124
+ return img_byte_arr.getvalue()
@@ -0,0 +1,5 @@
1
+ from .client import DangquSdk
2
+ from .OCR import OCR, OCRConfig
3
+ from .slider import get_slide_track, generate_trajectory, SliderTrajectory
4
+
5
+ __all__ = ['DangquSdk', "OCR", 'OCRConfig', 'SliderTrajectory', 'generate_trajectory', 'get_slide_track']
@@ -0,0 +1,64 @@
1
+ import json
2
+ import datetime
3
+ import functools
4
+
5
+ import requests
6
+ from oauthlib.oauth2 import BackendApplicationClient
7
+ from requests_oauthlib import OAuth2Session
8
+
9
+
10
+ class TimedCache:
11
+ def __init__(self, duration):
12
+ self.cache = {}
13
+ self.duration = datetime.timedelta(seconds=duration)
14
+
15
+ def __call__(self, func):
16
+ @functools.wraps(func)
17
+ def wrapped(*args):
18
+ now = datetime.datetime.now()
19
+ if args in self.cache and now - self.cache[args][1] < self.duration:
20
+ return self.cache[args][0]
21
+ else:
22
+ result = func(*args)
23
+ self.cache[args] = (result, now)
24
+ return result
25
+
26
+ return wrapped
27
+
28
+
29
+ class DangquSdk:
30
+
31
+ def __init__(self, domain, token_url, client_id, client_secret):
32
+ self.doamin = domain
33
+ self.logical_url = f'{domain}/openapis/api/Workflow/Workflow/WorkflowInstance'
34
+
35
+ self.client_id = client_id
36
+ self.client_secret = client_secret
37
+ self.token_url = token_url
38
+
39
+ self.client = BackendApplicationClient(client_id=client_id)
40
+ self.oauth = OAuth2Session(client=self.client)
41
+
42
+ @property
43
+ @TimedCache(60 * 60 * 2)
44
+ def token(self):
45
+ token = self.oauth.fetch_token(
46
+ token_url=self.token_url,
47
+ client_id=self.client_id,
48
+ client_secret=self.client_secret
49
+ )
50
+ return token
51
+
52
+ def execute_dangqu_request(self, host_tenant: str, unified_id: str, request_body: dict):
53
+ body = {
54
+ "isSync" : True,
55
+ "isReturnResult" : True,
56
+ "hostTenant" : host_tenant,
57
+ "unifiedId" : unified_id,
58
+ "inputParameterJson": json.dumps(request_body)
59
+ }
60
+
61
+ _headers = {'Authorization': 'Bearer %s' % self.token.get('access_token', '')}
62
+ response = requests.post(self.logical_url, headers=_headers, json=body)
63
+
64
+ return response
@@ -0,0 +1,257 @@
1
+ #! /usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ # @Time : 2024/10/14 9:59
4
+ # @Author : dmj-11740
5
+ # @File : slider.py
6
+ # @Software: PyCharm
7
+ # @desc : 滑块生成
8
+
9
+ import numpy as np
10
+ import math
11
+ import random
12
+
13
+
14
+ def __ease_out_expo(sep):
15
+ if sep == 1:
16
+ return 1
17
+ else:
18
+ return 1 - pow(2, -10 * sep)
19
+
20
+
21
+ def get_slide_track(distance):
22
+ """
23
+ V1:根据滑动距离生成滑动轨迹
24
+ :param distance: 需要滑动的距离
25
+ :return: 滑动轨迹<type 'list'>: [[x,y,t], ...]
26
+ x: 已滑动的横向距离
27
+ y: 已滑动的纵向距离, 除起点外, 均为0
28
+ t: 滑动过程消耗的时间, 单位: 毫秒
29
+ """
30
+
31
+ if not isinstance(distance, int) or distance < 0:
32
+ raise ValueError(
33
+ f"distance类型必须是大于等于0的整数: distance: {distance}, type: {type(distance)}"
34
+ )
35
+ # 初始化轨迹列表
36
+ slide_track = [
37
+ [random.randint(-50, -10), random.randint(-50, -10), 0],
38
+ [0, 0, 0],
39
+ ]
40
+ # 共记录count次滑块位置信息
41
+ count = 30 + int(distance / 2)
42
+ # 初始化滑动时间
43
+ t = random.randint(50, 100)
44
+ # 记录上一次滑动的距离
45
+ _x = 0
46
+ _y = 0
47
+ for i in range(count):
48
+ # 已滑动的横向距离
49
+ x = round(__ease_out_expo(i / count) * distance)
50
+ # 滑动过程消耗的时间
51
+ t += random.randint(10, 20)
52
+ if x == _x:
53
+ continue
54
+ slide_track.append([x, _y, t])
55
+ _x = x
56
+ slide_track.append(slide_track[-1])
57
+ return slide_track
58
+
59
+
60
+ def generate_trajectory(distance, shake: bool = True, shake_space: int = 5) -> list:
61
+ """
62
+ 根据滑动距离生成自动化操作所用的动作链
63
+ :param distance: 滑动距离
64
+ :param shake: 滑动距离是否左右抖动
65
+ :param shake_space: 抖动范围
66
+ :return:滑动轨迹<type 'list'>: [[x,y,t], ...]
67
+ x: 本次滑动距离
68
+ y: 本次上下抖动距离
69
+ t: 滑动过程消耗的时间, 单位: 秒
70
+ """
71
+ if shake:
72
+ distance += random.randint(-shake_space, shake_space)
73
+ slide_track = get_slide_track(distance)
74
+ trajectory = []
75
+ last_x = 0
76
+ last_time = 0
77
+ for x, y, time in slide_track:
78
+ this_time = time - last_time
79
+ this_x = x - last_x
80
+ trajectory.append([this_x, y, this_time])
81
+ last_time = time
82
+ last_x = x
83
+ # 将轨迹列表中的时间转换为毫秒
84
+ trajectory = [[x, y, time / 1000] for x, y, time in trajectory if x > 0]
85
+ return trajectory
86
+
87
+
88
+ class SliderTrajectory:
89
+ """
90
+ 贝赛尔曲线轨迹生成
91
+ """
92
+
93
+ def _bztsg(self, dataTrajectory):
94
+ lengthOfdata = len(dataTrajectory)
95
+
96
+ def staer(x):
97
+ t = ((x - dataTrajectory[0][0]) / (dataTrajectory[-1][0] - dataTrajectory[0][0]))
98
+ y = np.array([0, 0], dtype=np.float64)
99
+ for s in range(len(dataTrajectory)):
100
+ y += dataTrajectory[s] * ((math.factorial(lengthOfdata - 1) / (
101
+ math.factorial(s) * math.factorial(lengthOfdata - 1 - s))) * math.pow(t, s) * math.pow(
102
+ (1 - t), lengthOfdata - 1 - s))
103
+ return y[1]
104
+
105
+ return staer
106
+
107
+ def _type(self, type, x, numberList):
108
+ numberListre = []
109
+ pin = (x[1] - x[0]) / numberList
110
+ if type == 0:
111
+ for i in range(numberList):
112
+ numberListre.append(i * pin)
113
+ if pin >= 0:
114
+ numberListre = numberListre[::-1]
115
+ elif type == 1:
116
+ for i in range(numberList):
117
+ numberListre.append(1 * ((i * pin) ** 2))
118
+ numberListre = numberListre[::-1]
119
+ elif type == 2:
120
+ for i in range(numberList):
121
+ numberListre.append(1 * ((i * pin - x[1]) ** 2))
122
+
123
+ elif type == 3:
124
+ dataTrajectory = [np.array([0, 0]), np.array([(x[1] - x[0]) * 0.8, (x[1] - x[0]) * 0.6]),
125
+ np.array([x[1] - x[0], 0])]
126
+ fun = self._bztsg(dataTrajectory)
127
+ numberListre = [0]
128
+ for i in range(1, numberList):
129
+ numberListre.append(fun(i * pin) + numberListre[-1])
130
+ if pin >= 0:
131
+ numberListre = numberListre[::-1]
132
+ numberListre = np.abs(np.array(numberListre) - max(numberListre))
133
+ biaoNumberList = ((numberListre - numberListre[numberListre.argmin()]) / (
134
+ numberListre[numberListre.argmax()] - numberListre[numberListre.argmin()])) * (x[1] - x[0]) + x[0]
135
+ biaoNumberList[0] = x[0]
136
+ biaoNumberList[-1] = x[1]
137
+ return biaoNumberList
138
+
139
+ def _getFun(self, s):
140
+ '''
141
+
142
+ :param s: 传入P点
143
+ :return: 返回公式
144
+ '''
145
+ dataTrajectory = []
146
+ for i in s:
147
+ dataTrajectory.append(np.array(i))
148
+ return self._bztsg(dataTrajectory)
149
+
150
+ def _simulation(self, start, end, le=1, deviation=0, bias=0.5):
151
+ '''
152
+
153
+ :param start:开始点的坐标 如 start = [0, 0]
154
+ :param end:结束点的坐标 如 end = [100, 100]
155
+ :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
156
+ :param deviation:轨迹上下波动的范围 如 deviation = 10
157
+ :param bias:波动范围的分布位置 如 bias = 0.5
158
+ :return:返回一个字典equation对应该曲线的方程,P对应贝塞尔曲线的影响点
159
+ '''
160
+ start = np.array(start)
161
+ end = np.array(end)
162
+ cbb = []
163
+ if le != 1:
164
+ e = (1 - bias) / (le - 1)
165
+ cbb = [[bias + e * i, bias + e * (i + 1)] for i in range(le - 1)]
166
+
167
+ dataTrajectoryList = [start]
168
+
169
+ t = random.choice([-1, 1])
170
+ w = 0
171
+ for i in cbb:
172
+ px1 = start[0] + (end[0] - start[0]) * (random.random() * (i[1] - i[0]) + (i[0]))
173
+ p = np.array([px1, self._bztsg([start, end])(px1) + t * deviation])
174
+ dataTrajectoryList.append(p)
175
+ w += 1
176
+ if w >= 2:
177
+ w = 0
178
+ t = -1 * t
179
+
180
+ dataTrajectoryList.append(end)
181
+ # print(dataTrajectoryList)
182
+ return {"equation": self._bztsg(dataTrajectoryList), "P": np.array(dataTrajectoryList)}
183
+
184
+ def trackArray(self, start, end, numberList, le=1, deviation=0, bias=0.5, type=0, cbb=0, yhh=10):
185
+ '''
186
+
187
+ :param start:开始点的坐标 如 start = [0, 0]
188
+ :param end:结束点的坐标 如 end = [100, 100]
189
+ :param numberList:返回的数组的轨迹点的数量 numberList = 150
190
+ :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
191
+ :param deviation:轨迹上下波动的范围 如 deviation = 10
192
+ :param bias:波动范围的分布位置 如 bias = 0.5
193
+ :param type:0表示均速滑动,1表示先慢后快,2表示先快后慢,3表示先慢中间快后慢 如 type = 1
194
+ :param cbb:在终点来回摆动的次数
195
+ :param yhh:在终点来回摆动的范围
196
+ :return:返回一个字典trackArray对应轨迹数组,P对应贝塞尔曲线的影响点
197
+ '''
198
+ s = []
199
+ fun = self._simulation(start, end, le, deviation, bias)
200
+ w = fun['P']
201
+ fun = fun["equation"]
202
+ if cbb != 0:
203
+ numberListOfcbb = round(numberList * 0.2 / (cbb + 1))
204
+ numberList -= (numberListOfcbb * (cbb + 1))
205
+
206
+ xTrackArray = self._type(type, [start[0], end[0]], numberList)
207
+ for i in xTrackArray:
208
+ s.append([i, fun(i)])
209
+ dq = yhh / cbb
210
+ kg = 0
211
+ ends = np.copy(end)
212
+ for i in range(cbb):
213
+ if kg == 0:
214
+ d = np.array([end[0] + (yhh - dq * i),
215
+ ((end[1] - start[1]) / (end[0] - start[0])) * (end[0] + (yhh - dq * i)) + (
216
+ end[1] - ((end[1] - start[1]) / (end[0] - start[0])) * end[0])])
217
+ kg = 1
218
+ else:
219
+ d = np.array([end[0] - (yhh - dq * i),
220
+ ((end[1] - start[1]) / (end[0] - start[0])) * (end[0] - (yhh - dq * i)) + (
221
+ end[1] - ((end[1] - start[1]) / (end[0] - start[0])) * end[0])])
222
+ kg = 0
223
+ # print(d)
224
+ y = self.trackArray(ends, d, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
225
+ s += list(y['trackArray'])
226
+ ends = d
227
+ y = self.trackArray(ends, end, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
228
+ s += list(y['trackArray'])
229
+
230
+ else:
231
+ xTrackArray = self._type(type, [start[0], end[0]], numberList)
232
+ for i in xTrackArray:
233
+ s.append([i, fun(i)])
234
+
235
+ return {"trackArray": np.array(s), "P": w}
236
+
237
+ def generate(self, distance, action_count: int = 0, action_time: int = 0):
238
+ """
239
+ 根据滑动距离生成滑动轨迹
240
+ :param distance: 需要滑动的距离
241
+ :param action_count: 生成操作数量
242
+ :param action_time: 操作总用时
243
+ :return: 滑动轨迹<type 'list'>: [[x,y,t], ...]
244
+ x: 滑动的横向距离
245
+ y: 滑动的纵向距离
246
+ t: 滑动过程消耗的时间, 单位: 秒
247
+ """
248
+ if action_time == 0:
249
+ action_time = random.randint(700, 2000)
250
+ if action_count == 0:
251
+ action_count = distance // 2 + random.randint(10, 20)
252
+ interval_time = action_time // action_count
253
+ le = random.randint(2, 4)
254
+ action_speed_type = random.randint(0, 3)
255
+ data = self.trackArray([0, 0], np.array([distance, 0]), action_count, le, 20, type=action_speed_type, cbb=2)
256
+ slider_track = [[int(v[0]), int(v[1]), interval_time / 1000] for v in data['trackArray']]
257
+ return slider_track
dangqu-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
dangqu-0.1.0/setup.py ADDED
@@ -0,0 +1,29 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name='DangQu',
5
+ version='0.1.0',
6
+ description='dangqu sdk',
7
+ author='DMJ-11740',
8
+ author_email='rpa@lumi.com',
9
+ packages=find_packages(),
10
+ install_requires=[
11
+ 'requests',
12
+ 'oauthlib',
13
+ 'requests-oauthlib',
14
+ 'pillow',
15
+ 'numpy'
16
+ ],
17
+ classifiers=[
18
+ 'Development Status :: 3 - Alpha',
19
+ 'Intended Audience :: Developers',
20
+ 'License :: OSI Approved :: MIT License',
21
+ 'Programming Language :: Python :: 3',
22
+ 'Programming Language :: Python :: 3.7',
23
+ 'Programming Language :: Python :: 3.8',
24
+ 'Programming Language :: Python :: 3.9',
25
+ 'Programming Language :: Python :: 3.10',
26
+ 'Programming Language :: Python :: 3.11',
27
+ 'Programming Language :: Python :: 3.12',
28
+ ],
29
+ )