Skip to content

Commit

Permalink
add index
Browse files Browse the repository at this point in the history
  • Loading branch information
vn-ki committed Dec 19, 2020
1 parent 7d0b621 commit a76bf50
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
15 changes: 12 additions & 3 deletions src/letterboxd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use thiserror::Error;
pub enum LetterboxdError {
#[error("missing attr {0}")]
HtmlMissingAttr(String),

#[error("user not found {0}")]
UserNotFound(String),
}

pub struct LetterboxdClient {
Expand Down Expand Up @@ -135,10 +138,10 @@ impl LetterboxdClient {
}

#[tracing::instrument(skip(self))]
async fn get_letterboxd_film_by_page(&self, username: &str, page: usize) -> Result<String> {
async fn get_letterboxd_film_by_page(&self, username: &str, page: usize) -> Result<reqwest::Response> {
let url = format!("https://letterboxd.com/{}/films/page/{}", username, page);
debug!("fetching url={}", url);
let text = self.client.get(&url).send().await?.text().await?;
let text = self.client.get(&url).send().await?;
Ok(text)
}

Expand All @@ -149,7 +152,11 @@ impl LetterboxdClient {
// let no_of_pages = self.get_pages(&document)?;
// TODO: this is weird async problem
let no_of_pages = {
let text = self.get_letterboxd_film_by_page(username, 1).await?;
let resp = self.get_letterboxd_film_by_page(username, 1).await?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Err(LetterboxdError::UserNotFound(username.into()).into());
}
let text = resp.text().await?;
let document = Html::parse_document(&text);
self.get_pages(&document)
}?;
Expand All @@ -165,6 +172,8 @@ impl LetterboxdClient {
// get the next
let text = self
.get_letterboxd_film_by_page(username, curr_page)
.await?
.text()
.await?;
let document = Html::parse_document(&text);
for movie in document.select(&selector) {
Expand Down
31 changes: 17 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ use crate::cache::*;
use crate::letterboxd::*;
use anyhow::Result;
use askama::Template;
use std::cmp::Ordering;
use std::collections::HashSet;
use warp::Filter;
use tracing::{info, debug};
use tracing::{debug, info};
use tracing_subscriber;
use std::cmp::Ordering;
use warp::Filter;

#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {}

#[derive(Template)]
#[template(path = "diff.html")]
Expand Down Expand Up @@ -51,13 +54,11 @@ async fn get_diff(user1: &str, user2: &str) -> Result<String> {
.into_iter()
.filter(|x| !watched_by_2.contains(&x.id))
.collect();
diff.sort_by(|a, b| {
match (a.rating, b.rating) {
(Some(r1), Some(r2)) => r2.partial_cmp(&r1).unwrap(),
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(None, None) => Ordering::Equal,
}
diff.sort_by(|a, b| match (a.rating, b.rating) {
(Some(r1), Some(r2)) => r2.partial_cmp(&r1).unwrap(),
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(None, None) => Ordering::Equal,
});
Ok(DiffTemplate {
user1: &user1,
Expand All @@ -78,12 +79,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(s) => Ok(warp::reply::html(s)),
Err(_) => return Err(warp::reject::not_found()),
}
// Ok(warp::reply::html(
// // get_diff(&user1, &user2).await.unwrap_or_else(|err| err.to_string()),
// ))
});
let index = warp::path::end().map(|| -> warp::reply::Html<String> {
return warp::reply::html(IndexTemplate {}.render().unwrap().into());
});

let routes = hello.or(index);

warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;

Ok(())
}
2 changes: 1 addition & 1 deletion templates/diff.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Bulma!</title>
<title>{{ user1 }} vs {{ user2 }}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
</head>
<style>
Expand Down
34 changes: 34 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LB Diff</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<div class="columns is-mobile">
{# <div class="column"></div> #}
<div class="column is-full-mobile is-four-fifths-tablet is-three-quarters-desktop is-two-thirds-widescreen is-two-thirds-fullhd" style="margin: auto">
<h1 class="title is-4">
Movies that <input id="user1" class="input is-1" placeholder="LB username"></input> has watched, but <input id="user2" class="input is-1" placeholder="LB username"></input> hasn't!
</h1>
<p class="subtitle">
<a class="button is-link" onclick="redirect()">Compare</a>
</p>
</div>
{# <div class="column"></div> #}
</div>
</div>
</section>
</body>
<script>
function redirect() {
let user1 = document.getElementById('user1').value;
let user2 = document.getElementById('user2').value;
window.location = "/" + user1 + "/vs/" + user2;
}
</script>
</html>

0 comments on commit a76bf50

Please sign in to comment.