昨天木子问我能不能做自动刷某音短视频,还要自动刷小哥哥,不是小哥哥就划走。

我心想,这女人真麻烦,怎么这么多事。

不好好工作天天想着小哥哥!

为了不得罪她,当时我就先答应了下来,然而实际上我把小哥哥变成了小姐姐,刷什么小哥哥,多没品味!

好了,话不多说,我们直接上代码!

代码实战

首先导入需要使用的模块

import base64

import urllib

import json

import requests

import sys

 

获取接口

获取 access_token 有效期一般有一个月

client_id = api_key

client_secret = secret_key

# 完整代码我都放在这个群了 872937351

# 直接加它领取

auth_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

header_dict = {

'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',

"Content-Type": "application/json"

}

 

请求获取到token的接口

response_at = requests.get(auth_url, headers=header_dict)

json_result = json.loads(response_at.text)

access_token = json_result['access_token']

return access_token

 

调用人脸识别的接口,返回识别到的人脸列表。

headers ={

'Content-Type': 'application/json; charset=UTF-8'

}

if pic_type == TYPE_IMAGE_NETWORK:

image = pic_url

image_type = 'URL'

else:

with open(pic_url, 'rb') as file:

image = base64.b64encode(file.read())

image_type = 'BASE64'

post_data ={

'image': image,

'image_type': image_type,

'face_field': 'facetype,gender,age,beauty', # expression,faceshape,landmark,race,quality,glasses

'max_face_num': 2

}

response_fi = requests.post(url_fi, headers=headers, data=post_data)

json_fi_result = json.loads(response_fi.text)

 

如果人脸识别成功,返回人脸列表,否则返回None

if not json_fi_result or json_fi_result['error_msg'] != 'SUCCESS':

return None

else:

return json_fi_result['result']['face_list']

 

人脸识别,返回人脸列表。

def parse_face_pic(pic_url, pic_type, access_token):

url_fi = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + access_token

# 调用identify_faces,获取人脸列表

json_faces = identify_faces(pic_url, pic_type, url_fi)

if not json_faces:

return None

else:

return json_faces

 

解析人脸识别结果,判断颜值是否达标。

条件:性别女,颜值大于等于 70

def analysis_face(face_list):

# 是否能找到漂亮小姐姐

find_plxjj = False

if face_list:

for face in face_list:

# 判断是男、女

if face['gender']['type'] == 'female':

age = face['age']

beauty = face['beauty']

if beauty >= 70:

print('发现一个 ' + str(age) + ' 岁的美女,颜值为:%d,满足条件!' % beauty)

find_plxjj = True

break

else:

print('发现一个 ' + str(age) + ' 岁的女生,颜值为:%d,不及格,继续~' % beauty)

continue

return find_plxjj

 

App的应用包名和初始Activity

package_name = 'com.ss.android.ugc.aweme'

activity_name = 'com.ss.android.ugc.aweme.splash.SplashActivity'

 

打开 Android 应用

def start_my_app(package_name, activity_name):

os.popen('adb shell am start -n %s/%s' % (package_name, activity_name))

 

保存截图以及点赞

def save_video_met(screen_name, find_girl_num):

img = Image.open(screen_name).convert('RGB')

img.save("漂亮的小姐姐/DYGirl_%d.jpg" % find_girl_num)

os.system("adb shell input tap 666 800")

 

向上划屏幕,播放下一段视频

def play_next_video():

os.system("adb shell input swipe 540 1300 540 500 100")

 

截图并修剪保存

def get_screen_shot_part_img(image_name):

# 截图

os.system("adb shell /system/bin/screencap -p /sdcard/screenshot.jpg")

os.system("adb pull /sdcard/screenshot.jpg %s" % image_name)

# 打开图片

img = Image.open(image_name).convert('RGB')

# 图片的原宽、高

w, h = img.size

# 截取部分,去掉其头像、其他内容杂乱元素

img = img.crop((0, 400, 1200, 2750))

img.thumbnail((int(w / 1.5), int(h / 1.5)))

# 保存到本地

img.save(image_name)

return image_name

 

人脸识别时长、次数

设置一条视频最长的识别时间,要是墨迹 10 秒还不露脸,也不管她了,直接下一个。

access_token = get_access_token()

# 识别时长

RECOGNITE_TOTAL_TIME = 10

# 识别次数

recognite_count = 0

 

视频识别

start_my_app(package_name, activity_name)

time.sleep(3)

print("开始播放视频~")

find_girl_num = 0

# 对当前视频截图去人脸识别

while True:

# 开始识别的时间

recognite_time_start = datetime.datetime.now()

# 识别次数

recognite_count = 1

# 循环地去刷抖音

while True:

# 获取截图

screen_name = get_screen_shot_part_img('images/temp%d.jpg' % recognite_count)

# 人脸识别

recognite_result = analysis_face(parse_face_pic(screen_name, TYPE_IMAGE_LOCAL, access_token))

recognite_count += 1

# 第n次识别结束后的时间

recognite_time_end = datetime.datetime.now()

# 这是一个美女

if recognite_result:

find_girl_num += 1

save_video_met(screen_name, find_girl_num)

print("已经发现 %d 个漂亮小姐姐" % find_girl_num)

break

else:

if (recognite_time_end - recognite_time_start).seconds < RECOGNITE_TOTAL_TIME:

continue

else:

print('跳过!!!!只想刷美女视频')

# 跳出里层循环

break

 

删除临时文件

shutil.rmtree('./images')

time.sleep(0.05)

os.mkdir('./images')

 

播放下一条视频

print('==' * 30)

time.sleep(1)

print('准备播放下一个视频~')

play_next_video()

import random

time.sleep(random.uniform(0,1))

 

最后

好了,今天的分享就到这里结束了!

大家觉得有用的话可以来个免费的点赞+收藏+关注,防止下次我悄悄更新了好东西却不知道!

查看原文