Skip to content

Commit

Permalink
修复一些程序错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Dandelion246 committed Jul 8, 2024
1 parent d372ad6 commit c99ea5d
Show file tree
Hide file tree
Showing 11 changed files with 555 additions and 414 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ __pycache__/
.DS_Store
venv/
build/
dist/
dist/
main.spec
83 changes: 44 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,54 @@
1. 安装 python 3 (最好大于3.7以上)
2. 下载或者clone最新版本的源代码
3. 在项目根目录 安装依赖 `pip install -r requirements.txt`
4. 运行 `python main.py ` 首次运行会在目录下生成config.ini 文件
5. 配置config.ini 需要从 https://www.pixiv.net 获取 cookie 和 id 教程如下:
- 在登录的情况下进入 pixiv 首页 点击自己头像
- 点击鼠标右键-> 检查 或者 按下键盘 F12 然后 -> 点击网络(Network) 刷新界面
- 点击 Fetch/XHR 找个有cookie 的链接 然后复制 cookie 到配置文件
- 查看网页url获取 自身id 填入config.ini
4. 运行 `python main.py `
5. 如果无法登录的话可以手动配置token 教程如下:
- 在登录的情况下进入 pixiv 任意界面
- 找到如下图的token 复制到config.ini文件的User token中
- ![img](images/2416710515.png)

## Example:
> 下载总有失败的时候 失败的记录会存入数据库 调用 errors_download 重新下载
```python
p = Pixiv()
p.set_proxy('http://127.0.0.1:1080')
# 获取所有关注的用户的所有作品
p.download_user_following()
# 重试下载因各种异常而失败的作品
p.errors_download()
# 订阅关注的用户的最新作品
p.subscribing()
# 下载用户所有作品
p.download_user_works(62286279)
# 下载收藏的所有作品
p.download_user_bookmarks_illust()
# 用户搜索
p.search_user('666', is_all=True)
# 搜索 水着 json_result.illustManga.data
json_result = p.search('水着')
print(json_result.illustManga.data)
# 关注用户的新作
result = p.bookmark_new_illust()
print(result['thumbnails']['illust'])
# 排名榜 今日r18 的插画 每次返回50条 可以使用返回的 'rank_total' 进行分页获取
res = p.illust_ranking('daily_r18', 'illust')
print(res.contents)
# 过取几天的搜索
res = p.illust_ranking('daily', 'manga', '20240621')
print(res.contents)
from pixiv import Pixiv

p = Pixiv()
# 下载单个作品
p.work_detail(10000022).download()
p.work_detail('https://www.pixiv.net/artworks/10000022').download()

p.set_proxy('http://127.0.0.1:1080')
# 获取所有关注的用户的所有作品
p.download_user_following()
# 重试下载因各种异常而失败的作品
p.errors_download()
# 订阅关注的用户的最新作品
p.subscribing()
# 下载用户所有作品
p.download_user_works(62286279)
# 下载收藏的所有作品
p.download_user_bookmarks_illust()
# 用户搜索
p.search_user('666', is_all=True)
# 搜索 水着 json_result.illustManga.data
json_result = p.search('水着')
print(json_result.illustManga.data)
# 关注用户的新作
result = p.bookmark_new_illust()
print(result['thumbnails']['illust'])
# 排名榜 今日r18 的插画 每次返回50条 可以使用返回的 'rank_total' 进行分页获取
res = p.illust_ranking('daily_r18', 'illust')
print(res.contents)
# 过取几天的搜索
res = p.illust_ranking('daily', 'manga', '20240621')
print(res.contents)
```

## config.ini说明
***
```python
```yaml
[User]
cookies =
user_id =
token =

[Network]
# 用户的代理 默认不启用
Expand All @@ -58,7 +61,7 @@ use_proxy =
max_concurrent_threads = 5
# 失败了重试几次?
stop_max_attempt_number = 2
# 重试间隔n秒
# 重试间隔?秒
wait_fixed = 2

[Settings]
Expand All @@ -68,16 +71,18 @@ root =
db_path =
# 连续下载阈值
max_sleep_counter = 120
# 连续下载后休眠多少秒
# 连续下载后休眠?秒
sleep = 60
# 是否覆盖下载 默认为空 不重复下载
is_repeat =
# 是否覆盖下载 默认False 不覆盖
is_repeat = False
# 文件名格式 具体有哪些 继续往下看
illust_file_name = {user}/{title}{id}
manga_file_name = {user}/{title}{id}
series_manga_file_name = {user}/{series_title}/#{series_order} {title}{id}
# 不想下载的用户id 多个,分割
skip_user =
# 是否过滤名称中的特殊字符
is_filter_name = yes

