You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mix.install([# HTTP request helper{:finch,"~> 0.14"},# HTML Parser{:floki,"~> 0.34.0"}])
Section
Finch.start_link(name: MyFinch)
# Get all the links from hcrknews page (which gives us the top x links)url="https://hckrnews.com/"{:ok,response}=Finch.build(:get,url)|>Finch.request(MyFinch){:ok,document}=Floki.parse_document(response.body)links_hn=Floki.find(document,"a.link")|>Enum.map(fn{a,[b,{"href",link}],_}->linkend)
defmoduleKiruHelperdodeflink_to_meta_tag(link)doIO.puts(link)# Do http requestcaseFinch.build(:get,link)|>Finch.request(MyFinch)do{:ok,response}-># parse HTMLFloki.parse_document(response.body)|>extract_meta_tags_->[]endenddefextract_meta_tags({:ok,document})do# try to find a <head> tagcaseFloki.find(document,"head")|>Enum.at(0)do# nothign foundnil->[]# found head{"head",_,innerHead}-># we are only interested in meta tagsmeta_tags=innerHead|>Enum.filter(&is_tuple/1)|>Enum.filter(fneach->Tuple.to_list(each)|>Enum.count()==3end)|>Enum.filter(fn{name,_,_}->String.downcase(name)=="meta"end)# get the meta tags names out of each tag meta_tags|>Enum.map(fn{"meta",attributes,_}->r=attributes|>Enum.map(fn{attr_name,attr_value}-># we are only interested in certain tagscaseString.downcase(attr_name)do"property"->{"name",attr_value}"name"->{"name",attr_value}"content"->{"content",attr_value}"value"->{"value",attr_value}_->nilendend)|>Enum.reject(&is_nil/1)end)endenddefextract_meta_tags(_),do: []defextract_titles(meta_tags)doog_title=meta_tags|>Enum.map(fneach->found=Enum.find(each,fn{key,value}->value=="og:title"end)casefounddonil->nil_->each|>Enum.find(each,fn{key,value}->key=="content"end)|>casedonil->nil{"content",value}->valueendendend)|>Enum.reject(&is_nil/1)|>Enum.at(0)twitter_title=meta_tags|>Enum.map(fneach->found=Enum.find(each,fn{key,value}->value=="twitter:title"end)casefounddonil->nil_->each|>Enum.find(each,fn{key,value}->key=="content"end)|>casedonil->nil{"content",value}->valueendendend)|>Enum.reject(&is_nil/1)|>Enum.at(0){og_title,twitter_title}endend