From c16bfd7dc86e0c596bed0b3a6da4aa8c1fc1cf82 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 11 Oct 2014 19:53:22 +0200 Subject: [PATCH] new playme module --- modules/playme/__init__.py | 24 +++++++ modules/playme/browser.py | 108 ++++++++++++++++++++++++++++ modules/playme/favicon.png | Bin 0 -> 1117 bytes modules/playme/module.py | 139 +++++++++++++++++++++++++++++++++++++ modules/playme/test.py | 29 ++++++++ 5 files changed, 300 insertions(+) create mode 100644 modules/playme/__init__.py create mode 100644 modules/playme/browser.py create mode 100644 modules/playme/favicon.png create mode 100644 modules/playme/module.py create mode 100644 modules/playme/test.py diff --git a/modules/playme/__init__.py b/modules/playme/__init__.py new file mode 100644 index 00000000..990f3a3b --- /dev/null +++ b/modules/playme/__init__.py @@ -0,0 +1,24 @@ +# -*- 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 . + + +from .module import PlayMeModule + + +__all__ = ['PlayMeModule'] diff --git a/modules/playme/browser.py b/modules/playme/browser.py new file mode 100644 index 00000000..7add15f6 --- /dev/null +++ b/modules/playme/browser.py @@ -0,0 +1,108 @@ +# -*- 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 re + +from weboob.browser import DomainBrowser +from weboob.browser.pages import HTMLPage +from weboob.browser.profiles import Profile +from weboob.exceptions import BrowserIncorrectPassword +from weboob.tools.json import json + + +__all__ = ['PlayMeBrowser', 'FacebookBrowser'] + + +class FacebookBrowser(DomainBrowser): + BASEURL = 'https://graph.facebook.com' + + CLIENT_ID = "149987128492319" + access_token = None + info = None + + def login(self, username, password): + self.location('https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=https://www.facebook.com/connect/login_success.html&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']) + m = re.search('access_token=([^&]+)&', self.url) + 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 IPhoneClient(Profile): + def setup_session(self, session): + session.headers["Accept-Language"] = "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5" + session.headers["Accept"] = "*/*" + session.headers["User-Agent"] = "PlayMe/3.0.2 (iPhone; iOS 7.1; Scale/2.00)" + session.headers["Accept-Encoding"] = "gzip, deflate" + session.headers["Content-Type"] = "application/json" + + +class PlayMeBrowser(DomainBrowser): + BASEURL = 'https://api2.goplayme.com/' + PROFILE = IPhoneClient() + VERIFY = False + + recs = [] + + def __init__(self, facebook, *args, **kwargs): + super(PlayMeBrowser, self).__init__(*args, **kwargs) + self.facebook = facebook + + profile_picture = 'http%3A%2F%2Fgraph.facebook.com%2F' + facebook.info['id'] + '%2Fpicture%3Fwidth%3D600%26height%3D600' + me = self.request('/auth/facebook/callback?access_token=%s&profile_picture=%s' % (facebook.access_token, profile_picture)) + self.session.headers['Authorization'] = 'Token token="%s"' % me['token'] + + self.my_id = me['id'] + self.my_name = me['name'] + + def get_threads(self): + return self.request('/users/%s/contacts' % self.my_id) + + def get_thread_messages(self, contact_id): + return self.request('/messages/%s' % contact_id) + + def get_user(self, contact_id): + return self.request('/users/%s' % contact_id) + + def post_message(self, contact_id, content): + self.request('/messages', data={'id': contact_id, 'msg': content}) + + def request(self, *args, **kwargs): + if 'data' in kwargs: + kwargs['data'] = json.dumps(kwargs['data']) + + r = self.location(*args, **kwargs) + return json.loads(r.content) diff --git a/modules/playme/favicon.png b/modules/playme/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..9ab8732730a796250c3fb1ce6d8b0906f6d365b6 GIT binary patch literal 1117 zcmV-j1fu(iP)dhQq7_8NB8tZgMQia; z#VarRF}%o^5c~qdP!RFb4HE{@nq0HR(Vg@9Z=#I9 z@GHiueI^3NN)ce>?SRYJ5iXd*MGWCvoW&Jf?<9!40aWU~Qw=xFV;qC{1mEFLEOi1z z-T?k*SyfyQ2MnPUVlS;@-H44@u_aK$Ws!B;qmkvdmYZdL&6qY}sC4~*gjPGbx+ zSj+=PkpR>?VRE# zfN()~?D9i+0WafG++BARvIY|3wWt=Wg#%D`E8K&}qgHq}lH)$y8ltlX6XD-c7fck& z0U>->ob5w+EzbJ^>~2AAP>t65c=Qc(g#Zx3cZbanhw!IxS2&?nb--jKzxQ!2D_QO+ zfI8s^qY8XAEZ^G#;k5+16qVozJ_-OAHoO1Z<$xzc^y?vfe`M!oZN3ur9*^$uM2ytV zV}2`ba^3*mjx*ldr11LPo(-WxA$%CWM_at*`9R(Pj)c{l5njeZ3`IuqBTiX;XSi&2 ztlscoByRvcP1^iw?Dk+ZIU`~Ft6KD)(D6d(KZMsFQAR@aK(xBkA-bi5(e8qXu7u66 z)Sc~tINP&wd`9c;u<-in@Apx?oeG;TwPNU5L;y9L&)^)sjN!#7rn8czcK$QnZ4kSD z$EU15jP{54u}Esmop>9}8^BC-bG5nMXo&8Qws*cAHt!^W591q?vFks?dGEOJb^&78 literal 0 HcmV?d00001 diff --git a/modules/playme/module.py b/modules/playme/module.py new file mode 100644 index 00000000..548972b3 --- /dev/null +++ b/modules/playme/module.py @@ -0,0 +1,139 @@ +# -*- 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 weboob.capabilities.messages import CapMessages, CapMessagesPost, Thread, Message +from weboob.capabilities.dating import CapDating +from weboob.tools.backend import Module, BackendConfig +from weboob.tools.value import Value, ValueBackendPassword +from weboob.tools.date import utc2local + +from .browser import PlayMeBrowser, FacebookBrowser + + +__all__ = ['PlayMeModule'] + + +class PlayMeModule(Module, CapMessages, CapMessagesPost, CapDating): + NAME = 'playme' + DESCRIPTION = u'PlayMe dating mobile application' + MAINTAINER = u'Roger Philibert' + EMAIL = 'roger.philibert@gmail.com' + LICENSE = 'AGPLv3+' + VERSION = '1.0' + CONFIG = BackendConfig(Value('username', label='Facebook email'), + ValueBackendPassword('password', label='Facebook password')) + + BROWSER = PlayMeBrowser + STORAGE = {'contacts': {}, + } + + def create_default_browser(self): + facebook = FacebookBrowser() + facebook.login(self.config['username'].get(), + self.config['password'].get()) + return PlayMeBrowser(facebook) + + # ---- 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 + t.title = u'Discussion with %s' % thread['name'] + t.date = utc2local(datetime.datetime.fromtimestamp(thread['last_message']['utc_timestamp'])) + yield t + + def get_thread(self, thread): + if not isinstance(thread, Thread): + thread = Thread(thread) + thread.flags = Thread.IS_DISCUSSION + + user = self.browser.get_user(thread.id) + thread.title = u'Discussion with %s' % user['name'] + + contact = self.storage.get('contacts', thread.id, default={'lastmsg': 0}) + + signature = u'Age: %s' % user['age'] + signature += u'\nLast online: %s' % user['last_online'] + signature += u'\nPhotos:\n\t%s' % '\n\t'.join([user['photo_host'] + photo['large'] for photo in user['photos']]) + + parent = None + + for msg in self.browser.get_thread_messages(thread.id): + flags = 0 + if int(contact['lastmsg']) < msg['utc_timestamp']: + flags = Message.IS_UNREAD + + if msg['type'] == 'msg': + content = unicode(msg['msg']) + elif msg['type'] == 'new_challenge': + content = u'A new challenge has been proposed!' + elif msg['type'] == 'serie': + content = u"I've played" + elif msg['type'] == 'end_game': + content = u'%s is the winner! (%s VS %s)' % (self.browser.my_name if msg['score']['w'] == self.browser.my_id else user['name'], msg['score']['s'][0], msg['score']['s'][1]) + else: + content = u'Unknown action: %s' % msg['type'] + + msg = Message(thread=thread, + id=msg['utc_timestamp'], + title=thread.title, + sender=unicode(self.browser.my_name if msg['from'] == self.browser.my_id else user['name']), + receivers=[unicode(self.browser.my_name if msg['from'] != self.browser.my_id else user['name'])], + date=utc2local(datetime.datetime.fromtimestamp(msg['utc_timestamp'])), + content=content, + children=[], + parent=parent, + signature=signature if msg['from'] != self.browser.my_id else u'', + flags=flags) + + if parent: + msg.children.append(parent) + parent = msg + thread.root = parent + + 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) + + OBJECTS = {Thread: fill_thread, + } diff --git a/modules/playme/test.py b/modules/playme/test.py new file mode 100644 index 00000000..00b81a7f --- /dev/null +++ b/modules/playme/test.py @@ -0,0 +1,29 @@ +# -*- 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 . + + +from weboob.tools.test import BackendTest + + +class PlayMeTest(BackendTest): + MODULE = 'playme' + + def test_playme(self): + for m in self.backend.iter_unread_messages(): + pass