diff --git a/modules/happn/__init__.py b/modules/happn/__init__.py
new file mode 100644
index 00000000..e51d0ddd
--- /dev/null
+++ b/modules/happn/__init__.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2015 Roger Philibert
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see .
+
+
+from .module import HappnModule
+
+
+__all__ = ['HappnModule']
diff --git a/modules/happn/browser.py b/modules/happn/browser.py
new file mode 100644
index 00000000..b0637442
--- /dev/null
+++ b/modules/happn/browser.py
@@ -0,0 +1,122 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2015 Roger Philibert
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see .
+
+
+import re
+
+from weboob.browser.browsers import DomainBrowser
+from weboob.browser.profiles import IPhone
+from weboob.browser.pages import HTMLPage
+from weboob.exceptions import BrowserIncorrectPassword
+from weboob.tools.json import json
+
+
+__all__ = ['HappnBrowser', 'FacebookBrowser']
+
+
+class FacebookBrowser(DomainBrowser):
+ BASEURL = 'https://graph.facebook.com'
+
+ CLIENT_ID = "247294518656661"
+ access_token = None
+ info = None
+
+ def login(self, username, password):
+ self.location('https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=fbconnect://success&scope=email,user_birthday,user_friends,public_profile,user_photos,user_likes&response_type=token' % self.CLIENT_ID)
+ page = HTMLPage(self, self.response)
+ form = page.get_form('//form[@id="login_form"]')
+ form['email'] = username
+ form['pass'] = password
+ form['persistent'] = 1
+ form.submit(allow_redirects=False)
+ if 'Location' not in self.response.headers:
+ raise BrowserIncorrectPassword()
+
+ self.location(self.response.headers['Location'])
+
+ params = {}
+ for inp in re.findall(r'input [^>]*type=\\"hidden\\" [^>]*>', self.response.text, re.MULTILINE):
+ m = re.search(r'name=\\"([^"]+)\\"', inp)
+ m2 = re.search(r'value=\\"([^"]+)\\"', inp)
+ params[m.group(1)] = m2.group(1).replace('\\', '') if m2 else ''
+ params['__CONFIRM__'] = 1
+ m = re.search(r'rel=\\"async\\" ajaxify=\\"([^"]+)\\"', self.response.text, re.MULTILINE)
+ if m:
+ uri = m.group(1).replace('\\', '')
+ self.location(uri, data=params)
+ m = re.search('access_token=([^&]+)&', self.response.text)
+ if m:
+ self.access_token = m.group(1)
+
+ self.info = self.request('/me')
+
+ def request(self, url, *args, **kwargs):
+ url += '?access_token=' + self.access_token
+ r = self.location(self.absurl(url, base=True), *args, **kwargs)
+ return json.loads(r.content)
+
+
+class HappnBrowser(DomainBrowser):
+ BASEURL = 'https://api.happn.fr/'
+ PROFILE = IPhone('Happn/3.0.2')
+
+ recs = []
+
+ def __init__(self, facebook, *args, **kwargs):
+ super(HappnBrowser, self).__init__(*args, **kwargs)
+ self.facebook = facebook
+
+ r = self.request('/connect/oauth/token', data={
+ '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': facebook.access_token,
+ 'scope': 'mobile_app',
+ })
+ self.session.headers['Authorization'] = 'OAuth="%s"' % r['access_token']
+
+ self.my_id = r['user_id']
+ self.refresh_token = r['refresh_token']
+
+ me = self.request('/api/users/me')
+ self.my_name = me['data']['name']
+
+ def request(self, *args, **kwargs):
+ r = self.location(*args, **kwargs)
+ return r.json()
+
+ def get_contact(self, contact_id):
+ return self.request('/api/users/%s?fields=birth_date,first_name,last_name,display_name,login,credits,referal,matching_preferences,notification_settings,unread_conversations,about,is_accepted,age,job,workplace,school,modification_date,profiles.mode(0).width(1000).height(1000).fields(url,width,height,mode),last_meet_position,my_relation,is_charmed,distance,gender' % contact_id)['data']
+
+ def get_threads(self):
+ return self.request('/api/users/me/conversations')['data']
+
+ def get_thread(self, id):
+ r = self.request('/api/users/me/conversations/%s?fields=id,messages.fields(id,message,creation_date,sender.fields(id)),participants.fields(user.fields(birth_date,first_name,last_name,display_name,login,credits,referal,matching_preferences,notification_settings,unread_conversations,about,is_accepted,age,job,workplace,school,modification_date,profiles.mode(0).width(1000).height(1000).fields(url,width,height,mode),last_meet_position,my_relation,is_charmed,distance,gender))' % id)['data']
+ return r
+
+ def post_message(self, thread_id, content):
+ self.request('/api/conversations/%s/messages/' % thread_id, data={'message': content})
+
+ def find_users(self):
+ return self.request('/api/users/me/notifications?fields=id,is_pushed,lon,actions,creation_date,is_notified,lat,modification_date,notification_type,nb_times,notifier.fields(id,job,is_accepted,workplace,my_relation,distance,gender,my_conversation,is_charmed,nb_photos,last_name,first_name,age),notified.fields(is_accepted,is_charmed)')['data']
+
+ def accept(self, id):
+ self.request('/api/users/me/accepted/%s' % id, method='POST')
diff --git a/modules/happn/favicon.png b/modules/happn/favicon.png
new file mode 100644
index 00000000..54e9b77d
Binary files /dev/null and b/modules/happn/favicon.png differ
diff --git a/modules/happn/module.py b/modules/happn/module.py
new file mode 100644
index 00000000..a56d7acf
--- /dev/null
+++ b/modules/happn/module.py
@@ -0,0 +1,268 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2014 Roger Philibert
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see .
+
+
+import datetime
+from dateutil.parser import parse as parse_date
+from dateutil.tz import tzlocal
+
+from weboob.capabilities.base import NotAvailable
+from weboob.capabilities.messages import CapMessages, CapMessagesPost, Thread, Message
+from weboob.capabilities.dating import CapDating, Optimization
+from weboob.capabilities.contact import CapContact, Contact, ProfileNode
+from weboob.tools.backend import Module, BackendConfig
+from weboob.tools.date import local2utc
+from weboob.tools.value import Value, ValueBackendPassword
+from weboob.tools.ordereddict import OrderedDict
+from weboob.tools.log import getLogger
+
+from .browser import HappnBrowser, FacebookBrowser
+
+
+__all__ = ['HappnModule']
+
+
+class ProfilesWalker(Optimization):
+ def __init__(self, sched, storage, browser):
+ super(ProfilesWalker, self).__init__()
+ self._sched = sched
+ self._storage = storage
+ self._browser = browser
+ self._logger = getLogger('walker', browser.logger)
+
+ self._view_cron = None
+
+ def start(self):
+ self._view_cron = self._sched.schedule(1, self.view_profile)
+ return True
+
+ def stop(self):
+ self._sched.cancel(self._view_cron)
+ self._view_cron = None
+ return True
+
+ def set_config(self, params):
+ pass
+
+ def is_running(self):
+ return self._view_cron is not None
+
+ def view_profile(self):
+ try:
+ liked = self._storage.get('liked', default=[])
+
+ n = 0
+ for user in self._browser.find_users():
+ if user['notifier']['id'] in liked:
+ continue
+
+ self._browser.accept(user['notifier']['id'])
+ liked.append(user['notifier']['id'])
+ self._storage.set('liked', liked)
+ self._storage.save()
+ self._logger.info('Liked %s %s (%s at %s)', user['notifier']['first_name'], user['notifier']['last_name'], user['notifier']['job'], user['notifier']['workplace'])
+ n += 1
+ if n > 10:
+ break
+
+ finally:
+ if self._view_cron is not None:
+ self._view_cron = self._sched.schedule(60, self.view_profile)
+
+
+class HappnContact(Contact):
+ def set_profile(self, *args):
+ section = self.profile
+ for arg in args[:-2]:
+ try:
+ s = section[arg]
+ except KeyError:
+ s = section[arg] = ProfileNode(arg, arg.capitalize().replace('_', ' '), OrderedDict(), flags=ProfileNode.SECTION)
+ section = s.value
+
+ key = args[-2]
+ value = args[-1]
+ section[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value)
+
+ def __init__(self, info):
+ status = Contact.STATUS_OFFLINE
+ last_seen = parse_date(info['modification_date'])
+ if last_seen >= datetime.datetime.now(tzlocal()) - datetime.timedelta(minutes=30):
+ status = Contact.STATUS_ONLINE
+
+ super(HappnContact, self).__init__(info['id'], info['display_name'], status)
+
+ self.summary = info['about']
+ for photo in info['profiles']:
+ self.set_photo(photo['id'], url=photo['url'])
+ self.status_msg = u'Last seen at %s' % last_seen.strftime('%Y-%m-%d %H:%M:%S')
+ self.url = NotAvailable
+
+ self.profile = OrderedDict()
+
+ self.set_profile('info', 'id', info['id'])
+ self.set_profile('info', 'full_name', ' '.join([info['first_name'], info['last_name']]))
+ self.set_profile('info', 'login', info['login'])
+ if info['fb_id'] is not None:
+ self.set_profile('info', 'facebook', 'https://www.facebook.com/profile.php?id=%s&fref=ufi&pnref=story' % info['fb_id'])
+ if info['twitter_id'] is not None:
+ self.set_profile('info', 'twitter', info['twitter_id'])
+ self.set_profile('stats', 'accepted', info['is_accepted'])
+ self.set_profile('stats', 'charmed', info['is_charmed'])
+ self.set_profile('stats', 'unread_conversations', info['unread_conversations'])
+ self.set_profile('stats', 'credits', info['credits'])
+ if info['last_meet_position'] is not None:
+ self.set_profile('geoloc', 'last_meet',
+ 'https://www.google.com/maps/place//@%s,%s,17z' % (info['last_meet_position']['lat'],
+ info['last_meet_position']['lon']))
+ if info['distance'] is not None:
+ self.set_profile('geoloc', 'distance', '%.2f km' % (info['distance']/1000.0))
+ self.set_profile('details', 'gender', info['gender'])
+ self.set_profile('details', 'age', '%s yo' % info['age'])
+ self.set_profile('details', 'birthday', info['birth_date'])
+ self.set_profile('details', 'job', info['job'])
+ self.set_profile('details', 'company', info['workplace'])
+ self.set_profile('details', 'school', info['school'])
+ self.set_profile('settings', 'age_min', '%s yo' % info['matching_preferences']['age_min'])
+ self.set_profile('settings', 'age_max', '%s yo' % info['matching_preferences']['age_max'])
+ self.set_profile('settings', 'distance', '%s m' % info['matching_preferences']['distance'])
+ self.set_profile('settings', 'female', info['matching_preferences']['female'])
+ self.set_profile('settings', 'male', info['matching_preferences']['male'])
+
+
+class HappnModule(Module, CapMessages, CapMessagesPost, CapDating, CapContact):
+ NAME = 'happn'
+ DESCRIPTION = u'Happn dating mobile application'
+ MAINTAINER = u'Roger Philibert'
+ EMAIL = 'roger.philibert@gmail.com'
+ LICENSE = 'AGPLv3+'
+ VERSION = '1.1'
+ CONFIG = BackendConfig(Value('username', label='Facebook email'),
+ ValueBackendPassword('password', label='Facebook password'))
+
+ BROWSER = HappnBrowser
+ STORAGE = {'contacts': {},
+ 'liked': [],
+ }
+
+ def create_default_browser(self):
+ facebook = FacebookBrowser()
+ facebook.login(self.config['username'].get(),
+ self.config['password'].get())
+ return HappnBrowser(facebook)
+
+ # ---- CapDating methods -----------------------
+
+ def init_optimizations(self):
+ self.add_optimization('PROFILE_WALKER', ProfilesWalker(self.weboob.scheduler, self.storage, self.browser))
+
+ # ---- CapMessages methods ---------------------
+
+ def fill_thread(self, thread, fields):
+ return self.get_thread(thread)
+
+ def iter_threads(self):
+ for thread in self.browser.get_threads():
+ t = Thread(thread['id'])
+ t.flags = Thread.IS_DISCUSSION
+ for user in thread['participants']:
+ if user['user']['id'] != self.browser.my_id:
+ t.title = u'Discussion with %s' % user['user']['display_name']
+ t.date = local2utc(parse_date(thread['modification_date']))
+ yield t
+
+ def get_thread(self, thread):
+ if not isinstance(thread, Thread):
+ thread = Thread(thread)
+ thread.flags = Thread.IS_DISCUSSION
+
+ info = self.browser.get_thread(thread.id)
+ for user in info['participants']:
+ if user['user']['id'] == self.browser.my_id:
+ me = HappnContact(user['user'])
+ else:
+ other = HappnContact(user['user'])
+
+ thread.title = u'Discussion with %s' % other.name
+
+ contact = self.storage.get('contacts', thread.id, default={'lastmsg': 0})
+
+ child = None
+
+ for msg in info['messages']:
+ flags = 0
+ if int(contact['lastmsg']) < int(msg['id']):
+ flags = Message.IS_UNREAD
+
+ if msg['sender']['id'] == me.id:
+ sender = me
+ receiver = other
+ else:
+ sender = other
+ receiver = me
+
+ msg = Message(thread=thread,
+ id=msg['id'],
+ title=thread.title,
+ sender=sender.name,
+ receivers=[receiver.name],
+ date=local2utc(parse_date(msg['creation_date'])),
+ content=msg['message'],
+ children=[],
+ parent=None,
+ signature=sender.get_text(),
+ flags=flags)
+
+ if child:
+ msg.children.append(child)
+ child.parent = msg
+ child = msg
+ thread.root = child
+
+ return thread
+
+ def iter_unread_messages(self):
+ for thread in self.iter_threads():
+ thread = self.get_thread(thread)
+ for message in thread.iter_all_messages():
+ if message.flags & message.IS_UNREAD:
+ yield message
+
+ def set_message_read(self, message):
+ contact = self.storage.get('contacts', message.thread.id, default={'lastmsg': 0})
+ if int(contact['lastmsg']) < int(message.id):
+ contact['lastmsg'] = int(message.id)
+ self.storage.set('contacts', message.thread.id, contact)
+ self.storage.save()
+
+ # ---- CapMessagesPost methods ---------------------
+
+ def post_message(self, message):
+ self.browser.post_message(message.thread.id, message.content)
+
+ # ---- CapContact methods ---------------------
+ def get_contact(self, contact_id):
+ if isinstance(contact_id, Contact):
+ contact_id = contact_id.id
+
+ info = self.browser.get_contact(contact_id)
+ return HappnContact(info)
+
+ OBJECTS = {Thread: fill_thread,
+ }
diff --git a/modules/happn/test.py b/modules/happn/test.py
new file mode 100644
index 00000000..d5e270e1
--- /dev/null
+++ b/modules/happn/test.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2015 Roger Philibert
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see .
+
+
+from weboob.tools.test import BackendTest
+
+
+class HappnTest(BackendTest):
+ MODULE = 'happn'
+
+ def test_happn(self):
+ for m in self.backend.iter_unread_messages():
+ pass