-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jack Deng <[email protected]>
- Loading branch information
1 parent
e0e7549
commit 422015e
Showing
9 changed files
with
286 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
from peewee import CharField | ||
from ruia import AttrField, Item, Response, Spider, TextField | ||
|
||
from ruia_peewee_async import RuiaPeeweeInsert, RuiaPeeweeUpdate, TargetDB, after_start | ||
|
||
|
||
class DoubanItem(Item): | ||
target_item = TextField(css_select="tr.item") | ||
title = AttrField(css_select="a.nbg", attr="title") | ||
url = AttrField(css_select="a.nbg", attr="href") | ||
|
||
async def clean_title(self, value): | ||
return value.strip() | ||
|
||
|
||
class DoubanSpider(Spider): | ||
start_urls = ["https://movie.douban.com/chart"] | ||
# aiohttp_kwargs = {"proxy": "http://127.0.0.1:7890"} | ||
|
||
async def parse(self, response: Response): | ||
async for item in DoubanItem.get_items(html=await response.text()): | ||
yield RuiaPeeweeInsert(item.results) # default is MySQL | ||
# yield RuiaPeeweeInsert(item.results, database=TargetDB.POSTGRES) # save to Postgresql | ||
# yield RuiaPeeweeInsert(item.results, database=TargetDB.BOTH) # save to both MySQL and Postgresql | ||
|
||
|
||
class DoubanUpdateSpider(Spider): | ||
start_urls = ["https://movie.douban.com/chart"] | ||
|
||
async def parse(self, response: Response): | ||
async for item in DoubanItem.get_items(html=await response.text()): | ||
res = {} | ||
res["title"] = item.results["title"] | ||
res["url"] = "http://whatever.youwanttoupdate.com" | ||
yield RuiaPeeweeUpdate( | ||
res, | ||
{"title": res["title"]}, | ||
database=TargetDB.POSTGRES, # default is MySQL | ||
) | ||
|
||
# Args for RuiaPeeweeUpdate | ||
# data: A dict that's going to be updated in the database. | ||
# query: A peewee query or a dict to search for the target data in database. | ||
# database: The target database type. | ||
# create_when_not_exists: If True, will create a record when data not exists. Default is True. | ||
# only: A list or tuple of fields that should be updated. | ||
|
||
|
||
mysql = { | ||
"host": "127.0.0.1", | ||
"port": 3306, | ||
"user": "ruiamysql", | ||
"password": "abc123", | ||
"database": "ruiamysql", | ||
"model": { | ||
"table_name": "ruia_mysql", | ||
"title": CharField(), | ||
"url": CharField(), | ||
}, | ||
} | ||
postgres = { | ||
"host": "127.0.0.1", | ||
"port": 5432, | ||
"user": "ruiapostgres", | ||
"password": "abc123", | ||
"database": "ruiapostgres", | ||
"model": { | ||
"table_name": "ruia_postgres", | ||
"title": CharField(), | ||
"url": CharField(), | ||
}, | ||
} | ||
|
||
if __name__ == "__main__": | ||
DoubanSpider.start(after_start=after_start(mysql=mysql)) | ||
# DoubanSpider.start(after_start=after_start(postgres=postgres)) | ||
# DoubanSpider.start(after_start=after_start(mysql=mysql, postgres=postgres)) | ||
# DoubanUpdateSpider.start(after_start=after_start(mysql=mysql)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "ruia-peewee-async" | ||
version = "0.1.0" | ||
version = "1.0.0" | ||
description = "A Ruia plugin that uses the peewee-async to store data to MySQL" | ||
authors = ["Jack Deng <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.