Access openSearch:totalResults using feedparser - feedparser

How can I retrieve the openSearch:totalResults attribute using feedparser?
I have a blogger API result which looks a bit like this (I have cut out some stuff to make it compact here)
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
xmlns:gd='http://schemas.google.com/g/2005'
gd:etag='W/"CUYMQ348fyp7ImA9WB9UFkU."'>
<id>tag:blogger.com,1999:blog-blogID.postpostID..comments</id>
<updated>2007-12-14T17:46:22.077-08:00</updated>
<title>Comments on Lizzy's Diary: Quite disagreeable</title>
<generator version='7.00'
uri='http://www.blogger.com'>Blogger</generator>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
Currently, I do something like
req = urllib.urlopen(url)
urlContent = req.read()
feed = feedparser.parse(urlContent)
print feed.feed['openSearch_totalResults']
I get an error with the above code that the attribute does not exist. I have confirmed that the feed parser has the namespace by running
print feed['namespaces']
which gives me
{'': u'http://www.w3.org/2005/Atom', u'gd': u'http://schemas.google.com/g/2005', u'thr': u'http://purl.org/syndication/thread/1.0', u'openSearch': u'http://a9.com/-/spec/opensearchrss/1.0/'}

There is a problem with the capitalization: it should be opensearch_totalresults instead of openSearch_totalResults. Thus, the correct line would be:
print feed.feed['opensearch_totalresults']

Related

Pentaho JsonInput GET fields

I'm trying to use PDI to read data from an API (json) and now I'm simply trying to use json input to get a few specific fields but the get fields button on the input step gives me.
ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 11.09.08 by buildguy) : Index 1 out of bounds for length 1
all the steps execute fine, and produce data - just not the json input step doesn't wnat to give me the fields option! - I've tired the text file and json oput and both write valid json so IDK whats going on....
PS. this is my first time using PDI
ISSUE 2:
It looks like PDI uses jayway for its json path parsing so I've been using this site https://jsonpath.herokuapp.com/ jayway selection which gives me my expected path. When I put that into the 'fields' of the json input dialog I only get the FIRST instance of that path value vs it actually parsing the json and giving me every instance, and can't figure out why though I assume it has something to do with PDI's row based view on things but I also don't know how to get it to understand that its json and it should be giving me back all values that match that path.
UPDATE 1:
I've been looking at this https://forums.pentaho.com/threads/135882-Parsing-JSON-data-without-knowing-field-names/ it seems like this Modified Java Script Value step might be the way to go. Will continue testing.
UPDATE 2
OK - Used the MJSV as posted above along with a select fields step and finally able to get the key's
var obj = JSON.parse(mydata);
var keys = Object.keys(obj);
for (var i = 0; i < Object.keys(obj).length; i++) {
var row = createRowCopy(getOutputRowMeta().size());
var idx = getInputRowMeta().size();
row[idx++] = keys[i];
putRow(row);
}
trans_Status = SKIP_TRANSFORMATION;

Pymongo: insert_many() gives "TypeError: document must be instance of dict" for list of dicts

I haven't been able to find any relevant solutions to my problem when googling, so I thought I'd try here.
I have a program where I parse though folders for a certain kind of trace files, and then save these in a MongoDB database. Like so:
posts = function(source_path)
client = pymongo.MongoClient()
db = client.database
collection = db.collection
insert = collection.insert_many(posts)
def function(...):
....
post = parse(trace)
posts.append(post)
return posts
def parse(...):
....
post = {'Thing1': thing,
'Thing2': other_thing,
etc}
return post
However, when I get to "insert = collection.insert_many(posts)", it returns an error:
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
According to the debugger, "posts" is a list of about 1000 dicts, which should be vaild input according to all of my research. If I construct a smaller list of dicts and insert_many(), it works flawlessly.
Does anyone know what the issue may be?
Some more debugging revealed the issue to be that the "parse" function sometimes returned None rather than a dict. Easily fixed.

How to load objects from sql queries in Joomla 2.5?

Using the Andrew Eddie's tutorials here I am working on building some custom code for menus. Here we go:
$query ->select('id, menutype, title')
->from('#__menu_types')
->where('menutype='.$somemenu);
$db->setQuery($query);
I don't know how to load one object value like I used to do it with Joomla 1.5:
$result = $db->loadObject();
$thetitle = $result->title; // I need this value and I always get error "Notice: Trying to get property of non-object" at this line
How can I SUCCESSFULLY get the value of $thetitle please?
That should work. I see no problem with your code.
The error you are getting is consistent with not having found a match in the database.
Since you do not appear to have any error handling it might even be an SQL error.
Try and add this:
if ($error = $db->getErrorMsg()) {
throw new Exception($error);
}
The correct query is
$query ->select('id, menutype, title')
->from('#__menu_types')
->where('menutype='.$db->quote($somemenu));
$db->setQuery($query);
Now I can get the values of the query correctly.

Using the output of one velocity template in another

I have a velocity template (in a Confluence user macro) that looks like:
## This macro takes a jiraissues macro in the body with "Show Total Only" configured to TRUE.
## It then parses the return and puts a green check if the number returned is ZERO or a red X otherwise.
## #noparams
#set ($start = $body.indexOf("{") + 1)
#set ($end = $body.indexOf("}") )
Printf debugging...<br />
body.substring($start, $end) = $body.substring($start, $end) <br />
<ac:rich-text-body>
<ac:image ac:thumbnail="false">
## BUG BUG This substring is ALWAYS zero. Dunno why.
#if ($body.substring($start, $end) == "0")
<ri:url ri:value="/images/icons/emoticons/check.png" />
#else
<ri:url ri:value="/images/icons/emoticons/error.png" />
#end
</ac:image>
</ac:rich-text-body>
This template has a nested other velocity template that is configured by the user to query a DB and return the number of bugs that match some criteria. The idea is that if the number returned is ZERO, then everything is hunkydory. Otherwise, well... you get the picture.
Now, there's something CLEARLY screwed up in my thinking.
The $body string seems to returns something that looks like {0} issues.
The {0} seems like a variable or something, but hell if I can find any documentation.
Questions
Which template gets evaluated first?
Can I even base the logic of one template on the output of another?
Why is my life like this? Never mind, I know the answer to that one.
A string like {0} suggests that what you see isn't the actual end result, but a message template that is supposed to be filled in with real data. And to me it looks like a key used by MessageFormat, but it could be something else.
Do you happen to have the code for the inner macro as well?

joomla: use API inside an article

I am using the Sourcerer plugin to use PHP code inside my articles. I would like to use the Joomla API/framework inside my article to dynamically set the HTML meta tags and other stuff. I found the setHeadData method that should allow me do that but I have simply no idea as to how calling it.
[Q] Can someone give me 1 example or point me to a tutorial that would help me get started on using that joomla API/framework please?
Answer
Based on the numerous feedbacks all pointing in the same direction, using a content plugin to modify the head data is properly better than doing this via an article. If like me you want to do this in an article here is what I did:
(1) I used the snippet provided by ezpresso to set the head data inside my article.
(2) I modified the libraries/joomla/document/html/renderer/head.php file to change the way the head data was set there.
For instance you can set the title meta tag at step (1) and then at step (2) replace the following line:
$strHtml .= $tab.'<title>'.htmlspecialchars($document->getTitle()).'</title>'.$lnEnd;
with this one:
$strHtml .= $tab.'<title>'.htmlspecialchars($document['metaTags']['standard']['title']).'</title>'.$lnEnd;
You might also want to look into libraries/joomla/document/html/renderer/head.php to do some more cleanup in the head, like getting rid of the generator meta tag that Joomla inserts.
Here is the source code of the method you are referring to:
/**
* Set the html document head data
*
* #access public
* #param array $data The document head data in array form
*/
function setHeadData($data)
{
$this->title = (isset($data['title'])) ? $data['title'] : $this->title;
$this->description = (isset($data['description'])) ? $data['description'] : $this->description;
$this->link = (isset($data['link'])) ? $data['link'] : $this->link;
$this->_metaTags = (isset($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
$this->_links = (isset($data['links'])) ? $data['links'] : $this->_links;
$this->_styleSheets = (isset($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
$this->_style = (isset($data['style'])) ? $data['style'] : $this->_style;
$this->_scripts = (isset($data['scripts'])) ? $data['scripts'] : $this->_scripts;
$this->_script = (isset($data['script'])) ? $data['script'] : $this->_script;
$this->_custom = (isset($data['custom'])) ? $data['custom'] : $this->_custom;
}
It is implemented in a JDocumentHtml class, which is located in a libraries/joomla/document/html/html.php directory.
Below is the links to some of the examples of how to use it:
setHeadData difference between j1.5 and j1.6
Remove Mootools From Joomla Header
I guess you may call the setHeadData method like this:
$doc =& JFactory::getDocument();
$options = $doc->getHeadData();
$options['metaTags'] = array("tag1", "tag2", "tag3"); // you may change the meta tags here
$doc->setHeadData($options);
Putting PHP in the article is not a very good way to accomplish what you are trying to do. Joomla frameworks has an order of operation that determines when various functions and plugins run. Due to the order of operation, there are numerous functions that will happen after an article is rendered, probably negating any changes you make from within the article. You would be better off either using an extension to handle titles and meta tags rather than trying to do it inside the article.