Commit cf0fa24e by lei

1

parents
File added
# 默认忽略的文件
/shelf/
/workspace.xml
Img_Ai
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/python-demo.iml" filepath="$PROJECT_DIR$/.idea/python-demo.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager">
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed. Click to expand it.
import requests
# 官方文档地址,需要科学上网使用
# http://doc.bitbrowser.cn/api-jie-kou-wen-dang/ben-di-fu-wu-zhi-nan
# 此demo仅作为参考使用,以下使用的指纹参数仅是部分参数,完整参数请参考文档
request = requests.session()
url = "http://127.0.0.1:54345"
def createOrUpdateBrowser(browser_item): # 创建或者更新窗口
headers = {
# 7 1115ce586624431a8b7bcba8b1e82035
# 9 db474b6936a24a7a949377a6d90b4dcb
'id': browser_item['id'], # 有值时为修改,无值是添加
# 'groupId': '2c996b378054663b01805a69f0344410', # 群组ID,绑定群组时传入,如果登录的是子账号,则必须赋值,否则会自动分配到主账户下面去
'platform': 'https://discord.com', # 账号平台
'platformIcon': 'discord', # 取账号平台的 hostname 或者设置为other
'url': '', # 打开的url,多个用,分开
'name': 'Discord', # 窗口名称
'remark': '登录discord', # 备注
'userName': '', # 用户账号
'password': '', # 用户密码
'cookie': '', # Cookie,符合标准的序列化字符串,具体可参考文档
# IP库,默认ip-api,选项 ip-api | ip123in | luminati,luminati为Luminati专用
'ipCheckService': 'ip-api',
'proxyMethod': 2, # 代理方式 2自定义 3 提取IP
# 代理类型 ['noproxy', 'http', 'https', 'socks5', '911s5']
'proxyType': 'http',
'host': '192.168.100.139', # 代理主机
'port': 2009, # 代理端口
'proxyUserName': '', # 代理账号
'proxyPassword': '', # 代理密码
'ip': '', # ip
'city': '', # 城市
'province': '', # 州/省
'country': '', # 国家地区
'dynamicIpUrl': '', # 提取IP url,参考文档
'dynamicIpChannel': '', # 提取IP服务商,参考文档
'isDynamicIpChangeIp': True, # 提取IP方式,参考文档
'isGlobalProxyInfo': False, # 提取IP设置,参考文档
'isIpv6': False, # 是否是IP6
'syncTabs': True, # 同步标签页
'syncCookies': True, # 同步Cookie
'syncIndexedDb': False, # 同步IndexedDB
'syncLocalStorage': False, # 同步 Local Storage
'syncBookmarks': True, # 同步书签
'credentialsEnableService': True, # 禁止保存密码弹窗
'syncHistory': False, # 保存历史记录
'clearCacheFilesBeforeLaunch': False, # 启动前清理缓存文件
'clearCookiesBeforeLaunch': False, # 启动前清理cookie
'clearHistoriesBeforeLaunch': False, # 启动前清理历史记录
'randomFingerprint': False, # 每次启动均随机指纹
# 浏览器窗口工作台页面,默认 chuhai2345,不展示填 disable 可选 chuhai2345 | localserver | disable
'workbench': 'chuhai2345',
'disableGpu': False, # 关闭GPU硬件加速 False取反 默认 开启
'enableBackgroundMode': False, # 关闭浏览器后继续运行应用
'disableTranslatePopup': False, # 翻译弹窗
'syncExtensions': False, # 同步扩展应用数据
'syncUserExtensions': False, # 跨窗口同步扩展应用
'allowedSignin': False, # 允许google账号登录浏览器
'abortImage': False, # 禁止加载图片
'abortMedia': False, # 禁止视频自动播放
'muteAudio': False, # 禁止播放声音
'stopWhileNetError': False, # 网络不通停止打开
"browserFingerPrint": {
'coreVersion': '104', # 内核版本,默认104,可选92
'ostype': 'PC', # 操作系统平台 PC | Android | IOS
'os': 'Win32',
# 为navigator.platform值 Win32 | Linux i686 | Linux armv7l | MacIntel,当ostype设置为IOS时,设置os为iPhone,ostype为Android时,设置为 Linux i686 | | Linux armv7l
'version': '', # 浏览器版本,不填随机
# 'userAgent': '', # ua,不填则自动生成
'isIpCreateTimeZone': True, # 基于IP生成对应的时区
'timeZone': '', # 时区,isIpCreateTimeZone 为False时,参考附录中的时区列表
'timeZoneOffset': 0, # isIpCreateTimeZone 为False时设置,时区偏移量
'webRTC': '0', # webrtc 0替换 | 1允许 | 2禁止
'ignoreHttpsErrors': False, # 忽略https证书错误,True | False
'position': '1', # 地理位置 0询问 | 1允许 | 2禁止
'isIpCreatePosition': True, # 是否基于IP生成对应的地理位置
'lat': '', # 经度 isIpCreatePosition 为False时设置
'lng': '', # 纬度 isIpCreatePosition 为False时设置
'precisionData': '', # 精度米 isIpCreatePosition 为False时设置
'isIpCreateLanguage': True, # 是否基于IP生成对应国家的浏览器语言
'languages': '', # isIpCreateLanguage 为False时设置,值参考附录
'isIpCreateDisplayLanguage': False, # 是否基于IP生成对应国家的浏览器界面语言
'displayLanguages': '', # isIpCreateDisplayLanguage 为False时设置,默认为空,即跟随系统,值参考附录
'openWidth': 1280, # 窗口宽度
'openHeight': 720, # 窗口高度
'resolutionType': '0', # 分辨率类型 0跟随电脑 | 1自定义
'resolution': '1920 x 1080', # 自定义分辨率时,具体值
'windowSizeLimit': True, # 分辨率类型为自定义,且ostype为PC时,此项有效,约束窗口最大尺寸不超过分辨率
'devicePixelRatio': 1, # 显示缩放比例,默认1,填写时,建议 1|1.5 | 2 | 2.5 | 3
'fontType': '2', # 字体生成类型 0系统默认 | 1自定义 | 2随机匹配
'font': '', # 自定义或随机匹配时,设置的字体值,值参考附录字体
'canvas': '0', # canvas 0随机|1关闭
'canvasValue': None, # canvas为0随机时设置, 噪音值 10000 - 1000000
'webGL': '0', # webGL图像,0随机|1关闭
'webGLValue': None, # webGL为0时,随机噪音值 10000 - 1000000
'webGLMeta': '0', # webgl元数据 0自定义|1关闭
'webGLManufacturer': '', # webGLMeta 自定义时,webGL厂商值,参考附录
'webGLRender': '', # webGLMeta自定义时,webGL渲染值,参考附录
'audioContext': '0', # audioContext值,0随机|1关闭
'audioContextValue': None, # audioContext为随机时,噪音值, 1 - 100 ,关闭时默认10
'mediaDevice': '0', # 媒体设备信息,0自定义|1关闭
'mediaDeviceValue': None, # mediaDevice 噪音值,不填则由系统生成,填值时,参考附录
'speechVoices': '0', # Speech Voices,0随机|1关闭
'speechVoicesValue': None, # speechVoices为0时,随机时由系统自动生成,自定义时,参考附录
'hardwareConcurrency': '4', # 硬件并发数
'deviceMemory': '8', # 设备内存
'doNotTrack': '0', # doNotTrack 1开启|0关闭
# ClientRects True使用相匹配的值代替您真实的ClientRects | False每个浏览器使用当前电脑默认的ClientRects
'clientRectNoiseEnabled': True,
'clientRectNoiseValue': 0, # clientRectNoiseEnabled开启时随机,值 1 - 999999
'portScanProtect': '0', # 端口扫描保护 0开启|1关闭
'portWhiteList': '', # 端口扫描保护开启时的白名单,逗号分隔
'deviceInfoEnabled': True, # 自定义设备信息,默认开启
'computerName': '', # deviceInfoEnabled 为True时,设置
'macAddr': '', # deviceInfoEnabled 为True时,设置
# ssl是否禁用特性,默认不禁用,注意开启后自定义设置时,有可能会导致某些网站无法访问
'disableSslCipherSuitesFlag': False,
'disableSslCipherSuites': None, # ssl 禁用特性,序列化的ssl特性值,参考附录
'enablePlugins': False, # 是否启用插件指纹
'plugins': '' # enablePlugins为True时,序列化的插件值,插件指纹值参考附录
}
}
res = request.post(f"{url}/browser/update", json=headers).json()
browserId = res['data']['id']
print(browserId)
return browserId
def openBrowser(browser_item): # 打开窗口
headers = {'id': f'{createOrUpdateBrowser(browser_item)}'}
res = request.post(f"{url}/browser/open", json=headers).json()
print(res)
return res
def closeBrowser(id): # 关闭窗口
headers = {'id': f'{id}'}
request.post(f"{url}/browser/close", json=headers).json()
def deleteBrowser(id): # 删除窗口
headers = {'id': f'{id}'}
print(request.post(f"{url}/browser/delete", json=headers).json())
if __name__ == '__main__':
createOrUpdateBrowser()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
import pyperclip
import time
from loguru import logger
from bit_api import *
import asyncio
from Helper import RedisCon
import json
from multiprocessing import Process
import random
class ToWork:
def __init__(self):
self.init_prompt = '/imagine'
# 随机等待
def random_sleep(self, min, max):
time.sleep(random.uniform(min, max))
# 粘贴板插入数据
def driver_copy(self, driver, value):
pyperclip.copy(value)
# 粘贴
def driver_paste(self, dricer, value):
ActionChains(dricer).send_keys(Keys.CONTROL, value).perform()
# 元素出现的循环次数
def CheckElement(self, dricer, type, value, num=10):
for i in range(num):
element = self.sleepElementShow(dricer, type, value)
if element:
# 随机等待
self.random_sleep(1, 2)
return element
time.sleep(1)
# 等待元素出现--自定义
def sleepElementShow(self, dricer, type, value):
try:
element = dricer.find_element(type, value)
if element:
return element
return False
except Exception as e:
return False
# 元素点击
def ElementClick(self, dricer, type, value, name=''):
try:
task = dricer.find_element(type, value)
if task:
# 随机等待
self.random_sleep(1, 2)
# js 点击
dricer.execute_script("arguments[0].click()", task)
# task.click()
logger.info(f'{name}-元素存在,开始点击')
return True
logger.info(f'{name}-元素不存在')
return False
except Exception as e:
logger.info(f'{name}-元素不存在')
logger.error(e)
return False
# 循环输入指令
def enter_prompt(self, dricer, tasks):
# task是一个列表
for task in tasks:
# 输入初始指令
logger.info('输入初始指令')
self.driver_paste(dricer, self.init_prompt)
# 选择指令
prompt_el = '//*[@id="autocomplete-0"]'
# 等待选择指令元素出现
element = self.CheckElement(dricer, 'xpath', prompt_el, 5)
if element:
status = self.ElementClick(dricer, 'xpath', prompt_el, '选择指令')
if status:
self.driver_paste(dricer, task['prompt'])
# 回车
ActionChains(dricer).send_keys(Keys.ENTER).perform()
time.sleep(2)
# 输入完成,循环获取生成成功后的链接
# 获取当前事件
while True:
# 获取任务列表
def get_current_tasks(self):
# 测试
# 当前浏览器需要执行的任务
cur_tasks = []
for j in range(2):
# task = RedisCon.get_task(key)
task = {
'prompt': "a very cute boys, 16 years old, fly a kite,in beautifulstreet,full body,3d art, c4d, octane render, ray tracting,clay material,popmart blind box,Pixar trend, animationlighting, depth of field, ultra detailed --q 5",
'user_id': 1,
'task_id': 1,
'prompt_img': [],
'result_img': "",
'type': 2,
'callback': 'http://test.phpgpt.com/api/users/prompt/callback',
}
if task:
# 将任务添加到列表
cur_tasks.append(task)
if len(cur_tasks):
return cur_tasks
return []
def open_browser(self, browser_item):
try:
logger.info(f'浏览器id-{browser_item["num"]},开始执行')
# /browser/open 接口会返回 selenium使用的http地址,以及webdriver的path,直接使用即可
res = openBrowser(browser_item)
driverPath = res['data']['driver']
debuggerAddress = res['data']['http']
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", debuggerAddress)
dricer = webdriver.Chrome(service=ChromeService(driverPath), options=chrome_options)
logger.info('打开窗口')
dricer.get('https://discord.com/channels/@me')
# dricer.get('https://discord.com/login')
return True
except Exception as e:
logger.error(e)
return False
# 初始化任务
def on_start(self, browser_item):
while True:
# 先获取任务
tasks = self.get_current_tasks()
if len(tasks):
# 打开窗口
browser_status = self.open_browser(browser_item)
if not browser_status:
# 浏览器打开失败
logger.error(f'{browser_item["num"]},窗口打开失败,检查代理ip')
else:
# 开始执行任务
statsu = self.run_task(browser_item, tasks)
if statsu == False:
logger.info('请检查自动化')
else:
time.sleep(3)
logger.info('不存在任务,等待3秒')
def run_task(self, browser_item, tasks):
try:
logger.info('等待登录成功')
# 判断页面是否加载完成
page_load = '//*[@data-list-item-id="guildsnav___home"]'
element = self.CheckElement(dricer, 'xpath', page_load)
if not element:
logger.error('登录失败')
# 去登陆
return False
# 登录成功
logger.info('登录成功')
# 等待服务器元素出现
server_el = '//div[contains(@aria-label, "测试服务器")]'
element = self.CheckElement(dricer, 'xpath', server_el)
if element:
# 点击指定服务器
status = self.ElementClick(dricer, 'xpath', server_el, '测试服务器')
if status:
# 等待聊天窗口出现
input_el = '//*[@role="textbox"]'
element = self.CheckElement(dricer, 'xpath', input_el)
if element:
logger.info('聊天窗口存在')
# 输入指令
self.enter_prompt(dricer, tasks)
logger.info('元素未找到,任务暂停')
return False
except Exception as e:
logger.error(e)
return False
if __name__ == '__main__':
key = 'laravel_database_midjourney_prompt'
bro = ToWork()
# while False:
# tasks = list()
# # 获取任务数量
# task_num = RedisCon.get_task_num(key)
# print(task_num, '个任务')
# nums = 3
# if task_num and task_num > 0:
# if task_num < nums:
# process = task_num
# else:
# # 最多同时3个任务
# process = nums
# loop = asyncio.get_event_loop()
# for i in range(process):
# task = RedisCon.get_task(key)
# if task:
# # 存在任务--转对象
# task = json.loads(task)
# logger.info(task)
# tasks.append(bro.OpenBros(task))
# loop.run_until_complete((asyncio.wait(tasks, timeout=30)))
# else:
# time.sleep(3)
# continue
# 测试-两个任务
browser_list = [
{
# 7
'num': 7,
'id': '1115ce586624431a8b7bcba8b1e82035'
},
{
# 9
'num': 9,
'id': 'db474b6936a24a7a949377a6d90b4dcb'
}
]
#
l = []
for item in browser_list:
p = Process(target=bro.OpenBros, args=(item,))
p.start()
l.append(p) # 将进程加入列表中,这样 5 个子进程就会全部执行
for p in l:
p.join() # 当5 个子进程全部执行完,才会执行主进程
import asyncio
import json
import time
import openai
from loguru import logger
from Helper import RedisCon, Helper
# key
api_key = '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'
# 组织标识
organization = 'org-ZMM4WGwi3vmM7o5y51IljTcW'
# 回调接口
def gpt_callback(res_prompt, task):
url = task['callback']
print(1)
def split_prompt(response):
logger.info(response)
def get_gpt_message(task):
openai.organization = organization
openai.api_key = api_key
prompt_num = task['prompt_num']
# 指令
prompt = task['prompt']
logger.info('开始访问open_ai')
try:
response = openai.Completion.create(
model="text-davinci-003",
prompt="请根据我给你的指令生成"+str(prompt_num)+"条相似的指令,指令如下:"+prompt,
temperature=1,
max_tokens=500,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=None
)
logger.info(response)
except Exception as e:
logger.info(f"错误 {e}")
# 拆分指令
res_prompt = split_prompt(response, task)
# 回调
gpt_callback(res_prompt, task)
logger.info('open_ai_结束')
if __name__ == '__main__':
key = 'laravel_database_gpt_text_prompt'
logger.info('开始执行')
while True:
tasks = list()
# 获取任务数量
task_num = RedisCon.get_task_num(key)
print(task_num, '个任务')
nums = 10
if task_num and task_num > 0:
if task_num < nums:
process = task_num
else:
# 最多同时10个任务
process = nums
loop = asyncio.get_event_loop()
for i in range(process):
task = RedisCon.get_task(key)
if task:
# 存在任务--转对象
task = json.loads(task)
logger.info(task)
tasks.append(get_gpt_message(task))
loop.run_until_complete((asyncio.wait(tasks, timeout=30)))
else:
time.sleep(3)
continue
# test_task = {
# 'type': 2,
# 'prompt': 'a very cute boys, 16 years old, fly a kite,in beautifulstreet,full body,3d art, c4d, octane render, ray tracting,clay material,popmart blind box,Pixar trend, animationlighting, depth of field, ultra detailed --q 5',
# 'prompt_img': '',
# 'user_id': 1,
# 'prompt_num': 5,
# 'task_id': 10,
# 'callback': 'http://test.phpgpt.com/api/users/gpt/callback'
# }
# get_gpt_message(test_task)
# 图像质量增强
# client_id 为官网获取的AK, client_secret 为官网获取的SK
import requests
import base64
from loguru import logger
class ImgEnhance:
def __init__(self):
self.host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=SEVtEVlQPyqf55mgGt5u5Ad2&client_secret=0CNGhjMrgXRPmDE014S2IXyHLKCuTcfK'
self.res_token = ''
# 获取百度api的token
def get_api_token(self):
response = requests.get(self.host)
if response:
print(response.json())
access_token = response.json()['access_token']
self.res_token = access_token
print(access_token)
self.Work()
# self.Img_enlarge()
# 开始图片增强处理
def Work(self):
'''
图像清晰度增强
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance"
# 二进制方式打开图片文件
f = open('./res_assets/test_10.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
request_url = request_url + "?access_token=" + self.res_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
imgdata = base64.b64decode(response.json()["image"])
file = open(r'./res_assets/test_10.jpg', 'wb')
file.write(imgdata)
file.close()
# 图片无损放大
def Img_enlarge(self):
'''
图片无损放大
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_enhance"
# 二进制方式打开图片文件
f = open('./res_assets/test_10.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
request_url = request_url + "?access_token=" + self.res_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
imgdata = base64.b64decode(response.json()["image"])
file = open(r'./res_assets/test_10.jpg', 'wb')
file.write(imgdata)
file.close()
if __name__ == '__main__':
Img = ImgEnhance()
Img.get_api_token()
import cv2
import os
import pathlib
target_path = "./res_assets"
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
print(path + ' 目录已存在')
return False
def split_img(img_file):
img = cv2.imread(img_file)
# 这一段是改为灰色
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_h = img.shape[0] # 高度
img_w = img.shape[1] # 宽度
# h1_half = int(img_h / 2)
# w1_half = int(img_w / 2)
h1_half = img_h // 2
w1_half = img_w // 2
img_name = os.path.basename(img_file)
for i in range(4):
img1 = img[int(i / 2) * h1_half: h1_half * (int(i / 2) + 1), int(i % 2) * w1_half: (int(i % 2) + 1) * w1_half]
img1_path = os.path.join(target_path, f"{img_name[:-4]}_{i}.jpg")
print("spilt img:", img1_path)
cv2.imwrite(img1_path, img1)
if __name__ == '__main__':
mkdir(target_path)
split_img(os.path.join('./assets', 'test.png'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment