Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add moehu support #56

Merged
merged 11 commits into from
Mar 17, 2024
Merged

feat: add moehu support #56

merged 11 commits into from
Mar 17, 2024

Conversation

Kabuda-czh
Copy link
Member

@Kabuda-czh Kabuda-czh commented Mar 30, 2023

fix #50

@Kabuda-czh
Copy link
Member Author

python script

import requests
import re
from bs4 import BeautifulSoup

"""
    2023.3.30 版本
"""

res = requests.get("https://img.moehu.org/")
bs = BeautifulSoup(res.text, 'html.parser')

menu_divs = bs.find_all(class_='menuDiv')[:-3]

# 最终返回的 json 数据
key_map = {}

for menu in menu_divs:
    menu_ul = menu.find_next('ul')
    for pre in menu_ul.find_all('pre'):
        name, id_ = re.search(r'\((.*?)→.*id=(.*?)".*?>', str(pre)).groups()
        key_map[name] = id_

@MaikoTan MaikoTan marked this pull request as draft March 30, 2023 13:55
@MaikoTan
Copy link
Member

I asked ChatGPT and it gave me the following code do the same thing as the code you posted, see if this work. If so, I think it can be also merged into the code base to used for get the id list.

我询问了 ChatGPT 然后它给了我以下的代码,和你发布的代码做了同样的事情,看看它是否工作。如果是的,我想它也可以被合并到代码库里,用于获取 id 列表。

import * as request from 'request';
import * as cheerio from 'cheerio';

/**
 * 版本日期:2023.3.30
 */

const url = 'https://img.moehu.org/';

request(url, (error, response, body) => {
  if (!error && response.statusCode === 200) {
    const $ = cheerio.load(body);

    const menuDivs = $('.menuDiv').slice(0, -3);

    // 最终返回的 json 数据
    const keyMap: Record<string, string> = {};

    menuDivs.each((index, menu) => {
      const menuUl = $(menu).next('ul');
      menuUl.find('pre').each((_, pre) => {
        const match = $(pre).html()?.match(/\((.*?)→.*id=(.*?)".*?>/);
        if (match) {
          const [, name, id] = match;
          keyMap[name] = id;
        }
      });
    });

    // 使用 keyMap 进行后续操作
    console.log(keyMap);
  }
});

Another possible improvement is to read the first tag (in Chinese) only and compare it with cached keys in the map above, then convert it to the actual id.

另一个可能的提升是只读取第一个标签(在中文),然后将它和上面快取的地图中的钥匙进行对比,然后转换到真实的 id。

Copy link
Member Author

@Kabuda-czh Kabuda-czh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review

Copy link
Member

@Lipraty Lipraty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upgrade dependencies

Copy link

github-actions bot commented Mar 16, 2024

PR Health

Build ✅

Details
yarn run v1.22.22
$ yakumo build
Done in 2.92s.

@MaikoTan MaikoTan requested a review from Lipraty March 16, 2024 14:51
packages/moehu/src/index.ts Outdated Show resolved Hide resolved
@MaikoTan MaikoTan added this pull request to the merge queue Mar 17, 2024
Merged via the queue into main with commit ae087e4 Mar 17, 2024
8 checks passed
@MaikoTan MaikoTan deleted the 50-moehu branch March 17, 2024 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Support Moehu.org Source
3 participants