Package etl :: Package connector :: Module facebook_connector
[hide private]
[frames] | no frames]

Source Code for Module etl.connector.facebook_connector

  1  # -*- encoding: utf-8 -*- 
  2  ############################################################################## 
  3  # 
  4  #    ETL system- Extract Transfer Load system 
  5  #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved 
  6  #    $Id$ 
  7  # 
  8  #    This program is free software: you can redistribute it and/or modify 
  9  #    it under the terms of the GNU General Public License as published by 
 10  #    the Free Software Foundation, either version 3 of the License, or 
 11  #    (at your option) any later version. 
 12  # 
 13  #    This program is distributed in the hope that it will be useful, 
 14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  #    GNU General Public License for more details. 
 17  # 
 18  #    You should have received a copy of the GNU General Public License 
 19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20  # 
 21  ############################################################################## 
 22  """ 
 23  To provide connectivity with Facebook 
 24   
 25  Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). 
 26  GNU General Public License 
 27  """ 
 28  import datetime 
 29  import time 
 30  from etl.connector import connector 
 31   
32 -class facebook_connector(connector):
33 """ 34 This is an ETL connector that is used to provide connectivity with Facebook server. 35 """
36 - def __init__(self, facebook_uri, email, password=False, delay_time=20, name='facebook_connector'):
37 """ 38 Required Parameters 39 facebook_uri : URI of Facebook server. 40 email : Email Address of Facebook User. 41 42 Extra Parameters 43 password : Password. 44 delay_time : Time in sec which is use to wait for login while opening login page in browser. 45 name : Name of connector. 46 """ 47 super(facebook_connector, self).__init__(name) 48 self._type = 'connector.facebook_connector' 49 self.email = email 50 self.delay_time = delay_time 51 self.uid = False 52 self.api_key = '1673458a9d3ddaa8c6f888d7150da256' # TO CHECK 53 self.secret_key = '666197caab406752474bd0c6695a53f6' # TO CHECK 54 self.facebook_uri = facebook_uri
55
56 - def open(self):
57 """ 58 Opens connection to facebook. 59 """ 60 from facebook import Facebook 61 super(facebook_connector, self).open() 62 facebook = Facebook(api_key=self.api_key, secret_key=self.secret_key) 63 auth_token = facebook.auth.createToken() 64 facebook.login(self.email) 65 66 time.sleep(self.delay_time) 67 session = facebook.auth.getSession() 68 return facebook
69
70 - def execute(self, facebook, method, fields):
71 """ 72 Required Parameters 73 facebook : Facebook Object 74 method : Method name like 75 'get_user_info' => Returns information of current user. 76 'get_friends' => Returns all the friends and its information for current user. 77 'get_user_events' => Returns all the events related to current user and members of events. 78 'get_user_groups' => Returns all the groups and its members. 79 'get_user_notes' => Returns notes created by user. 80 'get_user_notification' => Returns information on outstanding Facebook notifications for current session user. 81 'get_user_profile' => Returns the specified user's application info section for the calling application. 82 'get_user_pages' => Returns all visible pages to the filters specified. 83 'get_user_photos' => Returns all visible photos according to the filters specified. 84 'get_user_albums' => Returns metadata about all of the photo albums uploaded by the specified user. 85 'get_user_status' => Returns the user's current and most recent status. 86 'get_user_links' => Returns all links the user has posted on their profile through your application. 87 fields : Fields List 88 """ 89 if method == 'get_user_info': 90 rows = facebook.users.getInfo(facebook.uid, fields) 91 if method == 'get_friends': 92 friends = facebook.friends.get() 93 friends.append(facebook.uid) 94 rows = facebook.users.getInfo(friends, fields) 95 if method == 'get_user_events': 96 rows_user = facebook.users.getInfo(facebook.uid, ['name']) 97 rows = facebook.events.get(facebook.uid) 98 map(lambda x: x.update({'user_name': rows_user[0]['name']}), rows) 99 # for event in event_ids:# can be used 100 # rows_member = facebook.events.getMembers(event) 101 if method == 'get_user_groups': 102 rows = facebook.groups.get() 103 group_ids = map(lambda x: x['gid'], rows) 104 for group in group_ids: 105 rows_member = facebook.groups.getMembers(group) 106 # user notes => Beta 107 if method == 'get_user_notes': 108 rows = facebook.notes.get(facebook.uid) 109 if method == 'get_user_notification': 110 rows = facebook.notifications.get() 111 if method == 'get_user_profile': 112 rows = facebook.profile.getInfo(facebook.uid) 113 if method == 'get_user_pages': 114 rows = facebook.pages.getInfo(uid=facebook.uid, fields=['name','written_by']) #Todo : add more fields 115 # fields_pages = 'name', 'written_by', 'website', 'location (street, city, state, country, zip)', 'founded', 'products', 'produced_by'...etc 116 117 # tobe test : photos.get, photos.getAlbums, status.get , links.get 118 if method == 'get_user_photos': 119 rows = facebook.photos.get(subj_id=facebook.uid) 120 if method == 'get_user_albums': 121 rows = facebook.photos.getAlbums(uid=facebook.uid) 122 if method == 'get_user_status':# Beta 123 rows = facebook.status.get() 124 if method == 'get_user_links': 125 rows = facebook.links.get() 126 127 if method == 'set_events':# to be check 128 perm = facebook.users.hasAppPermission(ext_perm='create_event') 129 event_info = {"name": "Tinyerp Event", "category": "1", "subcategory": "1", "host": "host", "location": "location", "city": "Palo Alto, CA", "start_time": 1215929160, "end_time": 1215929160} 130 event_id = facebook.events.create(event_info=event_info) 131 return rows
132
133 - def __copy__(self):
134 res = facebook_connector(self.facebook_uri, self.email, self.password, self.delay_time)
135
136 - def __copy__(self):
137 """ 138 Overrides copy method. 139 """ 140 res = facebook_connector(self.facebook_uri, self.email, self.password, self.delay_time, self.name) 141 142 return res
143
144 -def test():
145 """ 146 Test function. 147 """ 148 from etl_test import etl_test 149 import etl 150 facebook_conn=facebook_connector('http://facebook.com', 'modiinfo@gmail.com') 151 test = etl_test.etl_component_test(etl.component.input.facebook_in(facebook_conn, 'get_user_events')) 152 res=test.output() 153 print res
154 155 156 if __name__ == '__main__': 157 test() 158