This time, I would like to introduce what you can do with the API.
The API here is something like a function that can be called at any time if you are connected to the Internet.
We are planning to introduce what is open to the public by companies and individuals.
With reference to the here page, I wrote a Nico Nico video that creates and registers my list.
nico_mylist_add.py
#!/usr/bin/env python
#coding: utf8
import sys, re, cgi, urllib, urllib2, cookielib, xml.dom.minidom, time, netrc
import json
#netrc
netrc = netrc.netrc()
userid,a,passwd = netrc.authenticators("nicovideo")
def getToken():
html = urllib2.urlopen("http://www.nicovideo.jp/my/mylist").read()
for line in html.splitlines():
mo = re.match(r'^\s*NicoAPI\.token = "(?P<token>[\d\w-]+)";\s*',line)
if mo:
token = mo.group('token')
break
assert token
return token
def mylist_create(name):
cmdurl = "http://www.nicovideo.jp/api/mylistgroup/add"
q = {}
q['name'] = name.encode("utf8")
q['description'] = ""
q['public'] = 0
q['default_sort'] = 0
q['icon_id'] = 0
q['token'] = token
cmdurl += "?" + urllib.urlencode(q)
j = json.load( urllib2.urlopen(cmdurl), encoding='utf8')
return j['id']
def addvideo_tomylist(mid,smids):
for smid in smids:
cmdurl = "http://www.nicovideo.jp/api/mylist/add"
q = {}
q['group_id'] = mid
q['item_type'] = 0
q['item_id'] = smid
q['description'] = u""
q['token'] = token
cmdurl += "?" + urllib.urlencode(q)
j = json.load( urllib2.urlopen(cmdurl), encoding='utf8')
time.sleep(0.5)
#Login
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
urllib2.install_opener(opener)
urllib2.urlopen("https://secure.nicovideo.jp/secure/login",
urllib.urlencode( {"mail":userid, "password":passwd}) )
#Get token
token = getToken()
#argv
myn = sys.argv[1]
argvs = sys.argv[2:]
#Create my list and register videos
mid = mylist_create(myn)
addvideo_tomylist(mid, argvs )
It's easy to use and gives execute permission to the file. Then, pass the URL and execute it.
python
$ chmod +x nico_mylist_add.py
$ ./nico_mylist_add.py mylistname sm9 sm1097445 sm1715919
The changes are (1) the login information is obtained from netrc
, and (2) the name and URL of My List are obtained from the arguments.
netrc
The netrc
looks like this:
python
$ cat ~/.netrc
#############################
machine nicovideo
login [email protected]
password pass123
#############################
The processing is as follows.
python
#!/usr/bin/env python
import netrc
netrc = netrc.netrc()
l, a, p = netrccfg.authenticators("nicovideo")
print "My Login = %s" % (l)
print "My Password = %s" % (p)
print "My Account= %s" % (a)
python
#!/usr/bin/env python
import sys
mylistname = sys.argv[1]
argvs = sys.argv[2:]
print mylistname
print argvs
For example, to register the URL in the existing My List, make the following changes.
First, change ʻaddvideo_tomylist. Put
group_id in the part of
xxxx`.
Here, to get the group_id
, use http://www.nicovideo.jp/api/mylistgroup/list
. Maybe ...
reference: http://efcl.info/wiki/niconicoapi/
def addvideo_tomylist(smids):
for smid in smids:
cmdurl = "http://www.nicovideo.jp/api/mylist/add"
q = {}
q['group_id'] = xxxxxxxx
q['item_type'] = 0
q['item_id'] = smid
q['description'] = u""
q['token'] = token
cmdurl += "?" + urllib.urlencode(q)
j = json.load( urllib2.urlopen(cmdurl), encoding='utf8')
time.sleep(0.5)
Also, since it is no longer necessary to pass the name of My List as an argument, change that part.
#argv
#myn = sys.argv[1]
argvs = sys.argv[1:]
Google+
Try to get user information using Google+ API.
In Google Cloud Console, create a project and enable the API to be used.
First, access the following URL with a browser and get the Authorization code
.
python
https://accounts.google.com/o/oauth2/auth?client_id=[Client ID]&redirect_uri=[Redirect URI]&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me&response_type=code
Please note that []
is not included.
Authorization code
is the part of the page URL undercode =
.
python
$ curl -d client_id=[YOUR Client Id] -d client_secret=[YOUR Client Secret] -d redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback -d grant_type=authorization_code -d code=[YOUR Authorization code] https://accounts.google.com/o/oauth2/token
ʻAccess_token` etc. will be issued, so you can use that value to get user information. The page of here is very helpful. It will be.
python
$ curl -H "Authorization: OAuth [YOUR Access_token]" https://www.googleapis.com/plus/v1/people/me
Note that ʻaccess_token expires after a certain period of time, so if you want to create an app that uses the Google API, you should use
refresh_token`.
reference: http://mba-hack.blogspot.jp/2013/12/google-api.html
https://github.com/sorah/niconico
https://github.com/google/google-api-ruby-client
https://developers.google.com/+/api/latest/?hl=ja
Recommended Posts