Parse icons from HTML
This commit is contained in:
1 parent
08bdd0a18c
commit
7be7e45493
4 files changed
+38
-5
No files matched your search
@@ -10,15 +10,16 @@ module Manifique
|
||||
self.url = data[:url]
|
||||
self.from_web_manifest = []
|
||||
self.from_html = []
|
||||
self.icons = []
|
||||
end
|
||||
|
||||
def load_from_web_manifest(manifest)
|
||||
[:name, :short_name, :description, :icons,
|
||||
:theme_color, :background_color, :display,
|
||||
:start_url, :scope, :share_target].map(&:to_s).each do |prop|
|
||||
[ :name, :short_name, :description, :icons,
|
||||
:theme_color, :background_color, :display,
|
||||
:start_url, :scope, :share_target ].map(&:to_s).each do |prop|
|
||||
self.send("#{prop}=", manifest[prop]) if manifest[prop]
|
||||
self.from_web_manifest.push(prop)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_json
|
||||
|
||||
@@ -16,6 +16,7 @@ module Manifique
|
||||
|
||||
def fetch_metadata
|
||||
fetch_website
|
||||
|
||||
if manifest = fetch_web_manifest
|
||||
@metadata.load_from_web_manifest(manifest)
|
||||
else
|
||||
@@ -51,6 +52,7 @@ module Manifique
|
||||
parse_title_from_html
|
||||
parse_meta_elements_from_html
|
||||
parse_display_mode_from_html
|
||||
parse_icons_from_html
|
||||
end
|
||||
|
||||
def parse_title_from_html
|
||||
@@ -89,6 +91,27 @@ module Manifique
|
||||
end
|
||||
end
|
||||
|
||||
def parse_icons_from_html
|
||||
icon_links = @html.css("link[rel=icon]")
|
||||
if icon_links.any?
|
||||
icon_links.each do |link|
|
||||
icon = {}
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next if icon["src"].to_s.empty?
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
@metadata.icons.push icon
|
||||
end
|
||||
end
|
||||
|
||||
@metadata.from_html.push "icons" unless @metadata.icons.empty?
|
||||
end
|
||||
|
||||
def get_icon_type(src)
|
||||
extension = src.match(/\.([a-zA-Z]+)$/)[1]
|
||||
return "image/#{extension}"
|
||||
end
|
||||
|
||||
def do_get_request(url)
|
||||
conn = Faraday.new do |b|
|
||||
b.use FaradayMiddleware::FollowRedirects
|
||||
|
||||
+5
@@ -21,6 +21,11 @@
|
||||
<link rel="preload" href="/packs/features/compose-87d8eb2c08790e37389f.js" as="script" type="text/javascript" crossorigin="anonymous">
|
||||
<link rel="preload" href="/packs/features/home_timeline-09ae9683dfa755543815.js" as="script" type="text/javascript" crossorigin="anonymous">
|
||||
<link rel="preload" href="/packs/features/notifications-070ca1d5be465b079719.js" as="script" type="text/javascript" crossorigin="anonymous">
|
||||
<link rel="icon" sizes="512x512" href="/application_icon_x512.png">
|
||||
<link rel="icon" sizes="256x256" href="/application_icon_x256.png">
|
||||
<link rel="icon" sizes="228x228" href="/application_icon_x228.png">
|
||||
<link rel="icon" sizes="196x196" href="/application_icon_x196.png">
|
||||
<link rel="icon" sizes="192x192" href="/application_icon_x192.png">
|
||||
<meta content='blwq_9ticpcja4_ujs5avhcm6suljmbwcybgipkjjyzfkdiz88hpj1mlnj26j7aj0xzd87qwrn0exxplpenstsc=' name='applicationserverkey'>
|
||||
<script id='initial-state' type='application/json'>{"meta":{"streaming_api_base_url":"wss://kosmos.social","access_token":"34e249632c89759445fc6a1dc7153a22a0d693af3c3c7ab51fa53b1cfe1fd3a7","locale":"en","domain":"kosmos.social","admin":"8","search_enabled":false,"invites_enabled":false,"me":"2","unfollow_modal":false,"boost_modal":false,"delete_modal":true,"auto_play_gif":false,"display_sensitive_media":false,"reduce_motion":false},"compose":{"me":"2","default_privacy":"public","default_sensitive":false},"accounts":{"2":{"id":"2","username":"raucao","acct":"raucao","display_name":"râu cao","locked":false,"bot":false,"created_at":"2017-04-06t18:16:03.212z","note":"\u003cp\u003emaking web apps awesome with 5apps. redecentralizing the web with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@remotestorage\" class=\"u-url mention\"\u003e@\u003cspan\u003eremotestorage\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e and \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@kosmos\" class=\"u-url mention\"\u003e@\u003cspan\u003ekosmos\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e. evading winters with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@hackerbeach\" class=\"u-url mention\"\u003e@\u003cspan\u003ehackerbeach\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e.\u003c/p\u003e","url":"https://kosmos.social/@raucao","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","followers_count":327,"following_count":306,"statuses_count":2090,"emojis":[],"fields":[{"name":"home page","value":"\u003ca href=\"https://sebastian.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003esebastian.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"},{"name":"toot origin","value":"\u003ca href=\"https://updates.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003eupdates.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"}]},"8":{"id":"8","username":"kosmos","acct":"kosmos","display_name":"kosmos team","locked":false,"bot":false,"created_at":"2017-04-06t19:45:13.620z","note":"\u003cp\u003ethe people behind \u003ca href=\"https://kosmos.org\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.org\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e and \u003ca href=\"https://kosmos.social\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.social\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","url":"https://kosmos.social/@kosmos","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","followers_count":59,"following_count":9,"statuses_count":54,"emojis":[],"fields":[]}},"media_attachments":{"accept_content_types":[".jpg",".jpeg",".png",".gif",".webm",".mp4",".m4v","image/jpeg","image/png","image/gif","video/webm","video/mp4"]},"settings":{"frequentlyusedemojis":{"blush":1,"confused":1,"the_horns":1,"beers":1,"grinning":1,"heart":2,"zap":1,"point_up":1,"thinking_face":8,"upside_down_face":1,"fearful":1,"point_right":1,"wink":4,"yum":1,"grimacing":1,"ok_hand":2,"smiling_imp":1,"airplane":1,"face_with_rolling_eyes":9,"rocket":3,"money_mouth_face":1,"joy":2,"clap":1,"wave":1,"pray":2,"computer":1,"sweat_smile":2,"sunglasses":2,"clown_face":2},"onboarded":true,"notifications":{"alerts":{"follow":true,"favourite":true,"reblog":true,"mention":true},"shows":{"follow":true,"favourite":true,"reblog":true,"mention":true},"sounds":{"follow":true,"favourite":true,"reblog":true,"mention":true}},"public":{"regex":{"body":""}},"direct":{"regex":{"body":""}},"community":{"regex":{"body":"@twitter.com"}},"skintone":1,"trends":{"show":true},"columns":[{"id":"compose","uuid":"9092b61a-015e-49b8-a511Line truncated
|
||||
<script src="/packs/application-485a7a7977f02013f2fa.js" crossorigin="anonymous"></script>
|
||||
|
||||
@@ -166,10 +166,14 @@ RSpec.describe Manifique::WebClient do
|
||||
expect(subject.description).to eq("A friendly place for tooting")
|
||||
expect(subject.theme_color).to eq("#282c37")
|
||||
expect(subject.display).to eq("standalone")
|
||||
|
||||
png_icons = subject.icons.select{|i| i["type"] == "image/png"}
|
||||
expect(png_icons.length).to eq(5)
|
||||
expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
|
||||
end
|
||||
|
||||
it "knows which properties were loaded from HTML" do
|
||||
%w{ name description theme_color display }.each do |property|
|
||||
%w{ name description theme_color display icons }.each do |property|
|
||||
expect(subject.from_html).to include(property)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user