Get IDs of all Facebook groups - python

I need to get all user's groups ID or links to those groups which he belong
The code is executing but nothing is writing down to a file
Has anyone a clue or diffret way to get groups id's
I need it to make an alghortim which will be adding specific ads on every group but i cant get the id's
import requests
# Replace ACCESS_TOKEN with a valid access token
ACCESS_TOKEN = "TOKEN_ACCCES"
# Set the endpoint URL
url = "https://graph.facebook.com/me/groups"
# Set the HTTP headers
headers = {
"Authorization": f"Bearer {ACCESS_TOKEN}",
"Content-Type": "application/json",
}
# Set the params for the request
params = {
"limit": 100, # The maximum number of groups to retrieve per request
}
# Initialize an empty list to store the group IDs
group_ids = []
# Flag to determine whether there are more groups to retrieve
more_groups = True
# Keep making requests until all groups have been retrieved
while more_groups:
# Send the request
response = requests.get(url, headers=headers, params=params)
# Check the response status code
if response.status_code != 200:
print("Failed to retrieve groups")
print(response.text)
break
# Extract the data from the response
data = response.json()
# Add the group IDs to the list
group_ids.extend([group["id"] for group in data["data"]])
# Check if there are more groups to retrieve
if "paging" in data and "next" in data["paging"]:
# Update the URL and params for the next request
url = data["paging"]["next"]
params = {}
else:
# No more groups to retrieve
more_groups = False
# Write the group IDs to a file
with open("group_ids.txt", "w") as f:
f.write(",".join(group_ids))
print("Done!")

Related

Getting historical data from a wheniwork API

I'm retrieving shifts data from wheniwork API
My Python code:
import requests
wheniwork_url = 'https://api.login.wheniwork.com/login'
response = requests.post(
wheniwork_url,
headers = {
"W-Token": '98239akljfqdb3wu97982' #secret_token
"Content-Type": "application/json"},
json={"email": my_wheniwork_email, "password": my_wheniwork_password},
)
final_response = response.json()
token = final_response["token"]
headers = {"W-Token": token, "Content-Type": "application/json"}
users_url = "https://api.wheniwork.com/2/shifts"
res = requests.get(users_url, headers=headers)
output = res.json()
print(output)
I followed wheniwork shifts api documentation to get the shifts data but this is not giving me the historical data.
How/Where can I pass the date parameters in order to get the historical data.
There is start and end date but I'm not sure how to insert start and end date parameters in the API call

Api call using python and token_auth

"""
#Collects basic metrics from Matomo installation and returns a pandas dataframe
"""
token = os.getenv("token")
# Build url string
base_url = 'https://matomo.___.com/index.php?module=API'
site_num = '&idSite=1'
return_format = '&format=json'
period = '&period=day'
date_range = '&date=last30'
method = '&method=VisitsSummary.get'
token_string = "&token_auth=" + token
my_url = base_url + site_num + return_format + period + date_range + method + token_string
# send request for report
r = requests.get(my_url)
# parse and tidy collected data
data = pd.DataFrame(r.json()).T
data = data.reset_index()
data.columns = [
"date",
"uniq_visitors",
"users",
"visits",
"actions",
"visits_converted",
"bounces",
"sum_visit_length",
"max_actions",
"bounce_rate",
"actions_per_visit",
"avg_time_on_site",
]
return data
I am trying to get data from the matomo API using an auth_token and parameters by using above code but i am not able to access it and my url is not taking token code any one has idea how i can solve this
Given that you are using the request library, passing parameters and headers can be done using the following params in your get call:
r = requests.get(my_url, params=payload)
In the same way, an auth token is usually passed within headers:
r = requests.get(my_url, params=payload, headers=headers)
Using this format you can simply create a headers object which contains your token_auth and directly pass your parameters in a payload object:
headers = {'token_auth': token}
payload = {'module':'API', 'idSite':1, 'format':'json', 'period':'day', 'date':'last30', 'method':'VisitsSummary.get'}
Since you are now passing your parameters in you get request, there is no need to add them to the end of your url. Thus, your url should stay as https://matomo.___.com/index.php. These can then be used within your params and headers respectively. Please note that this assumes that the matomo API places the token_auth in its headers such as most APIs do. If this is not the case you could pass it directly within the params payload.
Here is a global overview:
token = os.getenv("token")
# Get url, headers and params
my_url = 'https://matomo.___.com/index.php'
payload = {'module':'API', 'idSite':1, 'format':'json', 'period':'day', 'date':'last30', 'method':'VisitsSummary.get'}
headers = {'token_auth': token}
# send request for report
r = requests.get(my_url, params=payload, headers=headers)
Note this answers your question specifically regarding the API call and not the processing after.

