def get_all_leads(self, group_id): result = [] lead_forms = self.get_leads_forms(group_id) forms = [form['form_id'] for form in lead_forms] for form_id in forms: leads = self.get_leads_form_id(group_id, form_id) time.sleep(1) for lead in leads: middle_list = {} for lead_key, lead_value in lead.items(): if lead_key == 'answers': for element in lead_value: middle_list[ element['key']] = element['answer']['value'] elif lead_key == 'date': middle_list[lead_key] = datetime.strftime( datetime.fromtimestamp(int(lead_value)), "%Y-%m-%d") else: middle_list[lead_key] = lead_value result.append(middle_list) keys = [ "lead_id", "user_id", "date", "first_name", "phone_number", "patronymic_name", "last_name", "email" ] result = pop_keys(keys, result) return result
def get_leads_forms(self, group_id): lead_forms = self.__request("leadForms.list", request_type='get', group_id=group_id) keys = [ "form_id", "group_id", "name", "title", "description", "site_link_url", "url" ] lead_forms = pop_keys(keys, lead_forms) return lead_forms
def get_campaigns(self): campaigns = self.__request('ads.getCampaigns', request_type='get', account_id=self.account_id, include_deleted=1, client_id=self.client_id) keys = ["id", "type", "name"] campaigns = pop_keys(keys, campaigns) campaign_ids = [campaign_id['id'] for campaign_id in campaigns] campaigns_df = pd.DataFrame(campaigns).fillna(0) self.bq.insert_difference( campaigns_df, self.fields, self.data_set_id, f"{self.client_name}_VKontakte_{self.client_id}_CAMPAIGNS", 'id', 'id', "%Y-%m-%d") return campaign_ids, campaigns_df
def get_ads(self, campaign_ids): campaign_ids = my_slice(campaign_ids, 100) ads_list = [] for campaign_ids_list in campaign_ids: campaign_ids_string = ",".join([str(x) for x in campaign_ids_list]) ads = self.__request('ads.getAds', request_type='get', account_id=self.account_id, campaign_ids=f"[{campaign_ids_string}]", client_id=self.client_id) ads_list += ads time.sleep(2) keys = [ "id", "campaign_id", "goal_type", "cost_type", "category1_id", "category2_id", "age_restriction", "name", "ad_format", "ad_platform" ] ads = pop_keys(keys, ads_list) return ads
def get_day_stats(self, ids_type, list_of_ids, limit=2000): day_stat_list = [] ids_list = my_slice(list_of_ids, limit) for ids_stat_list in ids_list: ids_stat_string = ",".join([str(x) for x in ids_stat_list]) day_stats = self.__request('ads.getStatistics', request_type='get', account_id=self.account_id, ids_type=ids_type, ids=ids_stat_string, period="day", date_from=self.date_from, date_to=self.date_to) for DayStat in day_stats: for stat in DayStat['stats']: stat[DayStat['type'] + "_id"] = DayStat['id'] day_stat_list.append(stat) time.sleep(2) keys = [ "day", "spent", "impressions", "clicks", "reach", "join_rate", "campaign_id", 'lead_form_sends', 'goals', "ad_id" ] day_stat_list = pop_keys(keys, day_stat_list) return day_stat_list