```

Expand Down
224 changes: 124 additions & 100 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,125 @@
import configparser
from typing import Any
from utils import init_config
try:
# Python>=3.8
from typing import Literal
except ImportError:
# Python ==3.7
from typing_extensions import Literal # type: ignore[assignment]

try:
# Python>=3.10
from typing import TypeAlias # type: ignore[attr-defined]
except ImportError:
# Python==3.7, ==3.8, ==3.9
from typing_extensions import TypeAlias

config_path = init_config()
c = configparser.RawConfigParser()
c.read(config_path)

# 并发下载数
MAX_CONCURRENT_THREADS = int(c['Network']['max_concurrent_threads'])
# 重试次数
STOP_MAX_ATTEMPT_NUMBER = int(c['Network']['stop_max_attempt_number'])
# 重试间隔时间
WAIT_FIXED = int(c['Network']['wait_fixed'])

TYPE: TypeAlias = Literal["illust", "manga", ""]
CONTENT_TYPE: TypeAlias = Literal["illust", "manga", 'ugoira', ""]
MODE: TypeAlias = Literal[
"daily", # 今日
"weekly", # 本周
"monthly", # 本月
"rookie", # 新人
"original", # 原创
"daily_ai", # AI生成
"male", # 受男性欢迎
"female", # 受女性欢迎
"daily_r18", # 今日r18
"weekly_r18", # 本周r18
"daily_r18_ai", # AI生成r18
"male_r18", # 受男性欢迎r18
"female_r18", # 受女性欢迎r18
"r18g",
"",
]

SEARCH_URL_TYPE: TypeAlias = Literal[
"artworks",
"top",
"illustrations",
"manga",
"novels",
]

LANG = 'zh'
HEADERS = {
"Content-Type": "text/json; charset=utf-8",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6",
"Referer": "https://www.pixiv.net",
"Sec-Ch-Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"macOS"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sentry-Trace": "730d6a9c934847f58664931e19674606-8d3bc6248cd52379-0",
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5',
"Baggage": "sentry-environment=production,sentry-release=f5f50bb540731f95e7b1eee0509ac311fd4e9525,sentry-public_key=7b15ebdd9cf64efb88cfab93783df02a,sentry-trace_id=3e9bb17e86ac4ca78c8d07847d31d949,sentry-sample_rate=0.0001",
"Cookie": c['User']['cookies'],
"X-User-Id": c['User']['user_id'],
}

TYPE_DICT = {
'0': 'illust',
'1': 'manga',
'2': 'ugoira',
}

MENU_DICT = {
'1': '获取所有关注的用户的所有作品',
'2': '订阅关注的用户的最新作品',
'3': '下载用户所有作品',
'4': '下载收藏的所有作品',
'5': '重试下载因各种异常而失败的作品',
}

sleep_counter = 0


class JsonDict(dict): # type: ignore[type-arg]
"""general json object that allows attributes to be bound to and also behaves like a dict"""

def __getattr__(self, attr: Any) -> Any:
return self.get(attr)

def __setattr__(self, attr: Any, value: Any) -> None:
self[attr] = value
import os
import re
import sys

from logger import logger


class Config:
config_path = ''
root = ''
data: configparser.RawConfigParser = None
# 并发下载数
max_concurrent_threads = 5
# 重试次数
stop_max_attempt_number = 2
# 重试间隔时间
wait_fixed = 2
headers = {
"Content-Type": "text/json; charset=utf-8",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6",
"Referer": "https://www.pixiv.net",
"Sec-Ch-Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"macOS"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sentry-Trace": "730d6a9c934847f58664931e19674606-8d3bc6248cd52379-0",
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5',
"Baggage": "sentry-environment=production,sentry-release=f5f50bb540731f95e7b1eee0509ac311fd4e9525,sentry-public_key=7b15ebdd9cf64efb88cfab93783df02a,sentry-trace_id=3e9bb17e86ac4ca78c8d07847d31d949,sentry-sample_rate=0.0001",
"Cookie": '',
"X-User-Id": '',
}

is_repeat = False

def __init__(self):
self.init()
self.sleep_counter = 0

def init(self):
if getattr(sys, 'frozen', False):
current_dir = os.path.dirname(sys.executable)
elif __file__:
current_dir = os.path.dirname(__file__)
else:
current_dir = os.getcwd()

config_path = os.path.join(current_dir, 'config.ini')
if not os.path.exists(config_path):
c = configparser.ConfigParser()
c['User'] = {
'token': '',
}

c['Network'] = {
'use_proxy': '',
'max_concurrent_threads': '5',
'stop_max_attempt_number': '2',
'wait_fixed': '2'
}

c['Settings'] = {
'root': os.getcwd(),
'db_path': os.path.join(os.getcwd(), 'pixiv.db'),
'max_sleep_counter': '120',
'sleep': '60',
'is_repeat': 'False',
'illust_file_name': '{user}/{title}{id}',
'manga_file_name': '{user}/{title}{id}',
'series_manga_file_name': '{user}/{series_title}/#{series_order} {title}{id}',
'skip_user': '',
'too_many_requests': '200',
'is_filter_name': 'yes',
}

with open(config_path, 'w') as configfile:
c.write(configfile)

logger.info(f"配置文件[{config_path}]已生成, 请配置后重些运行.")
sys.exit()
self.data = configparser.ConfigParser()
self.data.read(config_path)
self.config_path = config_path
# 并发下载数
self.max_concurrent_threads = int(self.data['Network']['max_concurrent_threads'])
# 重试次数
self.stop_max_attempt_number = int(self.data['Network']['stop_max_attempt_number'])
# 重试间隔时间
self.wait_fixed = int(self.data['Network']['wait_fixed'])
self.root = self.data.get('Settings', 'root')
self.is_repeat = self.data.getboolean('Settings', 'is_repeat')

def read(self, key, name):
self.data = configparser.ConfigParser()
self.data.read(self.config_path)
return self.data.get(key, name)

def modify(self, section, option, value):
self.data.set(section, option, value)
with open(self.config_path, 'w') as configfile:
self.data.write(configfile)

def add(self, section, option, value):
if not self.data.has_section(section):
self.data.add_section(section)
self.data.set(section, option, value)
with open(self.config_path, 'w') as configfile:
self.data.write(configfile)

def remove(self, section, option):
if self.data.has_option(section, option):
self.data.remove_option(section, option)

with open(self.config_path, 'w') as configfile:
self.data.write(configfile)


c4g = Config()
if __name__ == '__main__':
print(bool(c4g.is_repeat))
# Config.modify('Network', 'use_proxy', 'xxxx')
Loading

0 comments on commit c99ea5d

Please sign in to comment.