怎么添加handsome文档大部分都有写,但主要的问题是没有现成的表情,总不能只用别人现成的整理好的,比如我想要b站的表情包,以及还有我本地的一份别的表情包,找了找只找到了教你怎么扒表情包的,emm,几百个表情我总不能一个个手动输吧,懒得找了,就拿这个脚本改改

b站表情有对应的api地址原脚本是将表情名和url地址扒下来写入xlxs中,然后再下载

那么我只需要将数据整理下转成主题需要的json就行了

# -*- coding: utf-8 -*-
'''
@File: emoji.py
@Time: 2024/02/13 17:01:11
@Author: xiaolong
@Version: 1.0
@Link: https://xiaolong22333.top
'''

import requests
import pandas as pd
from openpyxl import Workbook
import time
import random
import re
from pypinyin import pinyin, Style
import json
import os

wb = Workbook()
ws = wb.active
ws.append(['text','url'])

#bilibili表情api(免费表情)
url = 'http://api.bilibili.com/x/emote/user/panel/web?business=reply'

#请求头
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.52"}

#获取b站表情包json数据
def get_emoji():
    res = requests.get(url,headers=headers)
    info = res.json()

    #遍历取出表情url,不需要颜文字
    for package in info['data']['packages'][:3]:
        for emo in package['emote']:
            ws.append([emo['text'],emo['url']])

    wb.save('emotion.xlsx')

#下载表情包,转成主题需要的json格式
def download_emoji():
    #准备下载表情
    data = pd.read_excel('emotion.xlsx',engine='openpyxl')

    #将表情包网址与表情包名称对应
    emote = zip(data['text'],data['url'])
    list_emote = list(emote)

    #小黄脸
    container_data = []
    for i in range(0,90):
        jpg = requests.get(list_emote[i][1],headers=headers)
        text = re.sub(r'[\[\]_?热词系列tv]', '', list_emote[i][0])
        #中文转为拼音
        icon = ''.join(item[0] for item in pinyin(text, style=Style.NORMAL))
        container_data.append({"icon": f"{icon}", "text": f"{text}"})
        #下载表情包
        with open('./download/小黄脸/%s.png'%icon,'wb') as file:
            file.write(jpg.content)
    emoji_data = {
        "小黄脸": {
            "type": "image",
            "name": "小黄脸",
            "container": container_data
        }
    }
    json_emoji_data = json.dumps(emoji_data, ensure_ascii=False, indent=4)
    with open('./download/小黄脸/小黄脸.json','w') as file:
        file.write(json_emoji_data)

    #热词系列
    container_data = []
    for i in range(147,222):
        jpg = requests.get(list_emote[i][1],headers=headers)
        text = re.sub(r'[\[\]_?热词系列tv]', '', list_emote[i][0])
        #中文转为拼音
        icon = ''.join(item[0] for item in pinyin(text, style=Style.NORMAL))
        container_data.append({"icon": f"{icon}", "text": f"{text}"})
        #下载表情包
        with open('./download/热词系列/%s.png'%icon,'wb') as file:
            file.write(jpg.content)
    emoji_data = {
        "热词系列": {
            "type": "image",
            "name": "热词系列",
            "container": container_data
        }
    }
    json_emoji_data = json.dumps(emoji_data, ensure_ascii=False, indent=4)
    with open('./download/热词系列/热词系列.json','w') as file:
        file.write(json_emoji_data)

    #小电视
    container_data = []
    for i in range(222,272):
        jpg = requests.get(list_emote[i][1],headers=headers)
        text = re.sub(r'[\[\]_?热词系列tv]', '', list_emote[i][0])
        #中文转为拼音
        icon = ''.join(item[0] for item in pinyin(text, style=Style.NORMAL))
        container_data.append({"icon": f"{icon}", "text": f"{text}"})
        #下载表情包
        with open('./download/小电视/%s.png'%icon,'wb') as file:
            file.write(jpg.content)
    emoji_data = {
        "小电视": {
            "type": "image",
            "name": "小电视",
            "container": container_data
        }
    }
    json_emoji_data = json.dumps(emoji_data, ensure_ascii=False, indent=4)
    with open('./download/小电视/小电视.json','w') as file:
        file.write(json_emoji_data)


#获取本地的表情包转成json格式
def local_emoji():
    container_data = []
    files = os.listdir('./download/OwO')
    for file in files:
        container_data.append({"icon": f"{os.path.splitext(file)[0]}", "text": f"{os.path.splitext(file)[0]}"})
    emoji_data = {
        "贴吧": {
            "type": "image",
            "name": "贴吧",
            "container": container_data
        }
    }
    json_emoji_data = json.dumps(emoji_data, ensure_ascii=False, indent=4)
    with open('./download/OwO/OwO.json','w') as file:
        file.write(json_emoji_data)

if __name__ == '__main__':
    get_emoji()
    download_emoji()
    local_emoji()

有了表情包和对应的json后按照文档上传

image-20240213223555295

然后还要编辑 /usr/themes/handsome/assets/css 里的 handsome.min.css 文件

添加对应表情包名称样式,大小按自己喜好

emotion-quyin{width: 35px;}.emotion-xiaodianshi{width: 30px;}.emotion-xiaohuanglian{width: 30px;}.emotion-recixilie{width: 30px;}.emotion-tieba{width: 25px;}

加在这个位置

添加多个表情包后原本的表情栏因为长度问题表情会显示不全,还需要手动修改下

/usr/themes/handsome/assets/css/origin/function.min.css中修改width

最后效果如下

毕业后就没写过代码了,不得不借助gpt(真好用),写得也很烂,大佬们见谅😢

最后修改:2024 年 02 月 15 日
如果觉得我的文章对你有用,请随意赞赏