def get_date_to_daily_engaged_users(graph: facebook.GraphAPI, page_id: str, start_date: str) -> dict: """ returns a dictionary mapping dates to daily engaged users for this page :param graph : facebook.GraphAPI :param page_id : str :param start_date : str (DATE_FORMAT) :return dict<str, int> a dictionary mapping dates to engaged users """ api_data = graph.get_connections( id=page_id, connection_name="insights/page_engaged_users", # reduce one day so that we're able to get the number # of followers at the end of that day as well since=utils.add_days_to_date(start_date, -1), period="day", )["data"][0]["values"] date_to_daily_engaged_users = {} for obj in api_data: value, date = obj["value"], utils.extract_date_from_date_time( obj["end_time"]) date_to_daily_engaged_users[date] = value return date_to_daily_engaged_users
class FacebookResource: FB_URL_PATTERN = re.compile('(?:(?:http|https)://)?(?:www.)?(mbasic.facebook|m\.facebook|facebook|fb)\.(com|me)/') def __init__(self, app_id, app_secret): self.facebook = GraphAPI() self.facebook.access_token = self.facebook.get_app_access_token(app_id, app_secret) def extract_fanpage_name_from_url(self, url: str): if self.FB_URL_PATTERN.match(url, 0): sub_url = self.FB_URL_PATTERN.sub('', url) return sub_url.rsplit('/', 1)[0].replace('/', '') return url def get_all_posts(self, page_name: str): return self.__get_all_connections(page_name, 'posts') def get_all_comments(self, post_id: str): return self.__get_all_connections(post_id, 'comments') def __get_all_connections(self, id: str, connection: str, **args): assert id is not None assert connection is not None while True: page = self.facebook.get_connections(id, connection, **args) for item in page['data']: yield item next_page = page.get('paging', {}).get('next') if not next_page: return args = parse_qs(urlparse(next_page).query) del args['access_token']
def get_friend_event(user, access_token): graph = GraphAPI(access_token) events = graph.get_connections(user['id'], 'events') for event in events['data']: event = EventActions.create(event, user['id']) pass
def get_date_to_page_consumptions(graph: facebook.GraphAPI, page_id: str, start_date: str) -> dict: """ returns a dictionary mapping dates to daily page consumptions that is, the number of times people clicked on any of your content :param graph : facebook.GraphAPI :param page_id : str :param start_date : str (DATE_FORMAT) :return dict<str, int> a dictionary mapping dates to page consumptions """ api_data = graph.get_connections( id=page_id, connection_name="insights/page_consumptions", # reduce one day so that we're able to get the page # consumption for the start date as well since=utils.add_days_to_date(start_date, -1), period="day", )["data"][0]["values"] date_to_daily_page_consumptions = {} for obj in api_data: value, date = obj["value"], utils.extract_date_from_date_time( obj["end_time"]) date_to_daily_page_consumptions[date] = value return date_to_daily_page_consumptions
def get_date_to_page_views_total(graph: facebook.GraphAPI, page_id: str, start_date: str) -> dict: """ returns a dictionary mapping dates to daily page views total that is, the number of times a Page's profile has been viewed by logged in and logged out people :param graph : facebook.GraphAPI :param page_id : str :param start_date : str (DATE_FORMAT) :return dict<str, int> a dictionary mapping dates to daily page views """ api_data = graph.get_connections( id=page_id, connection_name="insights/page_views_total", # reduce one day so that we're able to get the page views # total for the start date as well since=utils.add_days_to_date(start_date, -1), period="day", )["data"][0]["values"] date_to_page_views_total = {} for obj in api_data: value, date = obj["value"], utils.extract_date_from_date_time( obj["end_time"]) date_to_page_views_total[date] = value return date_to_page_views_total
def get_friend_post(user, access_token): graph = GraphAPI(access_token) posts = graph.get_connections(user['id'], 'posts') for post in posts['data']: post = PostActions.create(post, user['id']) pass
def get_object_reactions(graph: facebook.GraphAPI, object_id: str) -> dict: """ returns all the facebook reactions to the given object by object id :param graph : facebook.GraphAPI :param object_id : str :return : dict<str, int> """ reactions = {} for fb_reaction_type in FB_REACTIONS: try: count = graph.get_connections( id=object_id, connection_name="reactions", summary=True, type=fb_reaction_type, )["summary"]["total_count"] if count > 0: reactions[fb_reaction_type.lower()] = count except facebook.GraphAPIError: print("GraphAPIError Occurred for reaction type %s for " "object id %s" % (fb_reaction_type, object_id)) return reactions
def profile_handler(request, socket, context, channel): """ Event handler for online status in new users at home. """ usr = User.objects.get(id=request.user.id) usr.is_active = False; usr.save() print str(request.user.first_name)+' '+request.user.last_name+' setting online' try: session, created = Session.objects.get_or_create(id_websocket = request.user.id) if created == False: session.time = True session.save() else: session.time = False session.save() token = UserSocialAuth.objects.get(user__username=request.user.username, provider='facebook').extra_data['access_token'] uid = UserSocialAuth.objects.get(user__username=request.user.username, provider='facebook').uid graph = GraphAPI(token) friends = graph.get_connections(uid, "friends", fields="installed,id,name")['data'] for friend in friends: if friend.get("installed"): social_usr = UserSocialAuth.objects.get(uid=friend.get("id"), provider='facebook') #Searching for the user to call in db dbfriend = User.objects.get(id=social_usr.user_id) if dbfriend.is_active == False: online_message = {"id": str(dbfriend.id), "action": "friend_online", "message": str(request.user.id)} print online_message try: socket.broadcast_channel(online_message, channel='user-'+str(dbfriend.id)) except NoSocket, e: print e except: print "error subscribe"
def get_date_to_page_fans(graph: facebook.GraphAPI, page_id: str, start_date: str) -> tuple: """ gets the number of page fans (page likes) on facebook for that page :param graph : facebook.GraphAPI :param page_id : str :param start_date : str (DATE_FORMAT) :return tuple<dict<str, int>, str> A tuple with two things: - A dictionary mapping date (in format YYYY-MM-DD) to integers representing the number of page fans at that date - A string representing the earlier valid date that we have (this is the date that has at least 1 fan, which may or may not be start_date) """ api_data = graph.get_connections( id=page_id, connection_name="insights/page_fans", # reduce one day so that we're able to get the number # of followers at the end of that day as well since=utils.add_days_to_date(start_date, -1), )["data"][0]["values"] date_to_page_fans, valid_start_date = {}, None for obj in api_data: value, date = obj.get("value", None), obj.get("end_time", None) if value is None or date is None: continue date_to_page_fans[utils.extract_date_from_date_time(date)] = value if valid_start_date is None and value > 0: valid_start_date = date return date_to_page_fans, valid_start_date
def room(request, name, slug): """ Show a room. """ if request.user.username == name: c = {} c.update(csrf(request)) context = {"room": get_object_or_404(Call, slug=slug)} #print request.user #template="space/room.html" owner=request.user try: owner.token = UserSocialAuth.objects.get(user__username=owner.username, provider='facebook').extra_data['access_token'] owner.uid = UserSocialAuth.objects.get(user__username=owner.username, provider='facebook').uid graph = GraphAPI(owner.token) friends = graph.get_connections(owner.uid, "friends", fields="installed,id,name")['data'] me = graph.get_object("me") except: uid = '' token = '' friends = '' me = '' return render_to_response('space/room.html',context, RequestContext(request,{'owner': owner, 'me': me})) #return render(request, template, context) else: raise Http404
def show_post_form_pages(): """Show posting for for Facebook Pages""" if 'user_id' not in session: flash("You need to be logged in for that!") return redirect('/') user_id = session["user_id"] user = User.query.filter_by(user_id=user_id).first() username = user.username facebook_info = FacebookInfo.query.filter_by(user_id=user_id).first() if not facebook_info: flash("You need to log into Facebook first!") return redirect('/') access_token = facebook_info.access_token api = GraphAPI(access_token) page_response = api.get_connections("me", "accounts") return render_template("post_pages.html", username=username, pages=page_response["data"])
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) data = { "text": [], "links": [] } posts = [] posts_blob = yield facebook_paginate( graph.get_connections( 'me', 'posts', fields='message, link, created_time' ), max_results=self.num_posts_per_user ) posts, links = self.message_filter(posts_blob) # To do comments we'd have to go through the different posts and look # Scraping the person's feed is another option, but we get more garbage data['text'] = posts data['links'] = links return data
def get_page_feed_for_date_range(graph: facebook.GraphAPI, page_id: str, start_date: str, end_date: str) -> list: """ gets the posts from the page within that time frame :param graph : facebook.GraphAPI :param page_id : str :param start_date : str (DATE_FORMAT) :param end_date : str (DATE_FORMAT) :return : list[dict<str, primitive>] a list of post dictionary """ api_data = graph.get_connections( id=page_id, connection_name="posts", limit=100, since=start_date, until=end_date, )["data"] posts = [] for post_data in api_data: message = post_data.get("message", None) if message is None: continue posts.append({ "created_time": post_data["created_time"], "story": post_data.get("story", None), "post_id": post_data["id"], "message": message, }) return posts
def getFBPosts(station,function): """ Gets the most recent posts from a Facebook page since the last run :param station: (int) id of the station :param function: (int) id of the function :return: """ ##Gets the url after the bot has been added url = StationhasBots.query.filter(StationhasBots.fk_radio_station_id==station,StationhasBots.fk_bot_function_id==function).first() # Gets an access token to allow us to fetch information from FB pages r = requests.get('https://graph.facebook.com/oauth/access_token?client_id='+FB_APP_ID+'&client_secret='+FB_APP_SECRET+'&grant_type=client_credentials') access_token = r.text[13:] graph = GraphAPI(access_token) linkCut(url.source_url) profile = graph.get_object(linkCut(url.source_url)) # Gets the last info that was introduced into the database last_bot_info = Bothasinfo.query.filter(Bothasinfo.fk_station_has_bots_radio_station_id == station, Bothasinfo.fk_station_has_bots_bot_function_id == function).order_by(Bothasinfo.created_at.desc()).first() if last_bot_info == None: last_info_date = datetime.now() - timedelta(days=7) else: last_info_date = last_bot_info.created_at posts = graph.get_connections(profile['id'], 'posts') for post in posts['data']: # This condition grants that we only get info from posts if 'message' in post: if datetime.strptime(post['created_time'], "%Y-%m-%dT%H:%M:%S+%f") > last_info_date: info = validate_sms(post['message']) new_info = Bothasinfo(created_at = post['created_time'], fk_station_has_bots_radio_station_id = station , fk_station_has_bots_bot_function_id = function, info = info) db.session.add(new_info) db.session.commit()
def profile_handler_disconnect(request, socket, context): """ Event handler for offline status in new users at home. """ usr = User.objects.get(id=request.user.id) print str(request.user.first_name)+' '+request.user.last_name+' setting offline' #Handles the error of refreshing the page by using a flag selector in the avalialbe socket table try: session = Session.objects.get(id_websocket = request.user.id) if session.time == False: print "deleting user..." usr.is_active = True; usr.save() session.delete() #Provisional way to alert friends of offline status token = UserSocialAuth.objects.get(user__username=request.user.username, provider='facebook').extra_data['access_token'] uid = UserSocialAuth.objects.get(user__username=request.user.username, provider='facebook').uid graph = GraphAPI(token) friends = graph.get_connections(uid, "friends", fields="installed,id,name")['data'] for friend in friends: if friend.get("installed"): social_usr = UserSocialAuth.objects.get(uid=friend.get("id"), provider='facebook') #Searching for the user to call in db dbfriend = User.objects.get(id=social_usr.user_id) if dbfriend.is_active == False: offline_message = {"id": str(dbfriend.id), "action": "friend_offline", "message": str(request.user.id)} print offline_message try: socket.broadcast_channel(offline_message, channel='user-'+str(dbfriend.id)) except NoSocket, e: print e else:
def Look_for_comments(vendor): print(vendor['page_id']) graph = GraphAPI(access_token=vendor['access_token']) print('Started at {} ....'.format(last_time_function_ran)) posts = graph.get_connections( id=vendor['page_id'], connection_name='posts', fields='updated_time,comments{message,created_time}') data = posts['data'] for post in data: post_updated_time = datetime.strptime( post['updated_time'], time_format) if post_updated_time > last_time_function_ran: for comment in post['comments']['data']: comment_created_time = datetime.strptime( comment['created_time'], time_format) if not check_for_comments(comment['id'], graph, vendor): print('Found Comment!') msg = ask_wit(comment['message'], vendor) if msg: graph.put_comment(object_id=comment['id'], message=msg) print('Replied to Comment') print('...Finished at {}'.format(last_time_function_ran))
def fix_facebook_ids(): """ Retroactively adds Facebook IDs to Facebook table """ db = dataset.connect(app_config.POSTGRES_URL) table = db['facebook'] min_created_time_result = list(db.query('select min(created_time) as min_created_time from facebook')) min_created_time = min_created_time_result[0]['min_created_time'] graph = GraphAPI(SECRETS['FACEBOOK_TOKEN']) profile = graph.get_object(FACEBOOK_USER) posts = graph.get_connections(profile['id'], 'posts') done = False while not done: print 'processing batch of 25' for post in posts['data']: created_time = parser.parse(post['created_time'], ignoretz=True) if created_time < min_created_time: done = True break if 'link' not in post.keys(): print 'skipping %s (no link)' % post['id'] continue link = post['link'].split('?')[0] print 'updating %s (%s)' % (link, post['id']) table.update({ 'facebook_id': post['id'], 'link_url': link, }, ['link_url']) posts = requests.get(posts['paging']['next']).json()
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) photos_friends_profile = yield self.get_friends_profile(graph) photos_me = yield facebook_paginate( graph.get_connections( 'me', 'photos', fields='images,tags.limit(50)' ), max_results=self.num_images_per_user ) photos_uploaded = yield facebook_paginate( graph.get_connections( 'me', 'photos', fields='images,tags.limit(50)', type='uploaded' ), max_results=self.num_images_per_user ) photos_friends_raw = yield facebook_paginate( graph.get_connections( 'me', 'friends', fields='photos.limit(1){tags.limit(100),images}' ), max_results=self.num_images_per_user ) photos_friends = [] for d in photos_friends_raw: try: for photo in d['photos']['data']: photos_friends.append(photo) except KeyError: pass photos_me = yield self.parse_photos(graph, photos_me) photos_uploaded = yield self.parse_photos(graph, photos_uploaded) photos_friends = yield self.parse_photos(graph, photos_friends) return {'me': photos_me, 'friends': photos_friends, 'friends_profile': photos_friends_profile, 'uploaded': photos_uploaded}
def get_graph_data(user, path, data=None): data = data or {} try: api = GraphAPI(user.access_token) r = api.get_connections('me', path, **data) except GraphAPIError: return GraphAPIError return r
def get_friends(request): try: graph = GraphAPI(request.user.accesstoken) user = graph.get_object('me') friends = graph.get_connections(user["id"], "friends") return friends except GraphAPIError as e: print >> sys.stderr, 'Facebook error: %s', (e, ) sys.stderr.flush()
def get_posts(facebook_api: facebook.GraphAPI, type_post: str) -> dict: """ Returns own posts or visitor posts based on type_post Arguments: facebook_api (facebook.GraphAPI) : object facebook API type_post (str) : indicator of type posts (Ej published_posts, visitor_posts) Returns: dict """ return facebook_api.get_connections(id = 'me', connection_name = type_post)
def get_permissions(): access_token = session.get('access_token', None) if not access_token: return jsonify(permissions=None, error="Missing access token.") try: graph = GraphAPI(access_token) permissions = graph.get_connections(id='me', connection_name='permissions') except GraphAPIError as e: return jsonify(permissions=None, error=e.result) return jsonify(permissions=permissions)
def get_page_list(fb_access_token): graph = GraphAPI(fb_access_token) page_list = [] response = graph.get_connections('me', 'accounts') for data in response['data']: page_dict = {'id': data['id'], 'name': data['name'], 'access_token': data['access_token'], } page_list.append(page_dict) return page_list
def get_posts(user="******"): access_token = session.get("token", None) graph = GraphAPI(access_token) profile = graph.get_object(user) posts = graph.get_connections(profile['id'], 'feed') posts_list = [] for post in posts['data']: posts_list.append(post) return posts_list
def interest_page(request, fb_id): try: interest = Interest.objects.get(fb_id=fb_id) graph = GraphAPI() graph_data = graph.get_connections(fb_id, "/") name = graph_data["name"] items = get_products_from_interest(interest) return render_to_response( "interests/view.html", {"fb_id": fb_id, "fb_name": name, "items": items}, context_instance=RequestContext(request), ) except Interest.DoesNotExist: return render_to_response("interests/view_404.html", {}, context_instance=RequestContext(request))
def get_post_data(graph: facebook.GraphAPI, post_id: str) -> dict: """ get the comments and each facebook reactions from the post and its post id :param graph : facebook.GraphAPI :param post_id : str :return : dict<key, int | dict<key, int>> """ comments = graph.get_connections( id=post_id, connection_name="comments", summary=True, )["summary"]["total_count"] reactions = graph.get_connections( id=post_id, connection_name="insights/post_reactions_by_type_total", )["data"][0]["values"][0]["value"] return { "comments": comments, "reactions": reactions, }
def home(request, name): """ It shows the profile of the authenticated user. Also redirect to another user space through the finder. Saves the token into usr.token for accessing FB data If the user doesn't exist in the database, It shows the errors and exceptions properly . """ c = {} c.update(csrf(request)) try: usr = User.objects.get(username=name) try: usr.token = UserSocialAuth.objects.get(user__username=usr.username, provider='facebook').extra_data['access_token'] usr.uid = UserSocialAuth.objects.get(user__username=usr.username, provider='facebook').uid graph = GraphAPI(usr.token) friends = graph.get_connections(usr.uid, "friends", fields="installed,id,name")['data'] me = graph.get_object("me") friends_local = [] for friend in friends: if friend.get("installed"): social_usr = UserSocialAuth.objects.get(uid=friend.get("id"), provider='facebook') #Searching for the user to call in db dbfriend = User.objects.get(id=social_usr.user_id) sample = {"id": str(dbfriend.id), "name": dbfriend.first_name+' '+dbfriend.last_name, "online": dbfriend.is_active, "username": dbfriend.username} friends_local.append(sample) except: uid = '' token = '' friends = '' me = '' friends_local = [] x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: usr.ip = x_forwarded_for.split(',')[-1].strip() else: usr.ip = request.META.get('REMOTE_ADDR') except User.DoesNotExist: raise Http404 if request.method == 'POST': another_user = request.POST.get('another_user') try: exist = User.objects.get(username=another_user) except User.DoesNotExist: return render_to_response("space/user_not_exist.html", RequestContext(request,{})) return HttpResponseRedirect("/" + another_user + "/") return render_to_response("space/profile.html", c, RequestContext(request,{ 'user':request.user, 'owner':usr, 'friends':friends, 'me':me, 'friends_local':friends_local }))
def my_form_post(): status = request.form['status'] print(status) graph = GraphAPI(FB_ACCESS_TOKEN_PAGE) profile = graph.get_object("me") graph.put_object(profile['id'], "feed", message=status) if g.user: posts = graph.get_connections(profile["id"], "posts") img_page = "https://graph.facebook.com/" + profile['id'] +"/picture?type=large" img_usr = "******" + g.user['id'] +"/picture?type=large" return render_template( "index.html", app_id=FB_APP_ID, app_name=FB_APP_NAME, user=g.user, page=profile["name"], img_usr=img_usr, img_page=img_page, posts=posts["data"] ) # Otherwise, a user is not logged in. return render_template("login.html", app_id=FB_APP_ID, name=FB_APP_NAME)
def run(self): print("Get friends datas from facebook") user = UserActions.find_by_id('118600698523150') graph = GraphAPI(user.access_token) args = {'fields': 'birthday, name, email'} friends = graph.get_object('me/friends', **args) for friend in friends['data']: UserActions.create(friend) posts = graph.get_connections(friend['id'], 'posts') for post in posts['data']: post = PostActions.create(post, friend) print(post) pass
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) likes = yield facebook_paginate(graph.get_connections( 'me', connection_name='likes'), max_results=self.num_likes_per_user) data = {'likes': []} for like in likes: data['likes'].append(like['name']) return data
def run(self): print("Get friends datas from facebook") user = UserActions.find_by_id('118600698523150') graph = GraphAPI(user.access_token) args = {'fields' : 'birthday, name, email'} friends = graph.get_object('me/friends', **args) for friend in friends['data']: UserActions.create(friend) posts = graph.get_connections(friend['id'], 'posts') for post in posts['data']: post = PostActions.create(post, friend) print(post) pass
def scrape(self, user_data): try: oauth = user_data.services["facebook"]["access_token"] except KeyError: return False graph = GraphAPI(access_token=oauth) photos_friends_profile = yield self.get_friends_profile(graph) photos_me = yield facebook_paginate( graph.get_connections("me", "photos", fields="images,tags.limit(50)"), max_results=self.num_images_per_user ) photos_uploaded = yield facebook_paginate( graph.get_connections("me", "photos", fields="images,tags.limit(50)", type="uploaded"), max_results=self.num_images_per_user, ) photos_friends_raw = yield facebook_paginate( graph.get_connections("me", "friends", fields="photos.limit(1){tags.limit(100),images}"), max_results=self.num_images_per_user, ) photos_friends = [] for d in photos_friends_raw: try: for photo in d["photos"]["data"]: photos_friends.append(photo) except KeyError: pass photos_me = yield self.parse_photos(graph, photos_me) photos_uploaded = yield self.parse_photos(graph, photos_uploaded) photos_friends = yield self.parse_photos(graph, photos_friends) return { "me": photos_me, "friends": photos_friends, "friends_profile": photos_friends_profile, "uploaded": photos_uploaded, }
def gen_babble(): limit = request.args.get('limit') or REQUEST_LIMIT friendId = request.args.get('id') or session['friend']['id'] access_token = session.get('access_token', None) if not access_token: return jsonify(babble=None, error="Missing access token.") try: graph = GraphAPI(access_token) posts = graph.get_connections(id=friendId, connection_name='posts', fields='message', limit=limit) except GraphAPIError as e: return jsonify(babble=None, error=e.result) dialogue = babble_posts(posts) return jsonify(babble=dialogue)
def show_albums(facebook_api: facebook.GraphAPI, albums_id: list) -> None: """ Prints a list of albums of the user Arguments: facebook_api (facebook.GraphAPI) albums_id (list): Contains the albums of the user """ albums = facebook_api.get_connections(id = 'me', connection_name = 'albums') info_list = albums['data'] print_write_chatbot("Your albums are: ") for count, info in enumerate(info_list, 1): print(count, info["name"]) albums_id.append(info["id"])
class FacebookAccount(OAuthAccount): """OAuth impl for FaceBook""" AUTH_URL="https://graph.facebook.com/oauth/authorize" TOKEN_URL="https://graph.facebook.com/oauth/access_token" def __init__(self): OAuthAccount.__init__(self, None, FB_CLIENT_ID, FB_CLIENT_SECRET, self.AUTH_URL, self.TOKEN_URL, scope='email, user_about_me, user_birthday, user_status, publish_actions', state="auth_provider=facebook") self.graph = None def get_user(self): '''Returns the user using the Graph API. ''' if not self.accessToken(): return None if not self.graph: self.graph = GraphAPI(self.accessToken()) user = None picture = None try: user = self.graph.get_object("me", fields="email, first_name, last_name") picture = self.graph.get_connections(user["id"], "picture", redirect=False) profile_picture_url = picture["data"]["url"] except GraphAPIError, e: current.session.token = None self.graph = None if user: if not user.has_key('username'): username = user['id'] else: username = user['username'] if not user.has_key('email'): email = '%s.fakemail' %(user['id']) else: email = user['email'] return dict(first_name = user['first_name'], last_name = user['last_name'], username = username, email = '%s' %(email), profile_picture_url = profile_picture_url)
def troll(): # 최근 실행에서 저장된 feed id fid = None try: f = open('fid.txt', 'r') fid = f.read().strip() f.close() except IOError: pass print('* Last feed id:', fid) graph = GraphAPI(access_token=token) print('* Retrieving feeds...') feeds = graph.get_connections(id='674366342659988', connection_name='feed', fields='message,id')['data'] print('Done') # 받아온 피드 중 가장 최신 피드의 id lastest_feed_id = None for feed in feeds: feed_id = feed['id'] feed_id = str(feed_id) # 받아온 피드 중 가장 최신의 피드 id 저장 if lastest_feed_id is None: lastest_feed_id = feed_id # 최근 실행에서 처리한 피드일 경우 종료 if fid == feed_id: break print('* Publishing comment on %s...' % feed_id) try: graph.put_comment(object_id=feed_id, message='일 안함? ㅡㅡ') except GraphAPIError as e: print(e) print('Failed') continue print('Done') # 중복검사를 위해 가장 최신 피드 id 저장 f = open('fid.txt', 'w') f.write(lastest_feed_id) f.close() print('* Finished.')
def _get_parser(self): api = GraphAPI(self.settings["facebook_access_token"]) events = api.get_connections(self.profile_id(), "events") today = datetime.today() event_ids = [] for event_info in events["data"]: start_time = date_util.parse_date_time(event_info["start_time"]) if start_time >= today: event_ids.append(event_info["id"]) if event_ids: parse_events = api.get_objects(event_ids) for event in parse_events.values(): yield self._parse_show(api, event)
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) events = yield facebook_paginate(graph.get_connections( 'me', connection_name='events'), max_results=self.num_events_per_user) data = {"events": []} for item in events: event = {} event['description'] = item.get('description', None) event['name'] = item.get('name', None) event['status'] = item.get('rsvp_status', None) data['events'].append(event) return data
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) events = yield facebook_paginate( graph.get_connections('me', connection_name='events'), max_results=self.num_events_per_user) data = {"events": []} for item in events: event = {} event['description'] = item.get('description', None) event['name'] = item.get('name', None) event['status'] = item.get('rsvp_status', None) data['events'].append(event) return data
def index(): # If a user was set in the get_current_user function before the request, # the user is logged in. if g.user: graph = GraphAPI(FB_ACCESS_TOKEN_PAGE) profile = graph.get_object("me") posts = graph.get_connections(profile["id"], "posts") img_page = "https://graph.facebook.com/" + profile['id'] +"/picture?type=large" img_usr = "******" + g.user['id'] +"/picture?type=large" for post in posts["data"]: up_post = Posts(id=str(profile["id"]), post=post["message"]) if Posts.query.filter_by(id = profile['id'], post=post['message']) == None: db.session.add(up_post) db.session.commit() return render_template( "index.html", app_id=FB_APP_ID, app_name=FB_APP_NAME, user=g.user, page=profile["name"], img_usr=img_usr, img_page=img_page, posts=posts["data"] ) # Otherwise, a user is not logged in. return render_template("login.html", app_id=FB_APP_ID, name=FB_APP_NAME)
def GetFriends(self): if not self.access_token: if not self.GetToken(): return False self.contacts = [] print self.access_token graph = GraphAPI(self.access_token) friends = graph.get_connections("me", "friends") for friend in friends["data"]: print "Adding", friend["name"] info = graph.get_object("%s" % (friend["id"])) print info picture = self.PHOTO_URI % (friend["id"]) self.contacts.append(FacebookContact(info, picture)) return True
def get_my_facebook_friends(self): access_token = self.social_auth.get(provider="facebook").extra_data.get("access_token") cache_key = '%s:%s' % (str(self.id), BanyanUser.FB_FRIENDS_CACHE_KEY) ''' First try to get the friend's list from cache if available If not, do a network request ''' my_friends = cache.get(cache_key) if my_friends is None: try: from facebook import GraphAPI graph = GraphAPI(access_token) result = graph.get_connections("me", "friends") my_friends = result.get('data', []) cache.set(cache_key, my_friends, 3600) # 1 hour timeout except: logger.error("accounds.models.BanyanUser:get_my_facebook_friends:GraphAPI err {}{} user:{}".format(sys.exc_info()[0], sys.exc_info()[1], self.id)) return [] return my_friends
def get_friends(): # TODO: paginate friends access_token = session.get('access_token', None) if not access_token: return jsonify(friends=None, error="Missing access token.") try: graph = GraphAPI(access_token) profiles = graph.get_connections(id='me', connection_name='friends', limit=REQUEST_LIMIT) except GraphAPIError as e: return jsonify(friends=None, error=e.result) # TODO: include privacy filters? friends = profiles['data'] if not friends: # you have no friends :( return jsonify(friends=None) return jsonify(friends=friends)
def scrape(self, user_data): try: oauth = user_data.services['facebook']['access_token'] except KeyError: return False graph = GraphAPI(access_token=oauth) data = {"text": [], "links": []} posts = [] posts_blob = yield facebook_paginate( graph.get_connections('me', 'posts', fields='message, link, created_time'), max_results=self.num_posts_per_user) posts, links = self.message_filter(posts_blob) # To do comments we'd have to go through the different posts and look # Scraping the person's feed is another option, but we get more garbage data['text'] = posts data['links'] = links return data
def get_posts(): if g.user: page_id = request.args.get('page_id') since = request.args.get('since') until = request.args.get('until') graph = GraphAPI(g.user['access_token']) all_posts = [] posts = graph.get_connections(page_id, 'posts', since=since, until=until) while True: try: for post in posts['data']: all_posts.append(dict(copy.deepcopy(post))) posts = requests.get(posts['paging']['next']).json() except KeyError: # When there are no more pages (['paging']['next']), break from the # loop and end the script. break return json.dumps(all_posts) else: return redirect(url_for('logout'))
def get_my_facebook_friends(self): access_token = self.social_auth.get( provider="facebook").extra_data.get("access_token") cache_key = '%s:%s' % (str(self.id), BanyanUser.FB_FRIENDS_CACHE_KEY) ''' First try to get the friend's list from cache if available If not, do a network request ''' my_friends = cache.get(cache_key) if my_friends is None: try: from facebook import GraphAPI graph = GraphAPI(access_token) result = graph.get_connections("me", "friends") my_friends = result.get('data', []) cache.set(cache_key, my_friends, 3600) # 1 hour timeout except: logger.error( "accounds.models.BanyanUser:get_my_facebook_friends:GraphAPI err {}{} user:{}" .format(sys.exc_info()[0], sys.exc_info()[1], self.id)) return [] return my_friends
def sync_user_fb_interests(user): fb_account = FacebookAccount.objects.get(user=user) fb_id = fb_account.social_id fb_token = fb_account.token graph = GraphAPI(fb_token) graph_likes = graph.get_connections(fb_id, "likes") likes = graph_likes['data'] for like in likes: like_name = like['name'] like_category = like['category'] like_id = like['id'] try: like_date = like['created_time'] like_creation = strptime(like_date[:-5],"%Y-%m-%dT%H:%M:%S") like_datecreation = datetime.date(like_creation.tm_year, like_creation.tm_mon, like_creation.tm_mday) except KeyError: like_datecreation = datetime.date.today() (category, created) = InterestCategory.objects.get_or_create(name=like_category) interests = Interest.objects.filter(fb_id=like_id) if len(interests) == 0: interest = Interest(name=like_name, category=category, fb_id=like_id) else: interest = interests[0] interest.name = like_name interest.category = category interest.save() user_interests = UserInterests.objects.filter(user=user, interest=interest) if len(user_interests) == 0: user_interest = UserInterests.objects.create(user=user, interest=interest, created=like_datecreation) else: user_interest = user_interests[0] user_interest.created = like_datecreation user_interest.save()
class FacebookResource: FB_URL_PATTERN = re.compile( '(?:(?:http|https)://)?(?:www.)?(mbasic.facebook|m\.facebook|facebook|fb)\.(com|me)/' ) def __init__(self, app_id, app_secret): self.facebook = GraphAPI() self.facebook.access_token = self.facebook.get_app_access_token( app_id, app_secret) def extract_fanpage_name_from_url(self, url: str): if self.FB_URL_PATTERN.match(url, 0): sub_url = self.FB_URL_PATTERN.sub('', url) return sub_url.rsplit('/', 1)[0].replace('/', '') return url def get_all_posts(self, page_name: str): return self.__get_all_connections(page_name, 'posts') def get_all_comments(self, post_id: str): return self.__get_all_connections(post_id, 'comments') def __get_all_connections(self, id: str, connection: str, **args): assert id is not None assert connection is not None while True: page = self.facebook.get_connections(id, connection, **args) for item in page['data']: yield item next_page = page.get('paging', {}).get('next') if not next_page: return args = parse_qs(urlparse(next_page).query) del args['access_token']
def fix_facebook_ids(): """ Retroactively adds Facebook IDs to Facebook table """ db = dataset.connect(app_config.POSTGRES_URL) table = db['facebook'] min_created_time_result = list( db.query('select min(created_time) as min_created_time from facebook')) min_created_time = min_created_time_result[0]['min_created_time'] graph = GraphAPI(SECRETS['FACEBOOK_TOKEN']) profile = graph.get_object(FACEBOOK_USER) posts = graph.get_connections(profile['id'], 'posts') done = False while not done: print 'processing batch of 25' for post in posts['data']: created_time = parser.parse(post['created_time'], ignoretz=True) if created_time < min_created_time: done = True break if 'link' not in post.keys(): print 'skipping %s (no link)' % post['id'] continue link = post['link'].split('?')[0] print 'updating %s (%s)' % (link, post['id']) table.update({ 'facebook_id': post['id'], 'link_url': link, }, ['link_url']) posts = requests.get(posts['paging']['next']).json()
def found_cb(results): friends_number = 0 followers_number = 0 since_id = 0 #let's see if the object has some nice things in it. try: config_returned = results['data']['service_facebook_config'] friends_number = int(config_returned['friends_list_size'][0]['@value']) followers_number = int(config_returned['followers_list_size'][0]['@value']) since_id = int(config_returned['since_id'][0]['@value']) logging.debug('Found the Facebook Config Object.') except: #print sys.exc_info() pass logging.info('Facebook Service - Getting users Facebook Friends') graph = GraphAPI(self.facebook_access_token_long) #profile = graph.get_object("me") #rint profile friends_all = graph.get_connections("me", "friends", limit=1000) friends = friends_all['data'] friends_number = len(friends) logging.debug('Facebook Service - Got users Facebook Friends: {0}'.format(friends_number)) objects_to_insert = [] timestamp = str(datetime.datetime.now().isoformat('T')).split(".")[0] friend_ids = [] counter = 0 friends_objs_to_insert = [] for friend in friends: uniq_id = "facebook_user_"+str(friend['id']) friend_obj = {"@id":uniq_id, "app_object": app_id, "timestamp":timestamp, "type": "user", "facebook_user_id": friend['id'], "facebook_username": friend['name']} friend_ids.append(uniq_id) friends_objs_to_insert.append(friend_obj) try: #now create the me facebook user friend object profile = graph.get_object("me") facebook_id = str(profile['id']) facebook_username = str(profile['name']) uniq_id = "facebook_friends_for_user_me" facebook_user_friend_obj = {"@id":uniq_id, "app_object": app_id, "timestamp":timestamp, "faceboob_user_id_indx": "facebook_user_me", "facebook_user_id": facebook_id, "facebook_username": facebook_username, "facebook_friends_ids_indx": friend_ids} objects_to_insert.append(facebook_user_friend_obj) except: pass #now set the config facebook_config_obj = {"@id": "service_facebook_config", "app_object": app_id, "type":"config", "config_last_updated_at": timestamp, "config_for_facebook_user_id": facebook_id, "friends_list_generated_at": timestamp, "follower_list_generated_at": timestamp, "friends_list_size":friends_number, "followers_list_size": followers_number, "since_id":since_id} objects_to_insert.append(facebook_config_obj) def insertfriends(self): if (len(friends_objs_to_insert) > 0): def insert_friend_cb(re): insertfriends(self) def insert_friend_cb_fail(re): insertfriends(self) friends_to_add = [] pop_point = 0 for x in range(0, 100): try: friends_to_add.append(friends_objs_to_insert[x]) pop_point += 1 except: pass for x in range(0, pop_point): try: friends_objs_to_insert.pop(0) except: pass #friend_object = friends_objs_to_insert[0] #print "friends list length: "+str(len(friends_objs_to_insert)) self.insert_object_to_indx(friends_to_add).addCallbacks(insert_friend_cb, insert_friend_cb_fail) else: logging.info("All Facebook Friends Added to Indx, will now proceed.") #this is a trick as indxclient is a bit nasty with big inserts logging.debug("Inserting friends into indx") insertfriends(self) #now need to perform the asnc def insert_cb(re): logging.debug("Facebook Service - Found Friends List, Added To INDX {0} Friends".format(len(friends))) harvest_friends_d.callback(True) def insert_cb_fail(re): harvest_friends_d.callback(True) logging.debug("inserting friends config into indx") self.insert_object_to_indx(objects_to_insert).addCallbacks(insert_cb, insert_cb_fail)
################################ # # # Get FB Fan-pages users like # # # ################################ from facebook import GraphAPI from urllib.request import urlopen import json token = "EAACEdEose0cBABmtSOWXZASD7QQVZAt4GGBF23XUFxENE98yhLsZANsvOg3iioxoomR8p051nK7BCYBn4XwuSujAGXox59hqYpyC2FiPvXsRppxjUTYHUB5Bd4ZBaQ0x1ZCcGcrojp4kf2dzhSz6IUibgDFJZAkgcm8yTGsNejR0qjdZBpAvX6NLfsg0ajeD07gogBDrHBRDAZDZD" g = GraphAPI(access_token = token) ct = 0 pages = g.get_connections("me", "likes") while True: for page in pages["data"]: print("%d."%ct, page["name"]) ct += 1 try: url = pages["paging"]["next"] print(url) response = urlopen(url) pages = json.load(response) except KeyError: break;
class FacebookClient(AbstractSocialClient): """ Summary: The FacebookClient class wraps an instance of the facebook.GraphAPI class. This class has built in support for common tasks like pagination and keyword matching to extract relevant data points for the Facebook platform. """ def __init__(self, access_token: str): """ Summary: Creates an instance of FacebookClient Args: access_token: your facebook graph api access token Returns: An instance of the FacebookClient class """ self.facebook = GraphAPI(access_token=access_token) def get_page(self, sourceName: str) -> (list, list): """ Summary: Gets the first page of results and a link to the next page of results for a top level page attribute of facebook. Args: sourceName: the name of the social media page to be searched for data Returns: dataPage: a list of individual data points taken from the api response nextPageLink: a list with info needed to link to the next page of data """ sourceId = self.facebook.get_object(sourceName)["id"] rawData = self.facebook.get_connections( sourceId, "posts", fields="permalink_url,message,name,id", limit=100) dataPage = rawData["data"] nextPageLink = [rawData["paging"]["next"]] return dataPage, nextPageLink def update_page(self, nextPageLink: list) -> (list, list): """ Summary: Updates the current data page to provide new data in the executing loop (search). Gets a list of data results and a link to the next page of data. Args: nextPageLink: a link to the next page of data results Returns: dataPage: a list of individual data points taken from the api response nextPageLink: a string linking to the next page of data """ rawData = requests.get(nextPageLink[0]).json() dataPage = rawData["data"] nextPageLink = [rawData["paging"]["next"]] return dataPage, nextPageLink def match(self, searchTerm: str, datum: dict) -> bool: """ Summary: Logic to filter relevant posts. If a post has one or more of the json attributes used for checking it's validity, then we check to see if the value of those json attributes contains the search term. Args: searchTerm: a word or phrase to search over datum: the data to be checked for relevance Returns: True if datum[<jsonAttributes>] contains searchTerm, else False """ searchTerm = searchTerm.lower() validFilter = datum.keys() jsonAttributes = ["message", "name"] validAttributes = [ attribute for attribute in jsonAttributes if attribute in validFilter ] for attribute in validAttributes: if searchTerm in datum[attribute].lower(): return True return False def parse(self, datum: dict) -> dict: """ Summary: Used to parse api response and add any additional data that is relevant to the response. Args: datum: the datapoint to be parsed and updated Returns: datum: the parsed data dictionary with secondary information added """ datum = self.get_secondary_information(datum) return datum def get_secondary_information(self, datum: dict) -> dict: """ Summary: Gathers any secondary information that is relevant to the social data point and updates the data point with that information. Args: datum: the data point to be updated with secondary information Returns: datum: the data point updated with secondary information """ datum["secondary_information"] = {} datum["secondary_information"].update({ "comments": self.facebook.get_connections(datum["id"], "comments") }) return datum def search(self, searchTerm: str, sources: list, limit: int) -> list: return search(client=self, searchTerm=searchTerm, sources=sources, limit=limit)
keys = list(set(keys)) with codecs.open('fb_posts.csv', 'w', encoding='utf-8') as f: w = csv.DictWriter(f, delimiter=';', fieldnames=list(keys)) w.writeheader() w.writerows(csvdata) #Get all the comments gatherer = {} for i, post_id in enumerate(post_ids): try: gatherer[post] except KeyError: print(post_id, i, len(ids)) time.sleep(5) comments = [] post_comments = graph.get_connections(id=post_id, connection_name="comments") comments.extend(post_comments["data"]) while True: try: post_comments = requests.get( post_comments["paging"]["next"]).json() comments.extend(post_comments["data"]) except KeyError: break gatherer[post_id] = comments #save it pickle.dump(gatherer, open('fbcomments.p', 'wb')) #Save it as a .csv out = [] keys = []
def social_action_checkin(track_id,guest_track,landing_site,guest_device): guest_check = Guest.query.filter_by(id=guest_device.guest_id,site_id=landing_site.id).first() if not guest_check : current_app.logger.error('Wifiguest Log - Site ID:%s social_action_checkin called with device not associated to guest for track ID:%s'%(guest_track.site_id,guest_track.id)) return redirect(url_for('guest.multi_login',track_id=track_id),code=302) code = request.args.get('code') access_token = None fb_appid = landing_site.fb_appid or current_app.config['FB_APP_ID'] fb_app_secret = landing_site.fb_app_secret or current_app.config['FB_APP_SECRET'] redirect_uri = url_for('guest.social_action_checkin',track_id=track_id,_external=True) if code: #URL called after OAuth try: at = GraphAPI().get_access_token_from_code(code, redirect_uri, fb_appid, fb_app_secret) access_token = at['access_token'] graph = GraphAPI(access_token) permissions = graph.get_connections("me","permissions") except: current_app.logger.exception('Wifiguest Log - Site ID:%s guest_device MAC:%s social_action_checkin exception while getting access_token redirecting to social_action_checkin %s'%(landing_site.id,guest_track.device_mac,request.url)) return redirect(url_for('guest.social_action_checkin',track_id=track_id),code=302) else: profile_check = Facebookauth.query.filter_by(id=guest_check.fb_profile).first() if not profile_check : current_app.logger.error('Wifiguest Log - Site ID:%s social_action_checkin called with device not associated to profile_check for track ID:%s'%(guest_track.site_id,guest_track.id)) return redirect(url_for('guest.multi_login',track_id=track_id),code=302) try: graph = GraphAPI(profile_check.token) permissions = graph.get_connections("me","permissions") except: current_app.logger.exception('Wifiguest Log - Site ID:%s guest_device MAC:%s social_action_checkin exception while trying to get permissions %s'%(landing_site.id,guest_track.device_mac,request.url)) return redirect(url_for('guest.multi_login',track_id=track_id),code=302) #check if the user has granted publish_permissions publish_permission = False for perm in permissions['data']: if perm.get('permission') == 'publish_actions' and perm.get('status') == 'granted': publish_permission = True if not publish_permission: current_app.logger.debug('Wifiguest Log - Site ID:%s social_action_checkin called without guest giving publish_permission redo Oauth track ID:%s'%(guest_track.site_id,guest_track.id)) params={'client_id':fb_appid,'redirect_uri':redirect_uri,'scope':'publish_actions '} url = 'https://www.facebook.com/dialog/oauth?'+urllib.urlencode(params) return redirect(url,code=302) form1 = CheckinForm() if form1.validate_on_submit(): #try to do checkin try: fb_page = landing_site.fb_page or current_app.config['FB_PAGE_URL'] page_info = graph.get_object(fb_page,fields='description,name,location,picture') print page_info['id'] print graph.put_wall_post(message=form1.message.data,attachment={'place':page_info['id']}) except: current_app.logger.exception('Wifiguest Log - Site ID:%s social_action_checkin exception while checkinfor track ID:%s'%(guest_track.site_id,guest_track.id)) else: guest_check = Guest.query.filter_by(id=guest_device.guest_id,site_id=landing_site.id).first() if not guest_check: current_app.logger.error('Wifiguest Log - Site ID:%s social_action_checkin called with device not associated to guest for track ID:%s'%(guest_track.site_id,guest_track.id)) abort(404) guest_track.fb_posted = 1 guest_check.fb_posted = 1 db.session.commit() if landing_site.auth_fb_like == 1 and guest_check.fb_liked !=1: #redirect to like return redirect(url_for('guest.social_action_like',track_id=guest_track.track_id),code=302) else: guest_track.state = GUESTRACK_SOCIAL_AUTH guest_device.state = DEVICE_AUTH db.session.commit() return redirect(url_for('guest.authorize_guest',track_id=guest_track.track_id),code=302) # show page asking user to checkin current_app.logger.debug('Wifiguest Log - Site ID:%s social_action_checkin new guest show page to checkin for track ID:%s'%(guest_track.site_id,guest_track.id)) landing_page = Landingpage.query.filter_by(id=landing_site.default_landing).first() fb_page = landing_site.fb_page or current_app.config['FB_PAGE_URL'] page_info = graph.get_object(fb_page,fields='location,name') loc = page_info['location'] location = ' %s - %s %s %s'%(page_info.get('name',''),loc.get('street',''),loc.get('city',''),loc.get('country','')) return render_template("guest/%s/fb_checkin.html"%landing_site.template,landing_page = landing_page,font_list=font_list,app_id=fb_appid,track_id=track_id, fb_page=fb_page,location=location,form1=form1)
print "자동 재생할 페이스북 그룹 주소를 입력해주세요." print "ex) python player.py rarelylive [ID]" exit(1) if len(sys.argv) == 3: lastest_feed_id = sys.argv[2] else: lastest_feed_id = None facebookId = sys.argv[1] graph = GraphAPIBase(token) while True: if lastest_feed_id is None: feeds = graph.get_connections(facebookId, 'feed', fields='source,message', type='video', limit='1') else: feeds = graph.get_connections(facebookId, 'feed', fields='source,message', type='video', until=lastest_feed_id, limit='1') lastest_feed_id = None for feed in feeds['data']: lastest_feed_id = feed['id'].encode('utf-8') if 'message' not in feed: continue if 'source' not in feed:
def get_artist_likes_by_country(facebook_page_id, ts, is_crawl_all=False, window_size=7): graph = GraphAPI(access_token=FACEBOOK_ACCESS_TOKEN, ) artist_data = {} ww_likes = get_artist_ww_likes(graph, facebook_page_id) if ww_likes: artist_data[ts.strftime(TIME_SERIES_FORMAT)] = [ww_likes] time.sleep(1) kwargs = { 'metric': 'page_fans_country', 'until': int(ts.timestamp()), 'since': int((ts - datetime.timedelta(days=window_size)).timestamp()) } while True: try: res = graph.get_connections(str(facebook_page_id), 'insights', **kwargs) assert len(res['data']) > 0 except AssertionError: logging.info(msg='no more data') break except GraphAPIError as e: if e.code == 17: logging.log(msg='backing off', level=logging.INFO) time.sleep(300) continue else: raise e for dp in res['data'][0][ 'values']: # get each country's views for artist, for each ts in values if 'value' in dp and dp['value']: value = dp['value'] date = dp['end_time'].split('T')[0] try: artist_data[date].extend([{ 'location': iso, 'likes': value[iso] } for iso in value]) except KeyError: artist_data[date] = [{ 'location': iso, 'likes': value[iso] } for iso in value] time.sleep(1) if is_crawl_all: previous = res['paging']['previous'] if 'previous' in res[ 'paging'] else None if not previous: break since = int(previous.split('since=')[-1].split('&')[0]) until = int(previous.split('until=')[-1].split('&')[0]) if kwargs['since'] <= since: break kwargs['since'] = since kwargs['until'] = until else: break return artist_data
def fb_checkin(trackid, guesttrack, wifisite, guestdevice, fbconfig, loginauth): '''End point for guest to checkin ''' code = request.args.get('code') access_token = None fb_appid = fbconfig.fb_appid fb_app_secret = fbconfig.fb_app_secret fb_page = fbconfig.fb_page redirect_uri = url_for('unifispot.modules.facebook.fb_checkin', trackid=trackid, _external=True) if code: #URL called after OAuth try: at = GraphAPI().get_access_token_from_code(code, redirect_uri,\ fb_appid, fb_app_secret) access_token = at['access_token'] graph = GraphAPI(access_token) permissions = graph.get_connections("me", "permissions") except: guestlog_exception( 'fb_checkin exception while getting access_token \ redirecting to fb_checkin ', wifisite, guesttrack) #send back to page return redirect(redirect_uri, code=302) else: try: graph = GraphAPI(loginauth.fbtoken) permissions = graph.get_connections("me", "permissions") except: guestlog_exception( 'fb_checkin exception while getting permissions \ redirecting to fb_checkin ', wifisite, guesttrack) return redirect_guest(wifisite, guesttrack) #check if the user has granted publish_permissions publish_permission = False for perm in permissions['data']: if perm.get('permission') == 'publish_actions' and\ perm.get('status') == 'granted': publish_permission = True if not publish_permission: guestlog_warn( 'fb_checkin called without guest giving publish_permission redo Oauth\ fbauth', wifisite, guesttrack) params = { 'client_id': fb_appid, 'redirect_uri': redirect_uri, 'scope': 'publish_actions ' } url = 'https://www.facebook.com/dialog/oauth?' + urllib.urlencode( params) return redirect(url, code=302) checkinform = CheckinForm() if checkinform.validate_on_submit(): #try to do checkin try: page_info = graph.get_object( fb_page, fields='description,name,location,picture') graph.put_wall_post(message=checkinform.message.data, attachment={'place': page_info['id']}) except Exception as e: if 'Duplicate status message' in str(e): guestlog_warn( 'duplicate message exception while doing checkin, \ ask guest to enter some message', wifisite, guesttrack) flash(_('Please enter some message'), 'danger') else: guestlog_exception('exception while doing checkin', wifisite, guesttrack) else: #mark fbauth with checkedin guesttrack.updatestat('fbcheckedin', 1) guest = Guest.query.get(guestdevice.guestid) if not guest: guestlog_warn("no guest associated with guestdevice", wifisite, guesttrack) return redirect_guest(wifisite, guesttrack) guest.fbcheckedin = 1 guest.save() #check if guest needs to be redirected to asklike #page if fbconfig.auth_fb_like and guest.fbliked != 1: return redirect( url_for('unifispot.modules.facebook.fb_like', trackid=trackid)) else: #redirect guest to auth page loginauth.populate_auth_details(fbconfig) loginauth.reset() loginauth.reset_lastlogin() loginauth.state = LOGINAUTH_FIRST loginauth.save() #neither configured, authorize guest guesttrack.state = GUESTTRACK_POSTLOGIN guesttrack.loginauthid = loginauth.id guesttrack.updatestat('auth_facebook', 1) return redirect_guest(wifisite, guesttrack) guestlog_debug('show ask for checkin page', wifisite, guesttrack) landingpage = Landingpage.query.filter_by(siteid=wifisite.id).first() page_info = graph.get_object(fb_page, fields='location,name') loc = page_info['location'] location = ' %s - %s %s %s' % (page_info.get( 'name', ''), loc.get('street', ''), loc.get( 'city', ''), loc.get('country', '')) return render_template("guest/%s/fb_checkin.html" % wifisite.template, landingpage=landingpage, app_id=fb_appid, trackid=trackid, fb_page=fb_page, location=location, checkinform=checkinform)