Retrieve extra fields with Graph API using PHP - facebook-graph-api

I'm trying to create some social dashboard and therefore I want to retrieve my posts from my page. When I use this one to fetch my posts, it doesn't return me all the information I need (e.g 'picture', 'full_picture', 'attachments')
$user_posts = $facebook->api('/me/posts', 'GET');
print_r($user_posts);
But when I try next one, it still doesn't return me my required information:
$user_posts = $facebook->api('/me/posts?{created_time,id,message,full_picture,picture,attachments{url,subattachments},likes{name},comments{from,message,comment_count,user_likes,likes{name}}}', 'GET');
print_r($user_posts);
Anyone ideas??

I know that this has been asked a long time ago, but maybe useful for someone:
After - me/posts? - you need to make sure to put fields= and then a list of fields required.
So this would be:
$user_posts = $facebook->api('/me/posts?fields={created_time,id,message,full_picture,picture,attachments{url,subattachments},likes{name},comments{from,message,comment_count,user_likes,likes{name}}}', 'GET');
print_r($user_posts);

Related

How to make filtering non model data in flask-admin

I have to make dashboard like view in flask-admin that will use data retrieved from external API. I have already written a functions that get date ranges and return data from that range. I should use BaseView probably but I don't know how to actually write it to make filters work. This is example function that i have to use: charts = generate_data_for_dashboard('164', '6423FACA-FC71-489D-BF32-3A671AB747E3', '2018-03-01', '2018-09-01'). Those params should be chosen from 3 different dropdowns. So far I know only how to render views with pre coded data like this :
class DashboardView(BaseView):
kwargs = {}
#expose('/', methods=('GET',))
def statistics_charts(self):
user = current_user
company = g.company
offices = Office.query.filter_by(company_id=company.id)
self.kwargs['user'] = user
self.kwargs['company'] = company
charts = generate_data_for_dashboard('164', '6423FACA-FC71-489D-BF32-3A671AB747E3', '2018-03-01', '2018-09-01')
self.kwargs['chart1'] = charts[0]
self.kwargs['chart2'] = charts[1]
return self.render('stats/dashboard.html', **self.kwargs)
But I need some kind of form to filter it. In addition date filter dropdown should have dynamic options : current_week, last_week, current_month, last_month, last_year. Don't know where to start.
You should use WTForms to build a form. You then have to decide if you want the data to be fetched on Submit or without a reload of the page. In the former case, you can just return the fetched information on the response page in your statistics_charts view. But if you want the data to update without a reload, you'll need to use JavaScript to track the form field changes, send the AJAX request to the API, and then interpret the resulting JSON and update your dashboard graphs and tables as needed.
I have not used it, but this tutorial says you can use Dash for substantial parts of this task, while mostly writing in Python. So that could be something to check out. There is also flask_jsondash which might work for you.

How to get Zomato restaurant ID using restaurant link?

