Created
October 1, 2015 12:15
-
-
Save RhaDaX/71736174441a2ec59988 to your computer and use it in GitHub Desktop.
Scrapr
This file contains hidden or 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
def load | |
request = params[:q] | |
url = Url.new() | |
url.url = request | |
html = Nokogiri::HTML(open(request)) | |
# Sélection dans la variable @url.title, de la balise h2 si la h1 est absente | |
if html.css('h1') == nil | |
url.title = html.css('h2').text | |
else | |
url.title = html.css('h1').text | |
end | |
@title = url.title | |
# Sélection d'une image valable | |
html.css('img').each do |i| | |
# Trier l'url est ajouter l'adresse du site au répertoire asset et images | |
if i['src'].start_with?("/assets") || i['src'].start_with?("/im") | |
base_url = request.split("/")[2] | |
i['src'] = "http://" + base_url + i['src'] | |
else | |
end | |
size_min = FastImage.size(i['src']) | |
# Sélectionner la 1er image ayant pour largeur un minimum de 300px | |
if size_min.present? && size_min[0] >= 300 | |
#img_size = FastImage.size(i['src']) | |
url.img_src = i['src'] | |
break | |
else | |
@img = nil | |
end | |
@img = url.img_src | |
url.user_id = current_user.id | |
url.save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment