Skip to content

Commit

Permalink
feat: 🎸 bing 多区域
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonjoe committed Feb 18, 2024
1 parent a6d3537 commit aa7e9d0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Binary file removed wallpaper_scrapy/@eaDir/.DS_Store@SynoResource
Binary file not shown.
14 changes: 11 additions & 3 deletions wallpaper_scrapy/wallpaper_scrapy/hugomiddlewares.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import pymysql
import subprocess
from scrapy import signals
from scrapy.utils.project import get_project_settings
from wallpaper_scrapy.mariadb import DBConnectionPool
from jinja2 import Template


Expand All @@ -10,11 +12,11 @@ class HugoMiddleware:
def from_crawler(cls, crawler):
middleware = cls()
crawler.signals.connect(middleware.files_downloaded, signal=signals.item_scraped)
return middleware
return middleware

def files_downloaded(self, item, response, spider):
# 在每次下载完成后触发
self.hugo_gen_html(item, spider)
self.hugo_gen_html(item, spider)

def hugo_gen_html(self, item, spider):
settings = get_project_settings()
Expand All @@ -23,6 +25,13 @@ def hugo_gen_html(self, item, spider):
self.hugo_gen_toml(item, working_directory)
self.hugo_gen(working_directory)
self.push_github(working_directory)
try:
db_pool = DBConnectionPool()
db_pool.insert_wallpaper(item['trivia_id'], item['image_urls'],
item['mkt'], item['platform'])
except pymysql.Error as e:
# 打印异常信息或者记录日志
spider.logger.error(f"Error: {e}")

def hugo_gen_toml(self, item, cwd_path):
# 读取 TOML 模板文件
Expand Down Expand Up @@ -54,4 +63,3 @@ def push_github(self, cwd_path):
subprocess.run(["git", "add", "."], cwd=cwd_path)
subprocess.run(["git", "commit", "-m", "auto add wallpaper"], cwd=cwd_path)
subprocess.run(["git", "push", "origin"], cwd=cwd_path)

3 changes: 2 additions & 1 deletion wallpaper_scrapy/wallpaper_scrapy/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class BingScrapyItem(scrapy.Item):
image_urls = scrapy.Field()
image_paths = scrapy.Field()
md_path = scrapy.Field()
trivia_id = scrapy.Field()
trivia_id = scrapy.Field()
mkt = scrapy.Field()
10 changes: 6 additions & 4 deletions wallpaper_scrapy/wallpaper_scrapy/markdownmiddlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
from datetime import datetime, timezone, timedelta
from scrapy.utils.project import get_project_settings


class MarkdownMiddleware:

@classmethod
def from_crawler(cls, crawler):
middleware = cls()
crawler.signals.connect(middleware.files_downloaded, signal=signals.item_scraped)
return middleware
return middleware

def files_downloaded(self, item, response, spider):
# 在每次下载完成后触发
self.create_md(item, spider)
self.create_md(item, spider)



def create_md(self, item, spider):

# 获取当前时间
Expand All @@ -40,4 +42,4 @@ def create_md(self, item, spider):
![{item['trivia_id']}](/{item['platform']}/{item['trivia_id']}.jpg)"""
print(md_filename)
with open(md_filename, 'w') as f:
f.write(markdown_content)
f.write(markdown_content)

0 comments on commit aa7e9d0

Please sign in to comment.