Skip to main content

StackExchange

Struct StackExchange 

Source
pub struct StackExchange { /* private fields */ }
Expand description

Scrape questions from StackExchange

Implementations§

Source§

impl StackExchange

Source

pub fn new() -> Self

Create a new StackExchange instance with a custom client that generates UA (user-agent in order to avoid getting rate limited by DuckDuckGO).

§Examples
use falion::search::stackexchange;

let se = stackexchange::StackExchange::new();
Source

pub fn with_client(client: Client) -> Self

Create a new StackExchange instance with a provided client. Note: DuckDuckGO will limit your requests if you don’t provide a user-agent.

use falion::search::stackexchange;

let se = stackexchange::StackExchange::with_client(reqwest::Client::new());
Source

pub async fn get_question_content( &self, question_url: &str, ) -> Result<Vec<String>, SeError>

Get the contents of a StackExchange question inside a vector, the first item being the question itself and the rest the answers.

§Arguments
  • question_url - The StackExchange absolute url, specifically like this https://stackexchange.com/questions/[0-9]*/the-question, to the question
§Examples
use falion::search::ddg;
use falion::search::stackexchange;

const STACKEXCHANGE_INVALID: [&str; 2] = [
    "stackexchange.com/questions/tagged",
    "stackexchange.com/tag",
];

let ddg = ddg::Ddg::new();
let se = stackexchange::StackExchange::new();
let link = &ddg.get_links("Rust threading", Some("stackexchange.com/questions/"), Some(true), Some(&STACKEXCHANGE_INVALID), Some(1)).await.unwrap()[0];

let question_content = se.get_question_content(&link).await.unwrap();
§Errors

returns stackexchange::SeError

  • NotSeQuestion - The given url does not correspond to a StackExchange question.
  • InvalidRequest - Reqwest returned an error when processing the request. This can be due to rate limiting, bad internet etc.
  • InvalidResponseBody - The response content you got back is corrupted, usually bad internet.
  • InvalidQuestionContent - Usually this means the content returned by the website is corrupted because it did return 200 OK.
  • ErrorCode - The website returned an error code
Source

pub async fn get_multiple_questions_content( &self, query: &str, limit: Option<usize>, ) -> Result<Vec<(String, JoinHandle<Result<Vec<String>, SeError>>)>, SeError>

Search for stackexchange results using duckduckgo and a provided query. This function will go through ALL of those results and crate a future for each one which will start getting the content asynchronously for ALL of them. Each of this Futures is associated with the title of the question and returned inside a Vec for preserved order.

PLEASE READ: While setting a limit is optional, doing 100 requests to StackExchange at once will probably get you rate limited.

§Arguments
  • query - The query to search for.
  • limit - Optional, but doing 100 requests to StackExchange at once will probably get you rate limited. A recommended value is something like 10 for enough results and still good results.
§Examples
use falion::search::stackexchange;

# async fn run() -> Result<(), stackexchange::SeError> {
let se = stackexchange::StackExchange::new();
let question_content = se
    .get_multiple_questions_content("Rust threading", Some(1))
    .await
    .unwrap();

for q in question_content {
   assert!(!q.1.await.unwrap().unwrap().is_empty())
}
# Ok(())
# }
§Errors

returns stackexchange::SeError;

  • DdgError - error with getting results from DuckDuckGO. (ddg::DdgError)

First error is for duckduckgo, second is for the future hanle, third is for the actual question content

Trait Implementations§

Source§

impl Debug for StackExchange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StackExchange

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more