活在当下

新年假期在连续两场的大雪后结束了,今天开始了新一周的正式的工作,今年特地为假期写了一个简略的手机笔记,以后需要把这个习惯好好保持下去,假期的每一天都记录下来。

假期中见到了许多人,聊到了许多事,每个场景的切换都一些唐突和无法适应,但是又合情合理。

老舅的话让我印象深刻,他说,人不能总是活在过去,过去的悔恨懊恼会让人抑郁,也不能总是活在未来,未来的彷徨未知让人焦虑,所以最好的选择是活在当下~

以前总是听到活在当下这句话,这次是真的有所理解了。

君不密则失臣,臣不密则失身,几事不密则害成。是以君子慎密而不出也。

密

Hexo保留链接隐藏文章

Hexo生成的一些文章,默认是全部被主页和标签索引的,那么有些文章不希望被索引,而且有希望通过链接直接分享给别人看,就可以利用hexo-hide-posts插件。

  1. 先安装hexo-hide-posts
    1
    npm install hexo-hide-posts --save
  2. 在想要隐藏的文章里添加hidden标记。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ---
    layout: post
    title: "Hexo保留链接隐藏文章"
    date: 2024-01-02 13:04:38 +0800
    hidden: true
    tags:
    - Hexo
    categories: Tech
    ---

只需简单这两部就可以了,虽然可以在_config.yml做一些更详细的配置,但是没必要。
重新生成文章,就可以看到隐藏的文章不在主页和标签页里了。但是可以通过链接访问,链接可以在生成文章的日志里找到。

在centos上安装immich

一直在研究怎么把自己的上万张照片找一个合适的地方放置,目前还没有定论,但是比较倾向于immich.app.

记录在centos上的安装过程。

安装docker-ce

  1. enable 阿里云里的docker-ce的repo

    1
    2
    yum install -y yum-utils
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  2. yum安装docker-ce

    1
    yum install -y docker-ce docker-ce-cli --nobest --skip-broken
  3. 启动docker-ce

    1
    2
    3
    systemctl enable docker
    systemctl start docker
    systemctl status docker # 检查是不是启动成功

通过docker compose安装immich.app

  1. 创建目录
    如果挂载了多个磁盘,建议选择一个合适的目录,immich启动后会挂载全部的所在目录。

    1
    2
    mkdir ./immich-app
    cd ./immich-app
  2. 准备 docker-compose.yml 和 example.env
    下载文件:

    1
    2
    wget https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml
    wget -O .env https://github.com/immich-app/immich/releases/latest/download/example.env

    下载后docker-compose.yml需要改动一下镜像的地址,换成国内的源。
    github的镜像源ghcr.io可以换成南京大学的ghcr.nju.edu.cn,默认dockerhub的源换成docker.nju.edu.cn,国内访问速度比较快。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    version: "3.8"

    services:
    immich-server:
    container_name: immich_server
    image: ghcr.nju.edu.cn/immich-app/immich-server:${IMMICH_VERSION:-release}
    command: [ "start.sh", "immich" ]
    volumes:
    - ${UPLOAD_LOCATION}:/usr/src/app/upload
    env_file:
    - .env
    depends_on:
    - redis
    - database
    - typesense
    restart: always

    immich-microservices:
    container_name: immich_microservices
    image: ghcr.nju.edu.cn/immich-app/immich-server:${IMMICH_VERSION:-release}
    # extends:
    # file: hwaccel.yml
    # service: hwaccel
    command: [ "start.sh", "microservices" ]
    volumes:
    - ${UPLOAD_LOCATION}:/usr/src/app/upload
    env_file:
    - .env
    depends_on:
    - redis
    - database
    - typesense
    restart: always

    immich-machine-learning:
    container_name: immich_machine_learning
    image: ghcr.nju.edu.cn/immich-app/immich-machine-learning:${IMMICH_VERSION:-release}
    volumes:
    - model-cache:/cache
    env_file:
    - .env
    restart: always

    immich-web:
    container_name: immich_web
    image: ghcr.nju.edu.cn/immich-app/immich-web:${IMMICH_VERSION:-release}
    env_file:
    - .env
    restart: always

    typesense:
    container_name: immich_typesense
    image: docker.nju.edu.cn/typesense/typesense:0.24.1@sha256:9bcff2b829f12074426ca044b56160ca9d777a0c488303469143dd9f8259d4dd
    environment:
    - TYPESENSE_API_KEY=${TYPESENSE_API_KEY}
    - TYPESENSE_DATA_DIR=/data
    # remove this to get debug messages
    - GLOG_minloglevel=1
    volumes:
    - tsdata:/data
    restart: always

    redis:
    container_name: immich_redis
    image: docker.nju.edu.cn/redis:6.2-alpine@sha256:70a7a5b641117670beae0d80658430853896b5ef269ccf00d1827427e3263fa3
    restart: always

    database:
    container_name: immich_postgres
    image: docker.nju.edu.cn/postgres:14-alpine@sha256:28407a9961e76f2d285dc6991e8e48893503cc3836a4755bbc2d40bcc272a441
    env_file:
    - .env
    environment:
    POSTGRES_PASSWORD: ${DB_PASSWORD}
    POSTGRES_USER: ${DB_USERNAME}
    POSTGRES_DB: ${DB_DATABASE_NAME}
    volumes:
    - pgdata:/var/lib/postgresql/data
    restart: always

    immich-proxy:
    container_name: immich_proxy
    image: ghcr.nju.edu.cn/immich-app/immich-proxy:${IMMICH_VERSION:-release}
    environment:
    # Make sure these values get passed through from the env file
    - IMMICH_SERVER_URL
    - IMMICH_WEB_URL
    ports:
    - 5003:8080
    depends_on:
    - immich-server
    - immich-web
    restart: always

    volumes:
    pgdata:
    model-cache:
    tsdata:

