creativecommonsjson

Creative Commons JSON

Returns json instead of xml from creative commons api.

flask api that returns cc license depending on what licence is requested.

add svg/eps along with current png.

88x31 image currently - add 88x15 images.

add public domain/cc zero to the api

In [2]:
import requests
import json
import xmltodict
from flask import Flask
from flask_restful import Resource, Api
In [113]:
app = Flask(__name__)
api = Api(app)

#CCBY CCBYNCSA CCBYSA CCBYND CCBYNCND CCBYND CCZERO CCPUBDOMAIN

class ccby(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=y&derivatives=y')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by.svg'})})}) 
        return licdic
    
class ccbync(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=n&derivatives=y')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nc.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by-nc.svg'})})}) 
        return licdic
    
class ccbyncsa(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/licenses/by-nc-sa/4.0/')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-sa.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nc-sa.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by-nc-sa.svg'})})}) 
        return licdic

class ccbysa(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/licenses/by-sa/4.0/')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-sa.svg'})})
        return licdic
        
class ccbync(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=n&derivatives=y')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nc.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by-nc.svg'})})}) 
        return(licdic)

    
class ccbyncnd(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=n&derivatives=n')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-nd.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nc-nd.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by-nc-nd.svg'})})}) 
        return licdic
    
class ccbynd(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=y&derivatives=n')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['html']['a'][1]['#text']
        ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/nd.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nd.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by-nd.svg'})})}) 
        return licdic
    
class cczero(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/publicdomain/zero/1.0/')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['license-name']
        ccimglic = xmpar['result']['html']['p']['a']['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/cc-zero.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/cc-zero.svg'})})}) 
        return licdic

class ccpubdom(Resource):
    def get(self):
        ccdev = requests.get('http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/publicdomain/mark/1.0/')
        xmpar = xmltodict.parse(ccdev.text)
        ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
        cchtmtxt = xmpar['result']['license-name']
        ccimglic = xmpar['result']['html']['p']['a']['img']['@src']
        licdic = dict({'url' : ccabtlic, 'text' : cchtmtxt, 'img' :
        dict({'fullbutton' : dict({'png' : ccimglic, 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-publicdomain.svg'}),
             'halfbutton' : dict({'png' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/png/cc-publicdomain.png',
                                 'svg' : 'http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/cc-publicdomain.svg'})})}) 
        
        return licdic
    
api.add_resource(ccby, '/by')

api.add_resource(ccbync, '/bync')

api.add_resource(ccbynd, '/bynd')

api.add_resource(ccbyncnd, '/byncnd')

api.add_resource(ccbysa, '/bysa')
                 
api.add_resource(ccbyncsa, '/byncsa')

api.add_resource(cczero, '/zero')

api.add_resource(ccpubdom, '/publicdomain')

if __name__ == '__main__':
    app.run(host = '0.0.0.0')
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-113-222dde2a7eb2> in <module>()
     12 
     13 if __name__ == '__main__':
---> 14     app.run(host = '0.0.0.0')

/usr/local/lib/python3.4/dist-packages/flask/app.py in run(self, host, port, debug, **options)
    841         options.setdefault('passthrough_errors', True)
    842         try:
--> 843             run_simple(host, port, self, **options)
    844         finally:
    845             # reset the first request information if the development server

/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes, request_handler, static_files, passthrough_errors, ssl_context)
    692                           reloader_type)
    693     else:
--> 694         inner()
    695 
    696 

/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py in inner()
    654                           processes, request_handler,
    655                           passthrough_errors, ssl_context,
--> 656                           fd=fd)
    657         if fd is None:
    658             log_startup(srv.socket)

/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py in make_server(host, port, app, threaded, processes, request_handler, passthrough_errors, ssl_context, fd)
    548     else:
    549         return BaseWSGIServer(host, port, app, request_handler,
--> 550                               passthrough_errors, ssl_context, fd=fd)
    551 
    552 

/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py in __init__(self, host, port, app, handler, passthrough_errors, ssl_context, fd)
    462                                       socket.SOCK_STREAM)
    463             port = 0
--> 464         HTTPServer.__init__(self, (host, int(port)), handler)
    465         self.app = app
    466         self.passthrough_errors = passthrough_errors

/usr/lib/python3.4/socketserver.py in __init__(self, server_address, RequestHandlerClass, bind_and_activate)
    427                                     self.socket_type)
    428         if bind_and_activate:
--> 429             self.server_bind()
    430             self.server_activate()
    431 

/usr/lib/python3.4/http/server.py in server_bind(self)
    131     def server_bind(self):
    132         """Override server_bind to store the server name."""
--> 133         socketserver.TCPServer.server_bind(self)
    134         host, port = self.socket.getsockname()[:2]
    135         self.server_name = socket.getfqdn(host)

/usr/lib/python3.4/socketserver.py in server_bind(self)
    438         if self.allow_reuse_address:
    439             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
--> 440         self.socket.bind(self.server_address)
    441         self.server_address = self.socket.getsockname()
    442 

OSError: [Errno 98] Address already in use
In [3]:
#licstand = requests.get('http://api.creativecommons.org/rest/1.5/license/standard')
In [7]:
#dictlic = xmltodict.parse(licstand.text)
In [21]:
#lenfield = len(dictlic['licenseclass']['field'])
In [36]:
#for lenf in range(0, lenfield):
#    print(dictlic['licenseclass']['field'][lenf]['label'])
    
    #print(dictlic['licenseclass']['field'][lenf]['@id'])
    
    #print(dictlic['licenseclass']['field'][lenf])
    
    #print(dictlic['licenseclass']['field'][lenf]['description'])
