Good evening n0bisuke. I'm taking notes of what I was addicted to in pepper development. (Last time => Note on creating your own Box with Pepper's Python)
For python, the request module is great. I want to use it positively when communicating with http ... However, pip cannot be used with pepper, so let's do it with the following procedure.
By the way, the photo is @ ukk0 who recently joined our team. It has nothing to do with the content of the article lol
Refer to Using Requests for HTTP communication of Pepper.
A module that makes http requests nice. There is ʻurllib2`, but it's so easy that it's incomparable.
$ pip install requests
app.py
import requests
r = requests.get('http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json')
print r.json()["title"]
Get information about Qiita posts. http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json
$ python app.py
Sample of HTTP GET and JSON parsing with python of pepper
I'll call you in two lines. It's easy.
It seems that pip cannot be used with pepper, and it is common to insert the entire directory orz
Download it as a zip from github page and insert the contents requests folder
into the choregraphe side.
↓
Create a lib folder
and put the requests folder
in it.
After that, edit Python Script
like Last article.
app.py
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
def onUnload(self):
import sys
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
def onInput_onStart(self):
import sys, os
self.folderName = os.path.join(self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
def onInput_onStop(self):
self.onUnload()
self.onStopped()
self.framemanager.getBehaviorPath (self.behaviorId)," ../ lib "
, etc. are like specifying the path to the lib folder
created earlier.
I'm not familiar with python, so I won't mention it anymore.
app.py
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
def onUnload(self):
import sys
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
def onInput_onStart(self):
import sys, os
self.folderName = os.path.join(self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
import requests
r = requests.get('http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json') #← Addendum
title = r.json()["title"].encode("utf-8") #← Addendum
self.logger.info(title) #← Addendum
def onInput_onStop(self):
self.onUnload()
self.onStopped()
Now you can finally ʻimport requests`. The rest is the same as the python code I wrote at the beginning.
If you execute it and the parsed character string is displayed in the dialog of the collegraph, it is successful.
Sample of HTTP GET and JSON parsing with python of pepper