How can RWordPress retrieve blog post content? - r

I would like to retrieve the content of posts from my WordPress blog. Using the package RWordPress it is straightforward to retrieve categories and tags and titles, but what about the content of posts?
# Download and load the package
if (!require('RWordPress')) {
devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
library(RWordPress)
# list all the functions in the package
lsf.str("package:RWordPress")
Here, for example, is the code to obtain categories, with my specifics redacted in brackets:
Cat <- getCategoryList(login = c([my user name] = '[my password'),
.server = 'http://[my blog on].wpengine.com/xmlrpc.php')
The linked SO question is not applicable as it doesn't use RWordPress [HTML and CSS and PHP coding].
This site is about posting on WordPress, not retrieving from WordPress [publishing, not gettting]. Another question uses xmlrpc as does RWordPress and a getPosts call, but it does not rely on R.
Posts <- getPosts(num = 100, blogid = 0L, login = c([my user name] = '[my password]'), .server = 'http://[my blog name].wpengine.com/xmlrpc.php')
The above code returns dates and titles and status, but not content.
Thank you for any guidance.
******************* Edit after first answer
After requiring RWordPress and XMLRPC, and then defining an object for login and for the .server, here is the console message:
> getPageList(blogid = 0L, login = WordpressLogin, .server = WordpressURL)
Error in xml.rpc(.server, op, as.integer(blogid), names(login), as.character(login), :
Problems
I find that "Problems" is not an informative error message for me.

Tell me if I am missing something, but for me the description identifier of posts seem to deliver the whole text.
RWordpress maps all the functions in XML-RPC wp
if (!require('RWordPress')) {
devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
library(RWordPress)
options(WordpressLogin = c(myusername = 'mypassword'),
WordpressURL = 'http://localhost/myblog/wordpress/xmlrpc.php')
# library(knitr)
# can refer this page
# http://codex.wordpress.org/XML-RPC_wp
#Rwordpress has a one to one mapping
getCategories()
#get a list of pages
getPageList()
# pick one id from above list
id=27
getPage(pageid = id)
# description seems to have all the text of post, even though the
# document is sparse
getPage(pageid = id)$description
#similarly for posts
getPost(postid = 6)$description
I am of course using a locally installed blog, but I'd reckon this should work remotely.

Related

Show "Microsoft Active Directory" information table in WordPress

I have a spreadsheet in Microsoft Active Directory
Now I want to display the information in this table in WordPress
I came across this plugin according to the searches I did
But this plugin also does not have the ability to display information registered in Active Directory
What method or plugin do you think I should use to display Active Directory registered information in WordPress?
You have to call the Graph API to display user informatiom. This document is a good starting point.For example:
public function loadViewData()
{
$viewData = [];
// Check for flash errors
if (session('error')) {
$viewData['error'] = session('error');
$viewData['errorDetail'] = session('errorDetail');
}
// Check for logged on user
if (session('userName'))
{
$viewData['userName'] = session('userName');
$viewData['userEmail'] = session('userEmail');
$viewData['userTimeZone'] = session('userTimeZone');
}
return $viewData;
}
Please let me know if you have any questions.
Best,
James

Publish posts with custom post type from R via RWordpress and knit2wp

I am currently using the RWordpress-package from Duncan Temple Lang and the knitr-package from Yihui Xie to generate blog posts directly from R. For regular posts that works fine, however I would like to generate a post with a previously custom-made post-type. Via knit2wp i seem only to generate a regular new post, edit a post already published or generate a new page.
If I'd like to write a post by hand I would visit a page within the Wordpress-backend. For a regular post that would be
https://www.your-wordpress.blog/wp-admin/post-new.php
For the custom-made post that would be
https://www.your-wordpress.blog/wp-admin/post-new.php?post_type=custom
So my suggestion is that I have to send some additional information with the action-argument sent over the knit2wp-function of knitr.
The function-call of knit2wp is defined as follows:
knit2wp(input, title = "A post from knitr", ..., envir = parent.frame(),
shortcode = FALSE, action = c("newPost", "editPost", "newPage"), postid,
encoding = getOption("encoding"), publish = TRUE)
After defining the arguments sent to Wordpress via
WPargs = list(content = list(description = content, title = title,
...), publish = publish)
the call itself is done:
do.call("library", list(package = "RWordPress", character.only = TRUE))
do.call(action, args = WPargs)
The information Wordpress provides hints me at a struct field called enclosure. My idea was thus to include a list named enclosure:
WPargs = list(content = list(description = content, title = title,
...), enclosure = list(type = "custom"), publish = publish)
which leads unfortunately to an error message:
unused argument (enclosure = list(type = "custom", categories = c("test1", "test2"), wp_post_thumbnail = 12345))
I assume that I can include the post-type correctly if I modify some calls from the XMLRPC-package but I dont know where to start. Does someone has any idea how to generate custom types of posts via R in Wordpress?
Perhaps not direct an answer but I found a solution using curl-commands (cf Media Api Reference of WordPress). In that way I sent a command simply as a system call. I concatenate several strings up to a curl command, eg:
header<- "--header 'Authorization: Basic your_token_here'"
title<- "'title=Some title here'"
excerpt<- "-d 'Some excerpt here'"
url <- "-d 'slug=some-customized-url-structure-here'")
command<-paste("curl ",header," -X POST -d ",title," -d 'status=publish' -d 'categories=12345' -d 'content= here goes the content' -d 'featured_media=xxxyyy' -d 'author=zzzz' ",url," ",excerpt," https://www.your-ur.l/wp-json/wp/v2/customized_structure_update",sep="")
If I then fire up
system(command)
Everything works fine.

Edit an existing Pastebin document via API

I am trying to write a function in LUA to edit my pastebin code.
I can use this code to make a HTTP post:
http.post(string url, string postData [, table headers])
I can also use this code for a HTTP get:
http.get(string url [, table headers])
on the Pastebin website https://pastebin.com/api are information about using the API.
I am not sure, if this website can help me to solve my problem.
Does someone know how to fill the headers table?
this is the program i tried:
headers = {}
headers["api_dev_key"]= 'myDevKey...'; // your api_developer_key
headers["api_paste_code"] = 'my edited text'; // your paste text
headers["api_paste_private"] = '0'; // 0=public 1=unlisted 2=private
headers["api_paste_name"] = 'justmyfilename.php'; // name or title of your paste
headers["api_paste_expire_date"] = '10M';
headers["api_paste_format"] = 'php';
headers["api_user_key"] = ''; // if an invalid or expired api_user_key is used, an error will spawn. If no api_user_key is used, a guest paste will be created
headers["api_paste_name"] = myPastebinName;
headers["api_paste_code"] = myPastebinPassword;
http.get("https://pastebin.com/edit/dQMDfbkM", headers)
unfortunately this is not working and on the pastebin API help site is no exsample for editing a paste. just for creating a new one.
for me it is also not clear if I have to use post or get
There is no API for editing pastes directly.
You can only delete old paste and create new with updated text.

Scraping Javascript generated data

I'm working on a project with the World Bank analyzing their procurement processes.
The WB maintains websites for each of their projects, containing links and data for the associated contracts issued (example). Contract-related data is available under the procurement tab.
I'd like to be able to pull a project's contract information from this site, but the links and associated data are generated using embedded Javascript, and the URLs of the pages displaying contract awards and other data don't seem to follow a discernable schema (example).
Is there any way I can scrape the browser rendered data in the first example through R?
The main page calls a javascript function
javascript:callTabContent('p','P090644','','en','procurement','procurementId');
The main thing here is the project id P090644. This together with the required language en are passed as parameters to a form at http://www.worldbank.org/p2e/procurement.html.
This form call can be replicated with a url http://www.worldbank.org/p2e/procurement.html?lang=en&projId=P090644.
Code to extract relevant project description urls follows:
projID<-"P090644"
projDetails<-paste0("http://www.worldbank.org/p2e/procurement.html?lang=en&projId=",projID)
require(XML)
pdData<-htmlParse(projDetails)
pdDescribtions<-xpathSApply(pdData,'//*/table[#id="contractawards"]//*/#href')
#> pdDescribtions
href
#"http://search.worldbank.org/wcontractawards/procdetails/OP00005718"
href
#"http://search.worldbank.org/wcontractawards/procdetails/OP00005702"
href
#"http://search.worldbank.org/wcontractawards/procdetails/OP00005709"
href
#"http://search.worldbank.org/wcontractawards/procdetails/OP00005715"
it should be noted that excel links are provided which maybe of use to you also. They may contain the data you intend to scrap from the description links
procNotice<-paste0("http://search.worldbank.org/wprocnotices/projectdetails/",projID,".xls")
conAward<-paste0("http://search.worldbank.org/wcontractawards/projectdetails/",projID,".xls")
conData<-paste0("http://search.worldbank.org/wcontractdata/projectdetails/",projID,".xls")
require(gdata)
pnData<-read.xls(procNotice)
caData<-read.xls(conAward)
cdData<-read.xls(conData)
UPDATE:
To find what is being posted we can examine what happens when the javascript function is called. Using Firebug or something similar we intercept the request header which starts:
POST /p2e/procurement.html HTTP/1.1
Host: www.worldbank.org
and has parameters:
lang=en
projId=P090644
Alternatively we can examine the javascript at http://siteresources.worldbank.org/cached/extapps/cver116/p2e/js/script.js and look at the function callTabContent:
function callTabContent(tabparam, projIdParam, contextPath, langCd, htmlId, anchorTagId) {
if (tabparam == 'n' || tabparam == 'h') {
$.ajax( {
type : "POST",
url : contextPath + "/p2e/"+htmlId+".html",
data : "projId=" + projIdParam + "&lang=" + langCd,
success : function(msg) {
if(tabparam=="n"){
$("#newsfeed").replaceWith(msg);
} else{
$("#cycle").replaceWith(msg);
}
stickNotes();
}
});
} else {
$.ajax( {
type : "POST",
url : contextPath + "/p2e/"+htmlId+".html",
data : "projId=" + projIdParam + "&lang=" + langCd,
success : function(msg) {
$("#tabContent").replaceWith(msg);
$('#map_container').hide();
changeAlternateColors();
$("#tab_menu a").removeClass("selected");
$('#'+anchorTagId).addClass("selected");
stickNotes();
}
});
}
}
examining the content of the function we can see it is simply posting relevant parameters to a form then updating the webpage.
I am not sure I have understood every details of your problem.
But what I know for sure is that casperJS works great for javascript generated content.
You can have a look at it here: http://casperjs.org/
It's written in Javascript and has a bunch of useful functions very well documented on the link I provided.
I have used it myself lately for a personal project and can be set up easily with a few lines of code.
Give it a go!
Hope, that helps..

How can I modify the Book Copy module in Drupal 6 to forward the user to a different starting tab once a copy is made?

Example call to copy a book using the Book Copy module in Drupal 6 (assuming a node exists with book id of 142):
www.examplesite.com/book_copy/copy/142
When the above site is called and node 142 is copied it then notifies the user that the book was copied, but starts the user out on the Outline tab for the book copy. I think it would be more intuitive to start the user out on the Edit tab for the book copy so the user can immediately start to edit the information for the book. The outline is less important than setting up the actual initial details for the book which are in the Edit tab.
Does anyone know how I could modify the module to forward the user to the Edit tab? I looked through the code and it's just not clicking. I'm having problems interpreting exactly how this Book Module is working under the hood. Any suggestions will be greatly appreciated. Thanks!
I have no experience with the book_copy module, but looking at the last lines of the book_copy_copy_book() function:
$book = node_load(array('nid' => $newbid));
$book->bookcopydata = array();
$book->bookcopydata['message'] = t('Successfully cloned "%message", now viewing copy.', array('%message' => $message));
if (_book_outline_access($book)) {
$book->bookcopydata['url'] = 'node/'. $newbid .'/outline';
}
else {
$book->bookcopydata['url'] = 'node/'. $newbid;
}
// The function signature is: hook_book_copy_goto_alter(&$data);
drupal_alter("book_copy_goto", $book);
drupal_set_message($book->bookcopydata['message']);
drupal_goto($book->bookcopydata['url']); // requires user has 'administer book outline' or can access personal books
the user would automatically end on the new book page if he did not have the 'administer book outlines' right. If you want the user to always end on the new book edit page, you could just replace the whole if
if (_book_outline_access($book)) { ... }
clause with the assignment from its else part with an adjusted url:
$book->bookcopydata['url'] = 'node/'. $newbid . '/edit';
However, changing a modules code directly is not recommended (update clashes), especially if the module provides a hook to achieve the change you need 'from outside'. So the 'right' way in your situation would be to implement the offered hook_book_copy_goto_alter(&$data) in a custom module in order to adjust the redirection URL to your liking:
function yourModule_book_copy_goto_alter(&$new_book) {
$new_book->bookcopydata['url'] = 'node/'. $new_book->nid . '/edit';
}

Resources