SponsorDetector / sponsor-detector-server

Server used to store / get informations for and from the addon
0 stars 0 forks source link

Conf api #1

Open ogdabou opened 8 years ago

ogdabou commented 8 years ago

Rest interface :

_CONF API_

_CONF VIEW API_

_STAT VIEW API_

_CONF DOCUMENT_

{
  "domain" : String,
  "name" : null,
  "author" : {
    "extractor" : {
      "name" : String,
      "params" : [ String, String, ... ]
    }
  },
  "sponsor" : {
    "detector" : {
      "name" : String,
      "params" : [ String, String, ... ]
    },
    "extractor" : {
      "name" : String,
      "params" : [ String, String, ... ]
    }
  }
}

_CONF FOR AUTHOR EXTRACTION_

{
  "domain" : String,
  "name" : null,
  "author" : {
    "extractor" : {
      "name" : String,
      "params" : [ String, String ]
    }
  }
}

_CONF FOR SPONSOR EXTRACTION_

{
  "domain" : String,
  "name" : null,
  "sponsor" : {
    "detector" : {
      "name" : String,
      "params" : [ String, String ]
    },
    "extractor" : {
      "name" : String,
      "params" : [ String, String ]
    }
  }
}

_STAT_

var result = {
  long : Long,
  author : String,
  sponsor : String,
  domain : String,
  title : String,
  link : String
};

_REPONSE_

{
  "sponsored" : Long, // nombre total de media sponsorisé par le sponsor
  "author" : {
    "endorsed" : Long, // nombre de fois que cet auteur à publié avec ce sponsor
    "total" : Long // nombre de publication sponsorisés
  }
}
Lenjyco commented 8 years ago

# Hello_mongoose


// Inclusion de Mongoose
var mongoose = require('mongoose');

// On se connecte à la base de données
// N'oubliez pas de lancer ~/mongodb/bin/mongod dans un terminal !
mongoose.connect('mongodb://localhost/plugin', function(err) {
  if (err) { throw err; }
});

// Création du schéma pour les commentaires

var Extracor = function (name){ this.name = name; return {name : name };};
var Detector = function (name){ this.name = name; return {name : name };};
var Author = function (name){ this.name = name; return {name : name };};
var Sponsor = function (name){ this.name = name; return {name : name };};

var conf = new mongoose.Schema({
  domain : String,
  sponsor : {
    name : String,
    detector : {
      name : String,
      params : { type : Array , "default" : [] }
    },
    extractor : {
      name : String,
      params : { type : Array , "default" : [] }
    }
  },
  author : {
    name : String,
    extractor : {
      name : String,
      params : { type : Array , "default" : [] }
    }
  }
});

// Création du Model pour les conf
var ConfModel = mongoose.model('configuration', conf);
var ex = new Extracor("youtube_extractor");
var ex_s = new Extracor("sponsor_extractor");
var det = new Detector("sponsor_detector");
ex.params = ["%%%%%","#####","*****"];
ex_s.params = ["%%%%%","#####","*****"];
det.params = ["%%%%%","#####","*****"];
var sponsor = new Sponsor("YOUTUBE_BANDAI");
var author = new Author("CYPRIEN_YOUTUBE");
sponsor.detector = det;
sponsor.extractor = ex_s;
author.extractor = ex;
// On crée une instance du Model
var conf = new ConfModel({ domain :'youtube.com', author : author, sponsor : sponsor });

// On le sauvegarde dans MongoDB !
conf.save(function (err) {
  if (err) { throw err; }
  console.log('Commentaire ajouté avec succès !');
  // On se déconnecte de MongoDB maintenant
  mongoose.connection.close();
});
Lenjyco commented 8 years ago

#Hello ES + Express.js

var elasticsearch = require('elasticsearch');

var elasticClient = new elasticsearch.Client({
    host: 'localhost:9300',
    log: 'info'
});

var configuration = "configurationIndex";
var sponsordContent = "sponsordContent";

