Skip to content

Commit

Permalink
fix: replicate tinder functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed Jun 23, 2017
1 parent 69bffc5 commit 3a9365b
Show file tree
Hide file tree
Showing 16 changed files with 938 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
node_modules
lib
coverage
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-hfreire"
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
*.iml
coverage
node_modules
lib
*.log
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea/
coverage/
src/
test/
.babelrc
.gitignore
.eslintignore
.eslintrc
.travis.yml
README.md
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- '6.3'
cache:
directories:
- node_modules
notifications:
email: false
slack:
rooms:
secure: nnkzVM4iL6nCILxLtMivzmRbN2jPC8vodXBZhYUGW0DL3+dgbgN9HxxRarAjJX4ExagJeVDuTe4FbExPJ9qMVG0HitzRg+fHgH8m/sei5elKM2idhgHUYMswv8M91STgbdYPGeGvabgUmdIw439oKVL9iUrs+QO6GReM1TRXMWeM799TV19l4Zt0AbtWh16F+X8/Sp6p0EdmBKXRlaTNfWsQOl9fyvhCweZFpA6+wjbypxafuiGiBGXIf2RT9cCTyJxG6Qx+7UEqQvjA6+Q76cbn4xa+nV34e8Whl+2JDHKGVipwen9w+3H4HabSum17Si4FTxsNyuA+Q+3pbEYcYnnRefvk2tjFxFFa8IKbBsDIZL6pKBBF24M+Jry8rcIjgvpxSIPVfmjJiOMKHXx6NEJ8F/7091i6ZjHAABhH5fVjJtUu3gV7HpOK3/M0MPi2/2/ND+HBKA4xk4lsDVmX4+lESOnaKqK4J17AhmkCQ761CWIP/x+8EV0KNIk1zLPX9kl7Le/mvAPLvWsQBffDlZ1kuVRgZADtfHoRA6PvZPIz6EFS2JSTl5nqkLXaspjYf8eyTqrs823x4LwtJ8njZ9/QRThz7qovkjbW2XjaJvh7CKnZOPVnuvowrgsL9JD5rysrqwSNkgFoCQxSDjmKv2tfN73flML+BS+4/29eRRI=
on_success: never
on_failure: always
on_pull_requests: false
before_script:
- npm prune
after_success:
- npm run coveralls
- npm run semantic-release
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Hugo Freire <hugo@exec.sh>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 60 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "happn-wrapper",
"description": "A Happn wrapper library",
"version": "0.0.0",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/hfreire/happn-wrapper.git"
},
"author": "Hugo Freire <hugo@exec.sh>",
"license": "MIT",
"bugs": {
"url": "https://github.com/hfreire/happn-wrapper/issues"
},
"homepage": "https://github.com/hfreire/happn-wrapper#readme",
"dependencies": {
"bluebird": "3.5.0",
"bluebird-retry": "0.11.0",
"brakes": "2.5.3",
"lodash": "4.17.4",
"request": "2.81.0"
},
"devDependencies": {
"babel-cli": "6.24.1",
"babel-preset-es2015": "6.24.1",
"chai": "4.0.2",
"chai-as-promised": "7.0.0",
"coveralls": "2.13.1",
"eslint": "3.19.0",
"eslint-config-hfreire": "1.0.4",
"eslint-plugin-import": "2.3.0",
"eslint-plugin-json": "1.2.0",
"eslint-plugin-mocha": "4.9.0",
"eslint-plugin-node": "5.0.0",
"eslint-plugin-promise": "3.5.0",
"eslint-plugin-standard": "3.0.1",
"eslint-plugin-unicorn": "2.1.1",
"istanbul": "0.4.5",
"mocha": "3.4.2",
"semantic-release": "6.3.6",
"pre-git": "3.14.1",
"testdouble": "3.0.0"
},
"config": {
"pre-git": {
"commit-msg": "conventional"
}
},
"scripts": {
"clean": "rm -rf lib coverage",
"lint": "node_modules/.bin/eslint . --ext .json --ext .js",
"coverage": "node_modules/.bin/istanbul cover --include-all-sources --root src --print detail ./node_modules/mocha/bin/_mocha -- --recursive test",
"test": "npm run lint && npm run coverage",
"compile": "npm run clean && mkdir -p lib && ./node_modules/.bin/babel src/ -d lib",
"prepublish": "npm run compile",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"semantic-release": "./node_modules/.bin/semantic-release pre && npm publish && node_modules/.bin/semantic-release post",
"commit": "./node_modules/.bin/commit-wizard"
}
}
22 changes: 22 additions & 0 deletions src/errors/happn-not-authorized-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

class HappnNotAuthorizedError extends Error {
constructor (message) {
super(message)

this.name = this.constructor.name

if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor)
} else {
this.stack = (new Error(message)).stack
}
}
}

module.exports = HappnNotAuthorizedError
10 changes: 10 additions & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
HappnNotAuthorizedError: require('./happn-not-authorized-error')
}
225 changes: 225 additions & 0 deletions src/happn-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

const BASE_URL = 'https://api.happn.fr'

