bilibili_store_impl.py 15.2 KB
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
# 声明:本代码仅供学习和研究目的使用。使用者应遵守以下原则:  
# 1. 不得用于任何商业用途。  
# 2. 使用时应遵守目标平台的使用条款和robots.txt规则。  
# 3. 不得进行大规模爬取或对平台造成运营干扰。  
# 4. 应合理控制请求频率,避免给目标平台带来不必要的负担。   
# 5. 不得用于任何非法或不当的用途。
#   
# 详细许可条款请参阅项目根目录下的LICENSE文件。  
# 使用本代码即表示您同意遵守上述原则和LICENSE中的所有条款。  


# -*- coding: utf-8 -*-
# @Author  : relakkes@gmail.com
# @Time    : 2024/1/14 19:34
# @Desc    : B站存储实现类
import asyncio
import csv
import json
import os
import pathlib
from typing import Dict

import aiofiles

import config
from base.base_crawler import AbstractStore
from tools import utils, words
from var import crawler_type_var


def calculate_number_of_files(file_store_path: str) -> int:
    """计算数据保存文件的前部分排序数字,支持每次运行代码不写到同一个文件中
    Args:
        file_store_path;
    Returns:
        file nums
    """
    if not os.path.exists(file_store_path):
        return 1
    try:
        return max([int(file_name.split("_")[0])for file_name in os.listdir(file_store_path)])+1
    except ValueError:
        return 1

class BiliCsvStoreImplement(AbstractStore):
    csv_store_path: str = "data/bilibili"
    file_count:int=calculate_number_of_files(csv_store_path)
    def make_save_file_name(self, store_type: str) -> str:
        """
        make save file name by store type
        Args:
            store_type: contents or comments

        Returns: eg: data/bilibili/search_comments_20240114.csv ...

        """
        return f"{self.csv_store_path}/{self.file_count}_{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}.csv"

    async def save_data_to_csv(self, save_item: Dict, store_type: str):
        """
        Below is a simple way to save it in CSV format.
        Args:
            save_item:  save content dict info
            store_type: Save type contains content and comments(contents | comments)

        Returns: no returns

        """
        pathlib.Path(self.csv_store_path).mkdir(parents=True, exist_ok=True)
        save_file_name = self.make_save_file_name(store_type=store_type)
        async with aiofiles.open(save_file_name, mode='a+', encoding="utf-8-sig", newline="") as f:
            writer = csv.writer(f)
            if await f.tell() == 0:
                await writer.writerow(save_item.keys())
            await writer.writerow(save_item.values())

    async def store_content(self, content_item: Dict):
        """
        Bilibili content CSV storage implementation
        Args:
            content_item: note item dict

        Returns:

        """
        await self.save_data_to_csv(save_item=content_item, store_type="contents")

    async def store_comment(self, comment_item: Dict):
        """
        Bilibili comment CSV storage implementation
        Args:
            comment_item: comment item dict

        Returns:

        """
        await self.save_data_to_csv(save_item=comment_item, store_type="comments")

    async def store_creator(self, creator: Dict):
        """
        Bilibili creator CSV storage implementation
        Args:
            creator: creator item dict

        Returns:

        """
        await self.save_data_to_csv(save_item=creator, store_type="creators")

    async def store_contact(self, contact_item: Dict):
        """
        Bilibili contact CSV storage implementation
        Args:
            contact_item: creator's contact item dict

        Returns:

        """

        await self.save_data_to_csv(save_item=contact_item, store_type="contacts")

    async def store_dynamic(self, dynamic_item: Dict):
        """
        Bilibili dynamic CSV storage implementation
        Args:
            dynamic_item: creator's dynamic item dict

        Returns:

        """

        await self.save_data_to_csv(save_item=dynamic_item, store_type="dynamics")


