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
//! A crate to asynchronously crawl web pages. This crate was largely inspired by the python module
//! `scrapy`, but updated to take advantage of Rust's safety, performance, and expressiveness.
//!
//! # About
//! This crate is built around the idea of transforming HTTP responses into scraped `Items`
//! through dynamically linked handlers. The caller defines the handlers and the crate takes care
//! of the scheduling.
//!
//! # Basic Example
//!
//!The following example shows the most basic handler chain possible:
//!
//! ```no_run
//! // Needed for the yield keyword to work
//! #![feature(generators)]
//!
//! use futures::stream::StreamExt; // Provides friendly methods for streams
//! use reqwest::{Client, Response};
//! use scrappy_do::{handle, wrap};
//! use url::Url;
//!
//! // This is what we are trying to create from the web pages
//! #[derive(Debug)]
//! struct SomeItem;
//!
//! // This tracks metadata we find useful during scraping.
//! #[derive(Debug)]
//! struct SomeContext;
//!
//! #[handle(item = SomeItem)]
//! fn handler_foo(
//!     client: Client,
//!     response: Response,
//!     context: SomeContext,
//! ) {
//!
//!     // Process the response....
//!
//!     // Return some scraped item
//!     yield SomeItem;
//! }
//!
//! // Sets up multithreaded runtime
//! #[tokio::main]
//! async fn main() {
//!     // Build the HTTP client
//!     let client = reqwest::Client::builder()
//!         .build()
//!         .unwrap();
//!
//!     // Build the spider
//!     let spider = scrappy_do::Spider::new(client.clone());
//!
//!     let items = spider
//!         // A web requires an initial address, handler, and context in order to be created. All
//!         // other configuration is optional.
//!         .web()
//!         .start(
//!             client
//!                 .get(Url::parse("http://somedomain.toscrape.com").unwrap())
//!                 .build()
//!                 .unwrap(),
//!         )
//!         .handler(wrap!(handler_foo))
//!         .context(SomeContext)
//!         .build()
//!         .crawl()
//!         .await;
//!
//!     // The stream must be pinned to the stack to iterate over it
//!     tokio::pin!(items);
//!
//!     // Process the items scraped.
//!     while let Some(item) = items.next().await {
//!         println!("{:?}", item);
//!     }
//! }
//! ```
//!
//! # Chaining Example
//!
//! This example shows a slightly more complicated chain where a handler invokes another handler.
//! The important thing to note is that the caller doesn't change the inital chain invocation at all.
//!
//! ```no_run
//! // Needed for the yield keyword to work
//! #![feature(generators)]
//!
//! use futures::stream::StreamExt; // Provides friendly methods for streams
//! use reqwest::{Client, Response};
//! use scrappy_do::{handle, wrap};
//! use url::Url;
//!
//! // This is what we are trying to create from the web pages
//! #[derive(Debug)]
//! struct SomeItem;
//!
//! // This tracks metadata we find useful during scraping.
//! #[derive(Debug)]
//! struct SomeContext;
//!
//!// The inital handler
//! #[handle(item = SomeItem)]
//! fn handler_foo(
//!     client: Client,
//!     response: Response,
//!     context: SomeContext,
//! ) {
//!
//!     // Process the response....
//!
//!     let callback = scrappy_do::Callback::new(
//!                wrap!(handler_bar),
//!                client
//!                    .get(response.url().clone())
//!                    .build()
//!                    .unwrap(),
//!                context,
//!            );
//!
//!     // Chain the next callback
//!     yield callback;
//!
//!     // Return some scraped item
//!     yield SomeItem;
//! }
//!
//! // Called by handler_foo
//! #[handle(item = SomeItem)]
//! fn handler_bar(
//!     client: Client,
//!     response: Response,
//!     context: SomeContext,
//! ) {
//!
//!     // Process the response....
//!
//!     // Return some scraped item
//!     yield SomeItem;
//! }
//!
//! // Sets up multithreaded runtime
//! #[tokio::main]
//! async fn main() {
//!     // Build the HTTP client
//!     let client = reqwest::Client::builder()
//!         .build()
//!         .unwrap();
//!
//!     // Build the spider
//!     let spider = scrappy_do::Spider::new(client.clone());
//!
//!     let items = spider
//!         // A web requires an initial address, handler, and context in order to be created. All
//!         // other configuration is optional.
//!         .web()
//!         .start(
//!             client
//!                 .get(Url::parse("http://somedomain.toscrape.com").unwrap())
//!                 .build()
//!                 .unwrap(),
//!         )
//!         .handler(wrap!(handler_foo))
//!         .context(SomeContext)
//!         .build()
//!         .crawl()
//!         .await;
//!
//!     // The stream must be pinned to the stack to iterate over it
//!     tokio::pin!(items);
//!
//!     // Process the items scraped.
//!     while let Some(item) = items.next().await {
//!         println!("{:?}", item);
//!     }
//! }
//! ```
//!
//! Ultimately the chain can be as complicated or as simple as the caller wants. It can even be
//! never ending if need be and the `Web` will just keep happily chugging along processing
//! handlers.
//!
pub use scrappy_do_codegen::*;

mod callback;
mod handler;
mod spider;
pub mod util;
pub use callback::{Callback, Indeterminate};
pub use handler::{Handler, HandlerImpl};
pub use spider::{Spider, Web, WebBuilder};

#[doc(hidden)]
pub use tokio::{
    spawn,
    sync::mpsc::{channel, Receiver},
};