File size: 5,465 Bytes
5db5d7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "590669bd-a7eb-46b8-b202-fd4a611bef08",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "from datetime import datetime\n",
    "from urllib.parse import urlparse\n",
    "\n",
    "# 遍历所有目录下的 JSON 文件\n",
    "def traverse_and_process(root_dir):\n",
    "    for root, dirs, files in os.walk(root_dir):\n",
    "        for file in files:\n",
    "            if file.endswith('.json'):\n",
    "                input_file = os.path.join(root, file)\n",
    "                process_file(input_file)\n",
    "\n",
    "# 处理单个文件\n",
    "def process_file(input_file):\n",
    "    count = 1\n",
    "    with open(input_file, 'r', encoding='utf-8') as file:\n",
    "        for line in file:\n",
    "            process_line(line, count, input_file)\n",
    "            count += 1\n",
    "\n",
    "def process_line(line, count, input_file):\n",
    "    try:\n",
    "        data = json.loads(line)\n",
    "        # 提取关键数据转为txt\n",
    "        json2txt(data, input_file)\n",
    "        print(f'Processed line {count}')\n",
    "    except json.JSONDecodeError as e:\n",
    "        print(f\"JSONDecodeError: {e}\")\n",
    "    except KeyError as e:\n",
    "        print(f\"KeyError: Missing key {e}\")\n",
    "\n",
    "def json2txt(data, input_file):\n",
    "    try:\n",
    "        danbooru_data = data['danbooru']\n",
    "        \n",
    "        # 提取关键参数\n",
    "        id_value = str(danbooru_data['id'])\n",
    "        tag_general = danbooru_data.get('tag_string_general', '')\n",
    "        tag_character = danbooru_data.get('tag_string_character', '')\n",
    "        tag_copyright = danbooru_data.get('tag_string_copyright', '')\n",
    "        tag_artist = danbooru_data.get('tag_string_artist', '')\n",
    "        tag_meta = danbooru_data.get('tag_string_meta', '')\n",
    "        rating = danbooru_data['rating']\n",
    "        created_at = danbooru_data['created_at']\n",
    "        file_url = danbooru_data['file_url']\n",
    "        \n",
    "        # 格式化标签字符串\n",
    "        def format_tags(tag_string):\n",
    "            tags = tag_string.split()\n",
    "            formatted_tags = ', '.join(tags)\n",
    "            return formatted_tags\n",
    "\n",
    "        tag_general_formatted = format_tags(tag_general)\n",
    "        tag_character_formatted = format_tags(tag_character)\n",
    "        tag_copyright_formatted = format_tags(tag_copyright)\n",
    "        tag_artist_formatted = format_tags(tag_artist)\n",
    "        tag_meta_formatted = format_tags(tag_meta)\n",
    "        \n",
    "        # 构造文件名\n",
    "        filename = f\"danbooru_{id_value}.txt\"\n",
    "        year = datetime.strptime(created_at, \"%Y-%m-%dT%H:%M:%S.%f%z\").year\n",
    "        \n",
    "        # 获取相对文件夹路径\n",
    "        relative_directory = os.path.dirname(input_file)\n",
    "        if not os.path.exists(relative_directory):\n",
    "            os.makedirs(relative_directory)\n",
    "        \n",
    "        # 写入文本文件\n",
    "        file_path = os.path.join(relative_directory, filename)\n",
    "        with open(file_path, 'w', encoding='utf-8') as f:\n",
    "            f.write(f\"{tag_artist_formatted}, \")\n",
    "            f.write(f\"{tag_meta_formatted}, \")\n",
    "            f.write(f\"{tag_general_formatted}, \")\n",
    "            f.write(f\"{tag_character_formatted}, \")\n",
    "            f.write(f\"{tag_copyright_formatted}, \")\n",
    "            f.write(f\"year_{year}, \")\n",
    "            if rating == 'e':\n",
    "                f.write(\"nsfw\")\n",
    "        print(f\"文件 {file_path} 已成功创建并保存相关数据。\")\n",
    "    \n",
    "    except KeyError as e:\n",
    "        print(f\"KeyError: Missing key {e}\")\n",
    "\n",
    "# 设置根目录路径\n",
    "root_dir = 'data2'\n",
    "\n",
    "# 开始遍历和处理\n",
    "traverse_and_process(root_dir)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2ff12cb-b06f-4f4d-82fb-1ed6702d606b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def delete_txt_files(directory):\n",
    "    for root, dirs, files in os.walk(directory):\n",
    "        for file in files:\n",
    "            if file.endswith(\".json\"):\n",
    "                file_path = os.path.join(root, file)\n",
    "                try:\n",
    "                    os.remove(file_path)\n",
    "                    print(f\"Deleted: {file_path}\")\n",
    "                except Exception as e:\n",
    "                    print(f\"Failed to delete {file_path}. Reason: {e}\")\n",
    "\n",
    "# Specify the directory to start from\n",
    "start_directory = './'\n",
    "\n",
    "delete_txt_files(start_directory)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}