class BiliDbStoreImplement(AbstractStore):
    async def store_content(self, content_item: Dict):
        """
        Bilibili content DB storage implementation
        Args:
            content_item: content item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_content,
                                         query_content_by_content_id,
                                         update_content_by_content_id)
        video_id = content_item.get("video_id")
        video_detail: Dict = await query_content_by_content_id(content_id=video_id)
        if not video_detail:
            content_item["add_ts"] = utils.get_current_timestamp()
            await add_new_content(content_item)
        else:
            await update_content_by_content_id(video_id, content_item=content_item)

    async def store_comment(self, comment_item: Dict):
        """
        Bilibili content DB storage implementation
        Args:
            comment_item: comment item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_comment,
                                         query_comment_by_comment_id,
                                         update_comment_by_comment_id)
        comment_id = comment_item.get("comment_id")
        comment_detail: Dict = await query_comment_by_comment_id(comment_id=comment_id)
        if not comment_detail:
            comment_item["add_ts"] = utils.get_current_timestamp()
            await add_new_comment(comment_item)
        else:
            await update_comment_by_comment_id(comment_id, comment_item=comment_item)

    async def store_creator(self, creator: Dict):
        """
        Bilibili creator DB storage implementation
        Args:
            creator: creator item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_creator,
                                         query_creator_by_creator_id,
                                         update_creator_by_creator_id)
        creator_id = creator.get("user_id")
        creator_detail: Dict = await query_creator_by_creator_id(creator_id=creator_id)
        if not creator_detail:
            creator["add_ts"] = utils.get_current_timestamp()
            await add_new_creator(creator)
        else:
            await update_creator_by_creator_id(creator_id,creator_item=creator)

    async def store_contact(self, contact_item: Dict):
        """
        Bilibili contact DB storage implementation
        Args:
            contact_item: contact item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_contact,
                                         query_contact_by_up_and_fan,
                                         update_contact_by_id, )

        up_id = contact_item.get("up_id")
        fan_id = contact_item.get("fan_id")
        contact_detail: Dict = await query_contact_by_up_and_fan(up_id=up_id, fan_id=fan_id)
        if not contact_detail:
            contact_item["add_ts"] = utils.get_current_timestamp()
            await add_new_contact(contact_item)
        else:
            key_id = contact_detail.get("id")
            await update_contact_by_id(id=key_id, contact_item=contact_item)

    async def store_dynamic(self, dynamic_item):
        """
        Bilibili dynamic DB storage implementation
        Args:
            dynamic_item: dynamic item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_dynamic,
                                         query_dynamic_by_dynamic_id,
                                         update_dynamic_by_dynamic_id)

        dynamic_id = dynamic_item.get("dynamic_id")
        dynamic_detail = await query_dynamic_by_dynamic_id(dynamic_id=dynamic_id)
        if not dynamic_detail:
            dynamic_item["add_ts"] = utils.get_current_timestamp()
            await add_new_dynamic(dynamic_item)
        else:
            await update_dynamic_by_dynamic_id(dynamic_id, dynamic_item=dynamic_item)


class BiliJsonStoreImplement(AbstractStore):
    json_store_path: str = "data/bilibili/json"
    words_store_path: str = "data/bilibili/words"
    lock = asyncio.Lock()
    file_count:int=calculate_number_of_files(json_store_path)
    WordCloud = words.AsyncWordCloudGenerator()


    def make_save_file_name(self, store_type: str) -> (str,str):
        """
        make save file name by store type
        Args:
            store_type: Save type contains content and comments(contents | comments)

        Returns:

        """

        return (
            f"{self.json_store_path}/{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}.json",
            f"{self.words_store_path}/{crawler_type_var.get()}_{store_type}_{utils.get_current_date()}"
        )

    async def save_data_to_json(self, save_item: Dict, store_type: str):
        """
        Below is a simple way to save it in json format.
        Args:
            save_item: save content dict info
            store_type: Save type contains content and comments(contents | comments)

        Returns:

        """
        pathlib.Path(self.json_store_path).mkdir(parents=True, exist_ok=True)
        pathlib.Path(self.words_store_path).mkdir(parents=True, exist_ok=True)
        save_file_name,words_file_name_prefix = self.make_save_file_name(store_type=store_type)
        save_data = []

        async with self.lock:
            if os.path.exists(save_file_name):
                async with aiofiles.open(save_file_name, 'r', encoding='utf-8') as file:
                    save_data = json.loads(await file.read())

            save_data.append(save_item)
            async with aiofiles.open(save_file_name, 'w', encoding='utf-8') as file:
                await file.write(json.dumps(save_data, ensure_ascii=False))

            if config.ENABLE_GET_COMMENTS and config.ENABLE_GET_WORDCLOUD:
                try:
                    await self.WordCloud.generate_word_frequency_and_cloud(save_data, words_file_name_prefix)
                except:
                    pass

    async def store_content(self, content_item: Dict):
        """
        content JSON storage implementation
        Args:
            content_item:

        Returns:

        """
        await self.save_data_to_json(content_item, "contents")

    async def store_comment(self, comment_item: Dict):
        """
        comment JSON storage implementation
        Args:
            comment_item:

        Returns:

        """
        await self.save_data_to_json(comment_item, "comments")

    async def store_creator(self, creator: Dict):
        """
        creator JSON storage implementation
        Args:
            creator:

        Returns:

        """
        await self.save_data_to_json(creator, "creators")

    async def store_contact(self, contact_item: Dict):
        """
        creator contact JSON storage implementation
        Args:
            contact_item: creator's contact item dict

        Returns:

        """

        await self.save_data_to_json(save_item=contact_item, store_type="contacts")

    async def store_dynamic(self, dynamic_item: Dict):
        """
        creator dynamic JSON storage implementation
        Args:
            dynamic_item: creator's contact item dict

        Returns:

        """

        await self.save_data_to_json(save_item=dynamic_item, store_type="dynamics")


class BiliSqliteStoreImplement(AbstractStore):
    async def store_content(self, content_item: Dict):
        """
        Bilibili content SQLite storage implementation
        Args:
            content_item: content item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_content,
                                         query_content_by_content_id,
                                         update_content_by_content_id)
        video_id = content_item.get("video_id")
        video_detail: Dict = await query_content_by_content_id(content_id=video_id)
        if not video_detail:
            content_item["add_ts"] = utils.get_current_timestamp()
            await add_new_content(content_item)
        else:
            await update_content_by_content_id(video_id, content_item=content_item)

    async def store_comment(self, comment_item: Dict):
        """
        Bilibili comment SQLite storage implementation
        Args:
            comment_item: comment item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_comment,
                                         query_comment_by_comment_id,
                                         update_comment_by_comment_id)
        comment_id = comment_item.get("comment_id")
        comment_detail: Dict = await query_comment_by_comment_id(comment_id=comment_id)
        if not comment_detail:
            comment_item["add_ts"] = utils.get_current_timestamp()
            await add_new_comment(comment_item)
        else:
            await update_comment_by_comment_id(comment_id, comment_item=comment_item)

    async def store_creator(self, creator: Dict):
        """
        Bilibili creator SQLite storage implementation
        Args:
            creator: creator item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_creator,
                                         query_creator_by_creator_id,
                                         update_creator_by_creator_id)
        creator_id = creator.get("user_id")
        creator_detail: Dict = await query_creator_by_creator_id(creator_id=creator_id)
        if not creator_detail:
            creator["add_ts"] = utils.get_current_timestamp()
            await add_new_creator(creator)
        else:
            await update_creator_by_creator_id(creator_id, creator_item=creator)

    async def store_contact(self, contact_item: Dict):
        """
        Bilibili contact SQLite storage implementation
        Args:
            contact_item: contact item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_contact,
                                         query_contact_by_up_and_fan,
                                         update_contact_by_id, )

        up_id = contact_item.get("up_id")
        fan_id = contact_item.get("fan_id")
        contact_detail: Dict = await query_contact_by_up_and_fan(up_id=up_id, fan_id=fan_id)
        if not contact_detail:
            contact_item["add_ts"] = utils.get_current_timestamp()
            await add_new_contact(contact_item)
        else:
            key_id = contact_detail.get("id")
            await update_contact_by_id(id=key_id, contact_item=contact_item)

    async def store_dynamic(self, dynamic_item):
        """
        Bilibili dynamic SQLite storage implementation
        Args:
            dynamic_item: dynamic item dict

        Returns:

        """

        from .bilibili_store_sql import (add_new_dynamic,
                                         query_dynamic_by_dynamic_id,
                                         update_dynamic_by_dynamic_id)

        dynamic_id = dynamic_item.get("dynamic_id")
        dynamic_detail = await query_dynamic_by_dynamic_id(dynamic_id=dynamic_id)
        if not dynamic_detail:
            dynamic_item["add_ts"] = utils.get_current_timestamp()
            await add_new_dynamic(dynamic_item)
        else:
            await update_dynamic_by_dynamic_id(dynamic_id, dynamic_item=dynamic_item)