/**
* Delete an existing configuration index
*/
function deleteConfigurationIndex() {
    return elasticClient.indices.delete({
        index: configuration
    });
}
exports.deleteConfigurationIndex = deleteConfigurationIndex;

/**
* create the configuration index
*/
function initConfigurationIndex() {
    return elasticClient.indices.create({
        index: configuration
    });
}
exports.initConfigurationIndex = initConfigurationIndex;

/**
* check if the configuration index exists
*/
function indexConfigurationExists() {
    return elasticClient.indices.exists({
        index: configuration
    });
}
exports.indexConfigurationExists = indexConfigurationExists;

/**
* create the sponsoredContent index
*/
function initSponsoredContentIndex() {
    return elasticClient.indices.create({
        index: sponsoredContent
    });
}
exports.initSponsoredContentIndex = initSponsoredContentIndex;

/**
* check if the sponsoredContent index exists
*/
function indexSponsoredContentExists() {
    return elasticClient.indices.exists({
        index: sponsoredContent
    });
}
exports.indexSponsoredContentExists = indexSponsoredContentExists;

/**
* Delete an existing sponsoredContent index
*/
function deleteSponsoredContentIndex() {
    return elasticClient.indices.delete({
        index: sponsoredContent
    });
}
exports.deleteSponsoredContentIndex = deleteSponsoredContentIndex;

function initMapping() {
    return elasticClient.indices.putMapping(
      {
        index: configuration,
        type: "configuration",
        body:
          dynamic_templates : [
              {
                string_fields: {
                  match: "*",
                  match_mapping_type: "string",
                  mapping: {
                    index: "not_analyzed",
                    omit_norms: true,
                    type: "string"
                  }
                }
              }
            ],
        properties: {
          pushed_at: {
            type: "date",
            format : "yyyy-MM-dd HH:mm"
          }
        }
    },
  {
    index: sponsoredContent,
    type: "sponsoredContent",
    body:
      dynamic_templates: [
          {
            string_fields: {
              match: "*",
              match_mapping_type: "string",
              mapping: {
                index: "not_analyzed",
                omit_norms: true,
                type: "string"
              }
            }
          }
        ],
    properties: {
      pushed_at: {
        type: "date",
        format : "yyyy-MM-dd HH:mm"
      }
    }
});
}
exports.initMapping = initMapping;

function addConfiguration(configuration) {
    return elasticClient.index({
        index: configuration,
        type: "configuration",
        body: {
            domain: configuration.domain,
            name: configuration.name,
            author: {
                extractor: {
                  name : configuration.author.extractor.name,
                  params : configuration.author.extractor.params.split(",")
                }
            },
            sponsor: {
                detector: {
                  name : configuration.sponsor.detector.name,
                  params : configuration.sponsor.detector.params.split(",")
                },
                extractor: {
                  name : configuration.sponsor.extractor.name,
                  params : configuration.sponsor.extractor.params.split(",")
                }
            }
            status : configuration.status
        }
    });
}
exports.addConfiguration = addConfiguration;

function addSponsoredContent(sponsoredContent) {
    return elasticClient.index({
        index: sponsoredContent,
        type: "sponsoredContent",
        body: {
            id: sponsoredContent.id,
            author: sponsoredContent.author,
            sponsor: sponsoredContent.sponsor,
            domain: sponsoredContent.domain,
            title: sponsoredContent.title,
            link: sponsoredContent.link
            }
        }
    });
}
exports.addSponsoredContent = addSponsoredContent;
Lenjyco commented 8 years ago