.env不需要改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# You can find documentation for all the supported env variables at https://immich.app/docs/install/environment-variables

# The location where your uploaded files are stored
UPLOAD_LOCATION=./library

# The Immich version to use. You can pin this to a specific version like "v1.71.0"
IMMICH_VERSION=release

# Connection secrets for postgres and typesense. You should change these to random passwords
TYPESENSE_API_KEY=some-random-text
DB_PASSWORD=postgres

# The values below this line do not need to be changed
###################################################################################
DB_HOSTNAME=immich_postgres
DB_USERNAME=postgres
DB_DATABASE_NAME=immich

REDIS_HOSTNAME=immich_redis

  1. 确认docker-compose.yml文件中immich-proxy段落里的ports端口没有问题,启动immich。

    1
    docker compose up -d
  2. 访问 http://{ip}:{port}

根据照片的拍摄时间重新命名照片文件名

这几天一直想给照片搞个备份,在寻找一个性价比比较高而且又比较稳定的方案,现在还没找到,先进行图片整理工作。
整理过程中想把所有的文件名给改一下,于是总结了这个脚本。
需要提前从这里https://exiftool.org/ 下载一个exiftool.exe用于操作图片或者视频的exif信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

$currentDirectory = Get-Location
$imageFiles = Get-ChildItem -Path $currentDirectory -File | Where-Object { $_.Extension -match '\.(jpg|jpeg|png|gif)' }

foreach ($imageFile in $imageFiles) {
$dateTaken = & "D:\exiftool.exe" "-DateTimeOriginal" "-d" "%Y%m%d_%H%M%S" "-s" $imageFile.FullName
$dateTaken = $dateTaken.Trim() -replace ".*: ", ""
if ($dateTaken -ne "") {
$newFileName = "IMG_$dateTaken$($imageFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName
while (Test-Path $newFilePath) {
# 将日期时间字符串解析为日期时间对象
$year = [int]($dateTaken.Substring(0, 4))
$month = [int]($dateTaken.Substring(4, 2))
$day = [int]($dateTaken.Substring(6, 2))
$hour = [int]($dateTaken.Substring(9, 2))
$minute = [int]($dateTaken.Substring(11, 2))
$second = [int]($dateTaken.Substring(13, 2))
$newDate = Get-Date -Year $year -Month $month -Day $day -Hour $hour -Minute $minute -Second $second

# 递增一秒钟
$newDate = $newDate.AddSeconds(1)
$dateTaken = $newDate.ToString("yyyyMMdd_HHmmss")

$newFileName = "IMG_$dateTaken$($imageFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName
}
Rename-Item -Path $imageFile.FullName -NewName $newFilePath -Force
}
Write-Host $imageFile
}

$videoFiles = Get-ChildItem -Path $currentDirectory -File | Where-Object { $_.Extension -match '\.(mp4|mov|avi|mkv)' }