const _ = require('lodash')
const Promise = require('bluebird')
const retry = require('bluebird-retry')
const Brakes = require('brakes')

const { HappnNotAuthorizedError } = require('./errors')

const request = require('request')

const handleResponse = ({ statusCode, statusMessage, body }) => {
if (statusCode >= 300) {
switch (statusCode) {
case 401:
throw new HappnNotAuthorizedError()
default:
throw new Error(`${statusCode} ${statusMessage}`)
}
}

let _body = body
if (_.isString(_body)) {
_body = JSON.parse(_body)
}

if (_body.error_code && _body.error_code !== 0) {
throw new Error(`${_body.error_code} ${_body.error}`)
}

return _body
}

const defaultOptions = {
request: {
headers: {
'User-Agent': 'Happn/19.1.0 AndroidSDK/19'
}
},
retry: { max_tries: 2, interval: 1000, timeout: 12000, throw_original: true },
breaker: { timeout: 16000, threshold: 80, circuitDuration: 3 * 60 * 60 * 1000 }
}

class HappnWrapper {
constructor (options = {}) {
this._options = _.defaultsDeep(options, defaultOptions)

this._request = Promise.promisifyAll(request.defaults(this._options.request))

this._breaker = new Brakes(this._options.breaker)

this._getRequestCircuitBreaker = this._breaker.slaveCircuit((...params) => retry(() => this._request.getAsync(...params), this._options.retry))
this._postRequestCircuitBreaker = this._breaker.slaveCircuit((...params) => retry(() => this._request.postAsync(...params), this._options.retry))
}

set accessToken (accessToken) {
this._accessToken = accessToken
}

get accessToken () {
return this._accessToken
}

set refreshToken (refreshToken) {
this._refreshToken = refreshToken
}

get refreshToken () {
return this._refreshToken
}

set userId (userId) {
this._userId = userId
}

get userId () {
return this._userId
}

authorize (facebookAccessToken) {
if (!facebookAccessToken) {
return Promise.reject(new Error('invalid arguments'))
}

const options = {
url: `${BASE_URL}/connect/oauth/token`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
client_id: 'FUE-idSEP-f7AqCyuMcPr2K-1iCIU_YlvK-M-im3c',
client_secret: 'brGoHSwZsPjJ-lBk0HqEXVtb3UFu-y5l_JcOjD-Ekv',
grant_type: 'assertion',
assertion_type: 'facebook_access_token',
assertion: facebookAccessToken,
scope: 'mobile_app'
}
}

return this._postRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
.then((data) => {
this._accessToken = data.access_token
this._refreshToken = data.refresh_token
this._userId = data.user_id

return data
})
}

getRecommendations (limit = 10, offset = 0) {
const options = {
url: `${BASE_URL}/api/users/${this._userId}/notifications`,
headers: {
'Authorization': `OAuth="${this._accessToken}"`
},
qs: {
types: 468,
limit,
offset,
fields: 'id,modification_date,notification_type,nb_times,notifier.fields(id,about,job,is_accepted,birth_date,workplace,my_relation,distance,gender,my_conversation,is_charmed,nb_photos,first_name,last_name,age,profiles.mode(1).width(360).height(640).fields(width,height,mode,url))'
},
json: true
}

return this._getRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
.then(({ data }) => data)
}

getAccount () {
return this.getUser(this._userId)
}

getUser (userId) {
if (!userId) {
return Promise.reject(new Error('invalid arguments'))
}

const options = {
url: `${BASE_URL}/api/users/${userId}`,
headers: {
'Authorization': `OAuth="${this._accessToken}"`
},
json: true
}

return this._getRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
.then(({ data }) => data)
}

getUpdates (limit = 10, offset = 0) {
const options = {
url: `${BASE_URL}/api/users/${this._userId}/notifications`,
headers: {
'Authorization': `OAuth="${this._accessToken}"`
},
qs: {
types: 473,
limit,
offset,
fields: 'id,modification_date,notification_type,nb_times,notifier.fields(id,about,job,is_accepted,birth_date,workplace,my_relation,distance,gender,my_conversation,is_charmed,nb_photos,first_name,last_name,age,profiles.mode(1).width(360).height(640).fields(width,height,mode,url))'
},
json: true
}

return this._getRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
.then(({ data }) => data)
}

sendMessage () {
return Promise.reject(new Error('not implemented'))
}

like (userId) {
if (!userId) {
return Promise.reject(new Error('invalid arguments'))
}

const options = {
url: `${BASE_URL}/api/users/${this._userId}/accepted/${userId}`,
headers: {
'Authorization': `OAuth="${this._accessToken}"`
},
body: {
id: userId
},
json: true
}

return this._postRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
}

pass (userId) {
if (!userId) {
return Promise.reject(new Error('invalid arguments'))
}

const options = {
url: `${BASE_URL}/api/users/${this._userId}/rejected/${userId}`,
headers: {
'Authorization': `OAuth="${this._accessToken}"`
},
body: {
id: userId
},
json: true
}

return this._postRequestCircuitBreaker.exec(options)
.then((response) => handleResponse(response))
}
}

module.exports = HappnWrapper

0 comments on commit 3a9365b

Please sign in to comment.