forked from fanzeyi/lares
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support finding feed URL from HTML page
- Loading branch information
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,56 @@ | ||
/// Finds Feed urls on a web page. | ||
use quick_xml::events::Event; | ||
use quick_xml::Reader; | ||
use std::io::BufRead; | ||
|
||
use crate::error::Result; | ||
|
||
/// Parses HTML page to find `<link rel="alternate" />` and extract hrefs. | ||
pub fn find_rel_alternates<B: BufRead>(reader: B) -> Result<Vec<String>> { | ||
let mut reader = Reader::from_reader(reader); | ||
reader.check_end_names(false); | ||
|
||
let mut buf = Vec::new(); | ||
let mut result = Vec::new(); | ||
|
||
loop { | ||
match reader.read_event(&mut buf) { | ||
Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) if e.name() == b"link" => { | ||
if e.attributes().fold(false, |acc, attr| { | ||
if acc { | ||
acc | ||
} else if let Ok(attr) = attr { | ||
attr.key == b"rel" && attr.value.as_ref() == b"alternate" | ||
} else { | ||
false | ||
} | ||
}) { | ||
if let Some(url) = e | ||
.attributes() | ||
.filter_map(|attr| { | ||
if let Ok(attr) = attr { | ||
if attr.key == b"href" { | ||
String::from_utf8(attr.value.into_owned()).ok() | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.next() | ||
{ | ||
result.push(url); | ||
} | ||
} | ||
} | ||
Ok(Event::Eof) => break, | ||
Err(e) => { | ||
return Err((e, reader.buffer_position()).into()); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
Ok(result) | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ mod error; | |
mod api; | ||
mod cli; | ||
mod crawler; | ||
mod find; | ||
pub mod model; | ||
mod opml; | ||
mod remote; | ||
|
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