Get og:image from url using JSoup - coldfusion

I don't know what I am doing wrong here. I am tring to get the og:image url using JSOUP and Coldfusion.
<cfhttp method="get" url="http://www.bbc.com/culture/story/20150304-is-house-of-cards-worth-watching" result="theresult">
<cfscript>
// Create the jsoup object
Jsoup = createObject("java", "org.jsoup.Jsoup");
// HTML string
html = "#theresult.filecontent#";
// Parse the string
document = Jsoup.parse(html);
// Extract content
title = document.title();
metaOgImage = document.select("meta[property=og:image]").first();
writeOutput("
<div>Title: #title#</div>
<div>Meta: #metaOgImage#</div>
");
</cfscript>

metaOgImage = document.select("meta[property=og:image]").first();
The returns an Element representing the <meta> tag. To display only the "content" attribute (which is where that page stores the url), try:
<div>Meta: #metaOgImage.attr("content")#</div>
Keep in mind metaOgImage could be null, if it wasn't found, so be sure to add handling for that in the CF code.

Related

ColdFusion Dropbox - How to get the token from the response URI

I am trying to implement the OAuth with Dropbox from a ColdFusion application, and I managed how to call the Dropbox method to generate the access token, but... I don't know how to get the generated TOKEN from the response URI. I am getting something like this from Dropbox:
http://localhost/dropbox/generate_token.cfm#access_token=AAAAAAAAYVM_XdCYlbTz0gQOwQkWlg6TDXf84_5h4giikg6J-7Man&token_type=bearer&uid=267693&account_id=dbid%3AAABeDMm-BN0n1DofLZz9kPZAipnQ
How to I retrieve the URL variables in this case? I mean if I do a
<cfdump var="#URL#">
I am getting an empty struct. If I do a
<cfdump var="#CGI#">
I still don't see any of the URL retrieved parameters in the structure. How do I get the variables and their values from the Dropbox response?
UPDATED
At some point I thought I found a way to read the URL but now - for no reason - this doesn't work anymore! I didn't change anything but the solution below doesn't work anymore.
I can read the full URL with JavaScript using document.location but this means to do an extra submit to a ColdFusion page and I don't want to do this. I want to get the Dropbox token from the URL and save it to the database directly in this page...
Any new ideas please?
SOLUTION THAT SEEMED TO WORK AT SOME POINT ...
I found a way to get the URI string using this:
<cfset objRequest = GetPageContext().GetRequest().getParameterMap() />
<cfdump var="#objRequest#">
<cfoutput>
<cfloop collection="#objRequest#" item="i">
<p>
#i# - #objRequest[i][1]#
</p>
</cfloop>
</cfoutput>
From now on, I know how to get the values returned by Dropbox.
I found a way to get the returned parameters by reading the browser URL with JavaScript, so in two steps: first, parse and extract the full URL including the part after the # sign (I found this has a name and it is called the "URL fragment") and second, create a JavaScript form with parsed parameters and resubmitted to the server. Here is the code:
<cfparam name="FORM.action" default="">
<cfif FORM.action IS "save_token">
<cfdump var="#FORM#">
<cfelse>
<form name="main" id="main" method="post">
<input type="hidden" name="action" id="action" value="save_token">
</form>
<script type="text/javascript" language="javascript">
<!--
var parameters = window.location.hash.substr(1).split("&");
function addHidden(theForm, key, value) {
// Create a hidden input element, and append it to the form:
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = value;
theForm.appendChild(input);
}
// Form reference:
var theForm = document.forms["main"];
for (var i=0; i<parameters.length; i++) {
// Add data:
addHidden(theForm, parameters[i].split("=")[0], parameters[i].split("=")[1]);
}
theForm.submit();
//-->
</script>
</cfif>

How to get the filename from a cfhttp get request?

If I run the following code to get an image from Medium's site:
<cfhttp url="https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png"
method="get" useragent="#CGI.http_user_agent#" getasbinary="yes">
I then want to save the image with the same name that they used i.e. 1*3j1McX-y1rvKewzI2gWc_w.png.
How can I get the name of the file from the cfhttp request? I looked in the cfhttp.header for any sign of the content-disposition attribute but can't find it.
Assuming you are getting these URLs dynamically, why not just parse it for the filename first, then apply that to the filename attribute?
<cfset filename1 = ListLast("https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png","/") />
<cfhttp url="https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png"
method="get" useragent="#CGI.http_user_agent#" getasbinary="yes" path="whateverpath" filename="#filename1#>

URL replacement for Javascript code in ColdFusion

I have inherited a external page where I have no control:
I have javascript sorting on that page: like http://www.exampledomain.com/javascript:void(1);
Now it has many links like this, the 1 you see is dynamic, what I want to is: convert this code to ColdFusion URL like http://www.exampledomain.com/sor=1&sort=asc & desc. The 1 should work as it is, like it should keep its value as it is 1,2,3,4 etc. I tried to do this with jQuery.
How can I alter these links in ColdFusion?
I tried to come up with some of Javascript solution but it did not work
$('#container').find('a').attr('href', function(i, old) {
var col = decodeURIComponent(old).match(/javascript:\s*sort\((.*?)\)/)[1];
return hrefcall+data+'&sortBy='+col;
Thanks
Your question is unclear, the source of the data is unknown and there are a few typoes.
This JQuery (1.9.1)
var ihref = "";
var col = 'somedata';
$("a").click(function (i) {
ihref = $(this).attr('href');
if (ihref.match(/javascript:\s*sort\(\d+\).*/i)) {
ihref = ihref.replace(/javascript:\s*sort\((\d+)\).*/i,"http://www.w3schools.com/sor=$1&sortBy=" + col);
$(this).attr('href',ihref);
alert("As a demonstration, you\'ll see the link is rewritten when a javascript:sort url is clicked.");
}
});
Will convert all javascript:void links as you wanted. I did change the dummy url to w3schools.com because the good folks who own w3 permit their site to be loaded in Iframes which is necessary to easily demonstrate that this code works.
Of course, JQuery only works when JS is enabled. Still, since you started with JQuery, I thought I might show you a working demonstration.
(The links don't actually work, because w3schools doesn't have pages at those points, but you can see in the status bar, the links are rewritten).
If you're retrieving the page via cfhttp.filecontent, you can do something like this
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sort=asc","ALL")>
The ReReplaceNoCase() was tested against this sample code..
<cfset cfhttp = {} />
<cfset col = "somedata" />
<cfsavecontent variable="cfhttp.filecontent">
test - will not alter url<Br />
test - will alter url<Br />
test - will alter url<Br />
test - will not alter url<Br />
</cfsavecontent>
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sortBy=#col#","ALL")>
<cfdump var="#cfhttp#">

replace %25 in url with space using coldfusion using cfhttp

I am making a cfhttp get call to another page. I am passing the url variable using cfhttpparam as shown below. But when I run the page, the url is rendered as shown in the image. I need to replace %25 to be able to get the correct url string. Can someone tell me what is wrong with the code?
<cfset vpName = "Abc def F hig K xyz" /> I want %20 in the spaces in the name here. But it is rending as show in the image![enter image description here][1]
<cfset urlvar = URLEncodedFormat("#vpName#")>
<!--- <cfoutput>#urlvar#</cfoutput>
--->
<cfhttp url="https://abc.com/xyz/EM2/LTMR.cfm" method="get" username="abcd" password="password" >
<cfhttpparam type="url" name="LTMX" value="#urlvar#">
</cfhttp>
<cfset myDocument = cfhttp.fileContent>
<cfoutput>#myDocument#</cfoutput>
URL is rendered as
abc.com/LTMR.cfm?LTMX=Andre%2520Fuetsch%2520%2520F%2520Shelly%2520K%2520Lazzaro
The %25 is what the "URLEncodedformat()" is supposed to do - replace spaces (etc) with the appropriate encoded sequence - and as Peter said the <cfhttpparam> does this automatically - so you should change this;
<cfset urlvar = URLEncodedFormat("#vpName#")>
to be this...
<cfset urlvar = vpName/>
Although you could of course simply pass in the vpName instead of creating a completely separate variable for it.

Replace url in all hrefs inside body tags

I am using cfhttp to get a website . I want to replace all the links inside the body tags. Importantly I don't want to mess up the stylesheets etc in the head.
I want to do the following:
In the external web page body we may find a link:
External Link
I want to replace it with the following:
External Link
Its easy enough using Replace() but then I also replace all the linked stylesheets etc. I just want to edit the href's of clickable links.
I've modified an HTML document's DOM to add tracking parameters to links in outbound email messages using the jsoup library. (jsoup is an open source Java HTML Parser and can be download at http://jsoup.org/.) You'll note that it uses jQuery-like select methods, but all manipulations are performed on the server-side (I've also used it for removing ads from CFHTTTP-fetched HTML.)
Here's a quick sample of working ColdFusion code that will do exactly what you want on the server-side:
<CFSET TheHTML = CFHTTP.FileContent>
<CFSET jsoup = CreateObject("java", "org.jsoup.Jsoup")>
<CFSET TempHTML = jsoup.parse(TheHTML)>
<CFLOOP ARRAY="#TempHTML.select('a')#" INDEX="ThisLink">
<CFSET TheLink = thisLink.attr("href").toString()>
<CFSET TheHTML = replace(TheHTML, TheLink, "http://mywebsite.com/?u=" & URLEncodedFormat(TheLink))>
</CFLOOP>