This commit is contained in:
Rajnish Mishra 2021-05-12 21:36:09 +05:30
parent ba741dc274
commit 5edad053b3
3 changed files with 18 additions and 8 deletions

View File

@ -52,8 +52,8 @@ async fn main() -> std::io::Result<()> {
) )
.wrap(Logger::new("%t [%{x-forwarded-for}i] %s %{User-Agent}i %r")) .wrap(Logger::new("%t [%{x-forwarded-for}i] %s %{User-Agent}i %r"))
.service(web::resource("/ws/").route(web::get().to(ws_index))) .service(web::resource("/ws/").route(web::get().to(ws_index)))
.service(web::resource("/gif/").route(web::get().to(gif))) .service(web::resource("/gif/{pos}/").route(web::get().to(gif)))
.service(web::resource("/gif/{query}").route(web::get().to(gif))) .service(web::resource("/gif/{pos}/{query}").route(web::get().to(gif)))
.service(fs::Files::new("/", &static_path).index_file("index.html")) .service(fs::Files::new("/", &static_path).index_file("index.html"))
}) })
.bind(config.bind_address)? .bind(config.bind_address)?
@ -67,6 +67,8 @@ async fn ws_index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse
async fn gif(req: HttpRequest) -> Result<HttpResponse, Error> { async fn gif(req: HttpRequest) -> Result<HttpResponse, Error> {
let name = req.match_info().get("query").unwrap_or(""); let name = req.match_info().get("query").unwrap_or("");
let mut pos = req.match_info().get("pos").unwrap_or("");
if pos == "_" { pos = "" }
let builder = SslConnector::builder(SslMethod::tls()).unwrap(); let builder = SslConnector::builder(SslMethod::tls()).unwrap();
let client = Client::builder() let client = Client::builder()
@ -74,7 +76,7 @@ async fn gif(req: HttpRequest) -> Result<HttpResponse, Error> {
.finish(); .finish();
let url = format!("https://g.tenor.com/v1/search?q={}&key={}&limit=20&media_filter=tinygif", name.replace(" ", "+"), TENOR_API_KEY.to_owned()); let url = format!("https://g.tenor.com/v1/search?q={}&key={}&limit=20&media_filter=tinygif&pos={}", name.replace(" ", "+"), TENOR_API_KEY.to_owned(), pos);
let response = client.get(url) let response = client.get(url)
.header("User-Agent", "actix-web/3.0") .header("User-Agent", "actix-web/3.0")
.send() .send()

View File

@ -200,7 +200,8 @@
<div id="gif_area"></div> <div id="gif_area"></div>
<div> <div>
<input type="text" id="gif_search" placeholder="Search" style="width: calc(100% - 4rem); display: inline-flex; margin-top: 3px;"> <input type="text" id="gif_search" placeholder="Search" style="width: calc(100% - 4rem); display: inline-flex; margin-top: 3px;">
<a onclick="$('#gif_clip').addClass('is-hidden');" style="float: right; margin-top: 12px; margin-right: 9px; vertical-align: middle; display:inline-flexbox;"> <a onclick="$('#gif_clip').addClass('is-hidden');"
style="float: right; margin-top: 12px; margin-right: 9px; vertical-align: middle; display:inline-flexbox;">
<img src="img/close.svg" alt="X" style="height: 1.8rem; width: auto; cursor: pointer;"> <img src="img/close.svg" alt="X" style="height: 1.8rem; width: auto; cursor: pointer;">
</a> </a>
</div> </div>

View File

@ -314,20 +314,27 @@ $('#dialog_ok').click(function() {
$("#gif_search").keyup(function(event){ $("#gif_search").keyup(function(event){
if (event.key === 'Enter') { if (event.key === 'Enter') {
event.preventDefault(); event.preventDefault();
loadGif(this.value); $('#gif_area').empty();
positiongif = '_';
querygif = this.value;
loadGif();
} }
}); });
function loadGif(query) { var positiongif = '_';
var querygif = '';
function loadGif() {
var area = $('#gif_area'); var area = $('#gif_area');
area.empty(); $.get('/gif/'+positiongif+'/'+querygif, function(data, status){
$.get("/gif/"+query, function(data, status){
if(status == 'success') { if(status == 'success') {
area.find('[name=more]').remove();
positiongif = data.next;
data.results.forEach(function(result) { data.results.forEach(function(result) {
var gif = result.media[0].tinygif.url; var gif = result.media[0].tinygif.url;
area.append($('<button>', {class: 'button', onclick: 'sendGif("'+encodeURI(gif)+'"); $("#gif_clip").addClass("is-hidden");'}) area.append($('<button>', {class: 'button', onclick: 'sendGif("'+encodeURI(gif)+'"); $("#gif_clip").addClass("is-hidden");'})
.append($('<img>', {src: gif}))); .append($('<img>', {src: gif})));
}); });
if(querygif != '') area.append($('<button>', {name: 'more', onclick: 'loadGif()', style: 'display: block'}).text('show more'));
} }
}); });
} }