OrderedDict([('@xml:lang', 'en'), ('#text', 'Allow commercial uses of your work?')])
OrderedDict([('@xml:lang', 'en'), ('#text', 'Allow modifications of your work?')])
OrderedDict([('@xml:lang', 'en'), ('#text', 'Jurisdiction of your license')])
In [ ]:
#ShareAlike
In [134]:
#ccdev = requests.get('http://api.creativecommons.org/rest/dev/license/standard/get?commercial=n&derivatives=y')
In [135]:
#xmpar = xmltodict.parse(ccdev.text)
In [136]:
#xmpar['result']['rdf']['rdf:RDF']
Out[136]:
OrderedDict([('@xmlns', 'http://creativecommons.org/ns#'),
             ('@xmlns:rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
             ('Work',
              OrderedDict([('@xmlns:dc', 'http://purl.org/dc/elements/1.1/'),
                           ('@rdf:about', ''),
                           ('license',
                            OrderedDict([('@rdf:resource',
                                          'http://creativecommons.org/licenses/by-nc/4.0/')]))])),
             ('License',
              OrderedDict([('@rdf:about',
                            'http://creativecommons.org/licenses/by-nc/4.0/'),
                           ('permits',
                            [OrderedDict([('@rdf:resource',
                                           'http://creativecommons.org/ns#DerivativeWorks')]),
                             OrderedDict([('@rdf:resource',
                                           'http://creativecommons.org/ns#Distribution')]),
                             OrderedDict([('@rdf:resource',
                                           'http://creativecommons.org/ns#Reproduction')])]),
                           ('requires',
                            [OrderedDict([('@rdf:resource',
                                           'http://creativecommons.org/ns#Attribution')]),
                             OrderedDict([('@rdf:resource',
                                           'http://creativecommons.org/ns#Notice')])])]))])
In [128]:
#xmpar['result']['rdf']['rdf:RDF']['@xmlns']
Out[128]:
'http://creativecommons.org/ns#'
In [129]:
#xmpar['result']['rdf']['rdf:RDF']['Work']
Out[129]:
OrderedDict([('@xmlns:dc', 'http://purl.org/dc/elements/1.1/'),
             ('@rdf:about', ''),
             ('license',
              OrderedDict([('@rdf:resource',
                            'http://creativecommons.org/licenses/by/4.0/')]))])
In [130]:
#xmpar['result']['rdf']['rdf:RDF']['License']
Out[130]:
OrderedDict([('@rdf:about', 'http://creativecommons.org/licenses/by/4.0/'),
             ('permits',
              [OrderedDict([('@rdf:resource',
                             'http://creativecommons.org/ns#DerivativeWorks')]),
               OrderedDict([('@rdf:resource',
                             'http://creativecommons.org/ns#Distribution')]),
               OrderedDict([('@rdf:resource',
                             'http://creativecommons.org/ns#Reproduction')])]),
             ('requires',
              [OrderedDict([('@rdf:resource',
                             'http://creativecommons.org/ns#Attribution')]),
               OrderedDict([('@rdf:resource',
                             'http://creativecommons.org/ns#Notice')])])])
In [120]:
#ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
In [121]:
#ccabtlic
Out[121]:
'http://creativecommons.org/licenses/by/4.0/'
In [92]:
#for perm in xmpar['result']['rdf']['rdf:RDF']['License']['permits']:
#    print(perm['@rdf:resource'])
http://creativecommons.org/ns#DerivativeWorks
http://creativecommons.org/ns#Distribution
http://creativecommons.org/ns#Reproduction
In [93]:
#xmpar['result']['html']['#text']
Out[93]:
'This work is licensed under a .'
In [107]:
#cchtmtxt = xmpar['result']['html']['a'][1]['#text']
In [108]:
#ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
In [109]:
#http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg
In [111]:
#licdic = dict({'licurl' : ccabtlic, 'lictext' : cchtmtxt, 'ccimg' : ccimglic})
In [112]:
#licdic
Out[112]:
{'ccimg': 'http://i.creativecommons.org/l/by/4.0/88x31.png',
 'lictext': 'Creative Commons Attribution 4.0 International License',
 'licurl': 'http://creativecommons.org/licenses/by/4.0/'}
In [138]:
#ccdev = requests.get('http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/licenses/by-nc-sa/4.0/')
In [141]:
#reqccsa.text

#xmpar = xmltodict.parse(reqccsa.text)
#ccabtlic = xmpar['result']['rdf']['rdf:RDF']['License']['@rdf:about']
#cchtmtxt = xmpar['result']['html']['a'][1]['#text']
#ccimglic = xmpar['result']['html']['a'][0]['img']['@src']
#licdic = dict({'licurl' : ccabtlic, 'lictext' : cchtmtxt, 'ccimg' : ccimglic})
In [142]:
#licdic
Out[142]:
{'ccimg': 'http://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png',
 'lictext': 'Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License',
 'licurl': 'http://creativecommons.org/licenses/by-nc-sa/4.0/'}
In [ ]:
#http://api.creativecommons.org/rest/dev/simple/chooser
In [ ]:
#http://api.creativecommons.org/rest/dev/details?license-uri=creativecommons.org
    
#http://api.creativecommons.org/rest/dev/details?license-uri=http://creativecommons.org/licenses/by-nc-sa/4.0/

Comments

Comments powered by Disqus