swagger: "2.0"
info:
  version: "0.0.1"
  title: DetectorApp
  description: Sponsor detector application
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /api/conf:
    # binds a127 app logic to a route
    x-swagger-router-controller: confs_get
    get:
      description: Return the list of the confs
      # used as the method name of the controller
      #operationId: getConfs
      responses:
        "200":
          description: Array of confs
          schema:
            # a pointer to a definition
            $ref: "#/definitions/Confs"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    post:
      description: Post a conf
      parameters:
        - name: conf
          in: body
          description: Post a new configuration
          required: true
          schema:
            $ref: "#/definitions/Confs"
      responses:
        "201":
          description: New conf created
          schema:
            $ref: "#/definitions/Confs"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /api/conf/autor/all:
    get:
      description: Return the list of all confs for an author
      responses:
        "200":
          description: Array of confs
          schema:
            # a pointer to a definition
            $ref: "#/definitions/Confs"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

  /api/conf/{domainName}:
    get:
      description: Return the conf for a domain
      parameters:
        - name: domainName
          in: path
          description: A domain name for the conf.
          required: true
          type: string
      responses:
        "200":
          description: A conf for a domain
          schema:
            # a pointer to a definition
            $ref: "#/definitions/Confs"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    delete:
      description: Delete a conf for a domain name
      parameters:
        - name: domainName
          in: path
          required: true
          type: string
      responses:
        200:
          description: Delete successful
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

  /api/conf/{domainName}/all:
    get:
      description: Return the list of all confs for a domain
      parameters:
        - name: domainName
          in: path
          description: A domain name for the confs.
          required: true
          type: string
      responses:
        "200":
          description: A list of confs for a domain
          schema:
            # a pointer to a definition
            $ref: "#/definitions/Confs"
            # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

  /api/conf/{domainName}/{authorName}:
    get:
      description: Return the list of all confs for a domain
      parameters:
        - name: domainName
          in: path
          description: A domain name for the conf.
          required: true
          type: string
        - name: authorName
          in: path
          description: An author for the conf.
          required: true
          type: string
      responses:
        "200":
          description: A list of confs for a domain and an author name
          schema:
              # a pointer to a definition
              $ref: "#/definitions/Confs"
          # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

  /api/stats/{domainName}:
    get:
      description: Return the stats for a domain
      parameters:
        - name: domainName
          in: path
          description: A domain name for the stats.
          required: true
          type: string
      responses:
        "200":
          description: A list of stats for a domain
          schema:
              # a pointer to a definition
            $ref: "#/definitions/Stat"
          # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

  /api/stats/{domainName}/{authorName}:
    get:
      description: Return the stats for a domain and  an author
      parameters:
        - name: domainName
          in: path
          description: A domain name for the stats.
          required: true
          type: string
        - name: authorName
          in: path
          description: An author name for the stats.
          required: true
          type: string
      responses:
        "200":
          description: A list of stats for a domain and a name
          schema:
              # a pointer to a definition
            $ref: "#/definitions/Stat"
          # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /api/stat:          
    post:
      summary: Add of a stat
      description: post a stat for an auhtor or domain
      parameters:
        - name: stat
          in: body
          description: Post a new configuration
          required: true
          schema:
            $ref: "#/definitions/Stat"
      responses:
        "201":
          description: New stat created
          schema:
            $ref: "#/definitions/Stat"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

definitions:
  Stat:
    properties:
      author:
        type: string
        description: An author name
      sponsor:
        type: string
        description: A sponsor name
      domain:
        type: string
        description: A domain name
      title:
        type: string
        description: A stat title
      link:
        type: string
        description: A stat link
  Extractor:
    properties:
      name:
        type: string
        description: Name of the extractor
      params:
        type: array
        $ref: "#/definitions/Param"
  Detector:
    properties:
      name:
        type: string
        description: Name of the detector
      params:
        type: array
        $ref: "#/definitions/Param"      
  Param:
    properties:
      param:
        type: string
        description: A parameter for an extractor or detector
  Author:
    properties:
      extractor:
        type: array
        $ref: "#/definitions/Extractor"
        description: An author extractor

  Sponsor:
    properties:
      extractor:
        type: array
        $ref: "#/definitions/Extractor"
        description: An sponsor extractor
      detector:
        type: array
        $ref: "#/definitions/Detector"
        description: An sponsor detector
  Confs:
    properties:
      domain:
        type: string
        description: Name of the domain
      name:
        type: string
        description: Name of the conf
      author:
        type: array
        $ref: "#/definitions/Author"
      sponsor:
        type: array
        $ref: "#/definitions/Sponsor"
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string
ogdabou commented 8 years ago

https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-query-strings.html