Struct rustube::VideoFetcher [−][src]
A fetcher used to download all necessary data from YouTube, which then could be used to extract video-URLs.
You will probably rarely use this type directly, and use Video
instead.
Example
const URL: &str = "https://youtube.com/watch?iv=5jlI4uzZGjU"; let url = Url::parse(URL).unwrap(); let fetcher: VideoFetcher = VideoFetcher::from_url(&url).unwrap();
How it works
So you want to download a YouTube video? You probably already noticed, that YouTube makes this quite hard, and does not just provide static URLs for their videos. In fact, there's not the one URL for each video. When currently nobody is watching a video, there's actually no URL for this video at all!
So we need to somehow show YouTube that we want to watch the video, so the YouTube server
generates a URL for us. To do this, we do what every 'normal' human being would do: we
request the webpage of the video. To do so, we need nothing more, then the video's id (If you
want to learn more about the id, you can have a look at Id
. But you don't need to know
anything about it for now). Let's, for example, take the id '5jlI4uzZGjU'. With this id, we
can then visit https://youtube.com/watch?v=5jlI4uzZGjU, the site, you as a human would visit
when just watching the video.
The next step is to extract as much information from https://youtube.com/watch?v=5jlI4uzZGjU as possible. This is, i.e., information like "is the video age-restricted?", or "can we watch the video without being a member of that channel?".
But there's information, which is a lot more important then knowing if we are old enough to watch the video: The VideoInfo
, the PlayerResponse
and the JavaScript of the
page. VideoInfo
and PlayerResponse
are JSON objects, which contain the most
important information about the video. If you are feeling brave, feel free to have a look
at the definitions of those two types, their subtypes, and all the information they contain
(It's huge!). The JavaScript is not processed by fetch
, but is used later by
VideoDescrambler::descramble
to generate the transform_plan
and the transform_map
(you will learn about both when it comes to descrambling).
To get the videos VideoInfo
, we actually need to request one more page. One you probably
don't frequently visit as a 'normal' human being. Because we, programmers, are really
creative when it comes to naming stuff, a video's VideoInfo
can be requested at
https://youtube.com/get_video_info. Btw.: If you want to see how the computer feels, when
we ask him to deserialize the response into the VideoInfo
struct, you can have a look
at https://www.youtube.com/get_video_info?video_id=5jlI4uzZGjU&eurl=https%3A%2F%2Fyoutube.com%2Fwatch%3Fiv%3D5jlI4uzZGjU&sts=
(most browsers, will download a text file!). This is the actual VideoInfo
for the
video with the id '5jlI4uzZGjU'.
That's it! Of course, we cannot download the video yet. But that's not the task of fetch
.
fetch
is just responsible for requesting all the essential information. To learn how the
journey continues, have a look at VideoDescrambler
.
Implementations
impl VideoFetcher
[src]
pub fn from_url(url: &Url) -> Result<Self>
[src]
fetch
and regex
only.Constructs a VideoFetcher
from an Url
.
Errors
- When
Id::from_raw
fails to extracted the videos id from the url. - When
reqwest
fails to initialize an newClient
.
pub fn from_id(video_id: IdBuf) -> Result<Self>
[src]
fetch
only.pub fn from_id_with_client(video_id: IdBuf, client: Client) -> Self
[src]
fetch
only.Constructs a VideoFetcher
from an Id
and an existing Client
.
There are no special constrains, what the Client
has to look like.
pub async fn fetch(self) -> Result<VideoDescrambler>
[src]
fetch
only.Fetches all available video data and deserializes it into VideoInfo
.
Errors
- When the video is private, only for members, or otherwise not accessible.
- When requests to some video resources fail.
- When deserializing the raw response fails.
When having a good internet connection, only errors due to inaccessible videos should occur.
Other errors usually mean, that YouTube changed their API, and rustube
did not adapt to
this change yet. Please feel free to open a GitHub issue if this is the case.
pub fn video_id(&self) -> Id<'_>
[src]
fetch
only.The id of the video.
pub fn watch_url(&self) -> &Url
[src]
fetch
only.The url, under witch the video can be watched.
Trait Implementations
impl Clone for VideoFetcher
[src]
fetch
only.fn clone(&self) -> VideoFetcher
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for VideoFetcher
[src]
fetch
only.impl Display for VideoFetcher
[src]
fetch
only.impl Eq for VideoFetcher
[src]
fetch
only.impl PartialEq<VideoFetcher> for VideoFetcher
[src]
fetch
only.Auto Trait Implementations
impl !RefUnwindSafe for VideoFetcher
[src]
impl Send for VideoFetcher
[src]
impl Sync for VideoFetcher
[src]
impl Unpin for VideoFetcher
[src]
impl !UnwindSafe for VideoFetcher
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
pub fn equivalent(&self, key: &K) -> bool
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,