Hello,
We use the tokens of this account @TaagColombia1,
And we want to bring the metrics of the 18ce55fo83o account, that is, impressions, clicks, etc. for organic tweets.
We use this request:
do_request(f'https://ads-api.twitter.com/11/stats/jobs/accounts/{user_id_encode}',{'entity':entity,'entity_ids':','.join(tweets) ,'start_time':start_date,'end_time':end_date,'granularity':granularity,'placement':placement,'metric_groups':metric_groups},'post')
With this we have a job_id and we do the following query:
`get_request(f'https://ads-api.twitter.com/11/stats/jobs/accounts/{user_id_encode}',{'job_ids':id_job},str(start_date),str(end_date))`
where:
def get_request(self, end_point, params,start_date,end_date):
result = []
boolean = True
while boolean:
response = self.oauth.get(
end_point, params=params
)
if response.status_code != 200:
raiseException(
"Request returned an error: {} {}".format(response.status_code, response.text)
)
boolean_2 = True
for result in response.json().get('data'):
if result.get('status') != 'SUCCESS':
print('Waiting 30 seconds')
time.sleep(30)
boolean_2 = False
break
if boolean_2:
boolean = False
json_response = response.json()
urls = [result.get('url') for result in response.json().get('data')]
to_return = [ pd.DataFrame(columns = list(schema.schemas.get('tweet_metrics').keys()))]
for url in urls:
name_arch = url.split('/')[-1]
wget.download(url,archname)
x = gzip.open(arch_name,'rb').read()
x = json.loads(x)
end_date = (datetime.strptime(end_date,'%Y-%m-%d')-timedelta(days=1)).strftime('%Y-%m-%d')
for i in range(len(x['data'])):
df = pd.DataFrame(x['data'][i]['id_data'][0]['metrics'],index=pd.date_range(start=start_date, end=end_date, freq='D'), dtype='str')
df['day'] = df.index
df['tweet_id'] = x['data'][i]['id']
## Look at the same case of report_to_df when it is a list
to_return.append(df)
return pd.concat(to_return, ignore_index=True)
By making this last call we hope that it will bring us the requested metrics:
metric_groups = ‘ENGAGEMENT,VIDEO’
But with the 18ce55fo83o account it does not bring us the information, although with the 18ce55icp5a, 18ce54tida1 and 18ce55f4quc accounts it does bring us information using the same tokens for all accounts.