foreach ($videoFile in $videoFiles) {
$mediaCreateTime = & "D:\exiftool.exe" "-MediaCreateDate" "-d" "%Y%m%d_%H%M%S" "-s" $videoFile.FullName
$mediaCreateTime = $mediaCreateTime.Trim() -replace ".*: ", ""

if ($mediaCreateTime -ne "") {
$newFileName = "VID_$mediaCreateTime$($videoFile.Extension)"
$newFilePath = Join-Path -Path $currentDirectory -ChildPath $newFileName

if ($videoFile.Name -ne $newFileName) {
Rename-Item -Path $videoFile.FullName -NewName $newFilePath -Force
}
}
Write-Host $videoFile
}

Write-Host "重命名完成。"

百度网盘读取全部文件列表

代码来源于网上,很遗憾找不到从哪里找到的了。

安装百度云盘的PC版本,BaiduYunCacheFileV0.db这个文件一般放置在百度网盘的安装目录下的user目录下。
这个目录类似于单文件数据SQLite,直接调用sqlite3的python库把数据读取出来,然后转成txt文件就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sqlite3
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.ttk import *


def select_db_file():
db_file = askopenfilename(title="请选择BaiduYunCacheFileV0.db文件", filetypes=[('db', '*.db')])
db.set(db_file)


def select_save_file():
save_file = asksaveasfilename(filetypes=[('文件', '*.txt')])
f.set(save_file + ".txt")


def write_file(file_dict, f, item, gap=""):
if item == "/":
f.write("━" + "/" + "\n")
for i in file_dict["/"]:
f.write("┣" + "━" + i + "\n")
i = item + i + "/"
if i in file_dict:
write_file(file_dict, f, i, gap="┣━")
else:
gap = "┃ " + gap
for i in file_dict[item]:
f.write(gap + i + "\n")
i = item + i + "/"
if i in file_dict:
write_file(file_dict, f, i, gap)


def create_baiduyun_filelist():
file_dict = {}
conn = sqlite3.connect(db.get())
cursor = conn.cursor()
cursor.execute("select * from cache_file")
while True:
value = cursor.fetchone()
if not value:
break
path = value[2]
name = value[3]
size = value[4]
isdir = value[6]
if path not in file_dict:
file_dict[path] = []
file_dict[path].append(name)
else:
file_dict[path].append(name)
with open(f.get(), "w", encoding='utf-8') as fp:
write_file(file_dict, fp, "/")


root = Tk()
root.title('百度云文件列表生成工具')
db_select = Button(root, text=' 选择DB文件 ', command=select_db_file)
db_select.grid(row=1, column=1, sticky=W, padx=(2, 0), pady=(2, 0))
db = StringVar()
db_path = Entry(root, width=80, textvariable=db)
db_path['state'] = 'readonly'
db_path.grid(row=1, column=2, padx=3, pady=3, sticky=W + E)
save_path = Button(root, text='选择保存地址', command=select_save_file)
save_path.grid(row=2, column=1, sticky=W, padx=(2, 0), pady=(2, 0))
f = StringVar()
file_path = Entry(root, width=80, textvariable=f)
file_path['state'] = 'readonly'
file_path.grid(row=2, column=2, padx=3, pady=3, sticky=W + E)
create_btn = Button(root, text='生成文件列表', command=create_baiduyun_filelist)
create_btn.grid(row=3, column=1, columnspan=2, pady=(0, 2))
root.columnconfigure(2, weight=1)
root.mainloop()

flask全局的@app.errorhandler(Exception)无法起效

正常情况下,在能查到的所有文档都会这样表达如何给falsk设置全局的Excepition拦截器。

1
2
3
@app.errorhandler(Exception)
def handle_exception(exc):
pass

但是无论如何,在我的项目中就是无法使用,后来看了一下报出的异常,是从flask_restx打出来的,总是返回internal server error。
于是怀疑是使用了flask_restx或者flask_restful的原因,是这个架构提前抓住了异常没有往上抛出,导致flask根本无法接到异常。
看了一flask_restx的源码,尤其是flask_restx.api.Api.handle_error方法,最后发现在一种判断条件是,flask_restx就会把异常给抛出去了。
于是发现,只需要加个配置就可以了。

1
app.config['PROPAGATE_EXCEPTIONS'] = True

这样问题就解决了。

在多个文件中搜索汉字

之前写代码的过程,用中文加了很多的注释,现在需要找出来,改掉。
可以利用正则表达式来进行搜索, 匹配中文汉字的编码就可以了。

1
^((?!(\*|//)).)+[\u4e00-\u9fa5]