I want to get details of a restaurant in Zomato. I have it's link as the input (https://www.zomato.com/mumbai/fantasy-the-cake-shop-kalyan?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1). By browsing the documentation of Zomato APIs, I didn't found a way to get it.
I tried searching for the restaurant using search API but it returns many results.
Any help will be appreciated
It's a two-step process:
Find out restaurant's city id, in your case, Mumbai's city id through the /cities API. It's a simple query search.
Use the city id from the above API call in the /search API, like, https://developers.zomato.com/api/v2.1/search?entity_type=city&entity_id=3&q=fantasy%20the%20cake%20shop%20kalyan
This would give all the basic information about a restaurant.
View the page's source and search for window.RES_ID
I had the same issue as you described. This Zomato's API approach is at least odd. It's almost immposible to GET any information about restaurant if you don't know res_id in advance and that's not possible to parse since Zomato will deny access.
This worked for me:
Obtain user-key from Zomato API Credentials (https://developers.zomato.com/api)
Search restaurant url via API (https://developers.zomato.com/api/v2.1/search?entity_id=84&entity_type=city&q=RESTAURANT_URL&category=7%2C9). The more specific you will be, the better results you'll get (This url is specified by city to Prague (ID = 84) and categories Daily menus (ID = 7) and Lunch (ID = 9). If there is possibility do specify city, category or cuisine, it helps, but should't be necessary. Don't forget to define GET method in headers.
Loop or filter through json results and search for the wanted url. You might need to use method valueOf() to search for the same url. Be careful, you might need to add "?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1" at the end of your wanted url so it has the same format. Check that through Zomato API Documentation page.
for (i in body.restaurants) {
var url_wanted = restaurant_url + '?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1'
var url_in_json = body.restaurants[i].restaurant.url;
if (url_wanted.valueOf() == url_in_json.valueOf()) {
var restaurant_id = body.restaurants[i].restaurant.id;
}
console.log('Voala! We have res_id:' + restaurant_id);
}
There you have it. It could be easier though.
Hope it helps!
once you have the url of the rseraunt's page you can simply look for a javascript object attribute named "window.RES_ID" and further use it in the api call.

Get profile picture from Facebook using social-auth

I am implementing social login in my application and I am already getting normal fields as First Name, Last Name on Facebook and LinkedIn... Now I want to get the profile picture. I did it for LinkedIn and it is working perfectly, i can see the URL on the extra fields from the database.
I am trying to do the same with Facebook, as I saw here https://groups.google.com/forum/?fromgroups#!topic/django-social-auth/Mee8H8HhLQk
defining
FACEBOOK_PROFILE_EXTRA_PARAMS = {'fields': 'picture'}
FACEBOOK_EXTRA_DATA = [('profile', 'profile')]
But when i did this, somethings got messed up. I am not able anymore to get the first name and last name in my user table as I was doing before these two lines of code. Does anyone know why this problem is happening?
The picture is under http://graph.facebook.com/<user_id>/picture. The user_id is given by the FacebookBackend.

query in query django

how to make such query in django.
I have Site model where I can find relation to the topic model. In topic model I can fing relation to the post model. I want to extract post from a site having information only about site, not a topic. What is more posts have to starts with query.
query = request.GET.get('query','')
iweb_obj = IWeb.objects.get(id=iweb_id)
topics = Topic.objects.filter(iweb=iweb_obj)
iweb_posts = []
for t in topics:
posts = Post.objects.filter(topic=t)
for p in posts:
iweb_posts.append(p)
iweb_posts = iweb_.filter(content__istartswith=query)
I have an error that iweb_posts isnt query set and I cant make such action. It is quite obvious, however I do not have idea how to make it works ? I've heard that I can use filter(**kwargs) but I do not know how to use it ?
Your logic looks a little funky since you're overwriting posts each time in the topic loop. You can accomplish what you need without loops and lists using only query set filters (I've added an __in filter, for example):
query = request.GET.get('query','')
iweb_obj = IWeb.objects.get(id=iweb_id)
topics = Topic.objects.filter(iweb=iweb_obj)
iweb_posts = Post.objects.filter(topic__in=topics).filter(content__istartswith=query)

Filter out multiple emails from a Queryset

I'm trying to filter out multiple emails from a queryset in order to stop our employee emails from affecting any exports. I have the following in settings.py:
EMAIL_FILTERS = ['email1', 'email2']
and I'm trying to use this to filter out any entries in the DB with our emails in the following query:
EMAIL_FILTERS = getattr(settings, 'EMAIL_FILTERS')
campaigns = CampaignsSignup.objects.filter(created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)).order_by('-created_date')
However after trying Q and exclude I can't seem to get it to work. I should also note as much as I would like to be able to use endswith some of the employees are using their personal email address so at the moment it wouldn't be viable.
Thanks.
I see you've found a solution, but the normal way to do this is with exclude:
campaigns = CampaignsSignup.objects.filter(
created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)
).order_by('-created_date').exclude(email__in=SAVCHAMP_EMAIL_FILTERS)
After throwing everything at it I stumbled upon a way of doing it. Using __in with Q correctly filters out the emails that have been included in the email_filter list in settings.
campaigns = CampaignsSignup.objects.filter(~Q(email__in=SAVCHAMP_EMAIL_FILTERS), created_date__gte=self._get_start_date(dte),
created_date__lte=self._get_end_date(dte)).order_by('-created_date')