Rest API iteration until number of records reached

Trying to get the api iteration until it pulls the whole records. Any idea/hits would be really appreciated. it returns 5000 records default max per api call and there are almost 30000 rows in account object.
As per doc- more than 5000 records that can be fetched, pass another API call with Offset as 5001 so that remaining records (maximum 5000 records again) are fetched
import requests
import json
url = 'https://xyzabc.com/account'
headers = {'content-type': 'application/json','Accesskey': '1234'}
body = {"select": [
"accountid",
"accountname",
"location"],
"offset" :0}
response = requests.post(url, data=json.dumps(body), headers=headers)
account = response.json()
As your offset is where you are, you can do it in a loop like this:
url = 'https://xyzabc.com/account'
headers = {'content-type': 'application/json','Accesskey': '1234'}
# Please check if you have a better way to get total number from your API specs,
# then specify it - that may need a separate API call.
total_records = 1000000000
# Get the results of your API calls into the list
accounts = []
# Go from 0 to total_records every 5000 records
try:
for i in range(0, total_records, 5000):
body = {"select": ["accountid",
"accountname",
"location"],
"offset" :i}
response = requests.post(url, data=json.dumps(body), headers=headers)
accounts.append(response.json())
except Exception as e:
print(f"Connection error - {e}") # Handle it your way
for account in accounts:
# Your logic for every account fetched.

Using "Next Page" in while loop

I am retrieving data from an API endpoint which only allows me to retrieve a maximum of 100 data points at a time. There is a "next page" field within the response which I could use to retrieve the next 100 data points and so on (there are about 70,000 in total) by plugging the next page url back into the GET request. How can I utilize a for loop or while loop to retrieve all the data available in the endpoint by automatically plugging the "next page" URL back into the get request?
Here is the code im using. The problem is when I execute the While loop I get the same response everytime because it is running on the first response instance. I can't think of the solution of how to adjust this.
response = requests.get(url + '/api/named_users?limit=100', headers=headers)
users = []
resp_json = response.json()
users.append(resp_json)
while resp_json.get('next_page') != '':
response = s.get(resp_json.get('next_page'), headers = headers)
resp_json = response.json()
users.append(resp_json)
To summarize: I want to take the "next page" URL in every response to get the next 100 data points and append it to a list each time until I have all the data fetched.
You can do it, with a recursive function.
For example something like this :
response = requests.get(url + '/api/named_users?limit=100', headers=headers)
users = []
resp_json = response.json()
users.append(resp_json)
users = next_page(resp_json.get('next_page'), users)
def next_page(url, users):
if url != '':
response = s.get(url, headers=headers)
resp_json = response.json()
users.append(resp_json)
if resp_json.get('next_page') != '':
return next_page(resp_json.get('next_page'), users)
return users
But in general, APIs return a total number of items and a number of items per request. So you can easily paginate and loop through all items.
Here is some pseudo-code :
for i in range(items_returned__per_request, total_number_of_items/items_returned__per_request):
response = s.get(resp_json.get('next_page'), headers=headers)
resp_json = response.json()
users.append(resp_json)

How can I make this API work with a payload?

I am using this API to list users. One of the parameters I could specify is a team id which is placed in an array. When I try to specify a team id it doesn't work when I put it in the payload, but it works when I change the url to include the team id.
This is the API reference: https://api-reference.pagerduty.com/#!/Users/get_users
Here is what I am basing my code off of: https://github.com/PagerDuty/API_Python_Examples/blob/master/REST_API_v2/Users/list_users.py
This is my code when I try to specify team id in the payload. It doesn't work like this for some reason, but it works when I change the url to url = 'https://api.pagerduty.com/users?team_ids%5B%5D=TEAMID&team_ids%5B%5D=' where in TEAMID I have an actual team id.
with open('config/config.json') as f:
config = json.load(f)
API_KEY = config['API_KEY']
TEAM_IDS = ['TEAMID']
def list_users():
url = 'https://api.pagerduty.com/users'
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token={token}'.format(token=API_KEY)
}
payload = {
'team_ids[]': TEAM_IDS
}
r = requests.get(url, headers=headers)
result = []
if r.status_code == 200:
# loops for each user and retrieves their email
result = [user['email'] for user in r.json()['users']]
return result
else:
return None
I want to get this work by listing team id's in the array and sending it in the payload so that I can list more than one team id and not clutter them all in the url.
Looks like you just need something like this
payload = {
'team_ids[]': TEAM_IDS
}
r = requests.get(url, headers=headers, params=payload)

Categories

Resources