R/yadirStartCampaigns.R

Defines functions yadirStartCampaigns

Documented in yadirStartCampaigns

yadirStartCampaigns <-  function(Login = NULL,
                                 Ids   = NULL,
                                 Token = NULL,
                                 AgencyAccount = NULL,
                                 TokenPath     = getwd()){

  #???????????
  Token <- tech_auth(login = Login, token = Token, AgencyAccount = AgencyAccount, TokenPath = TokenPath)

  if(length(Ids) > 1000){
    stop(paste0("? ???????? Ids ???????? ?????? ",length(Ids), "????????, ??????????? ?????????? ?????????? ???????? ? ????? ??????? 1000."))
  }

  if(is.null(Ids)){
    stop("? ???????? Ids ?????????? ???????? ?????? ?????????? Id ???????? ?? ??????? ?????????? ??????????? ????? ??????????. ?? ?? ???????? ???????? Id.")
  }

  #??????? ??????
  CounErr <- 0

  #Error vector
  errors_id <-  vector()

  #????????? ????? ?????? ??????
  start_time  <- Sys.time()

  #?????? ????????? offset
  lim <- 0

  #????????? ? ?????? ????????? ??????
  packageStartupMessage("Processing", appendLF = T)

  IdsPast <- paste0(Ids, collapse = ",")
  #????????? ???? POST ???????
  queryBody <- paste0("{
                      \"method\": \"resume\",
                      \"params\": {
                      \"SelectionCriteria\": {
                      \"Ids\": [",IdsPast,"]}
                        }
                      }")

  #???????? ???????
  answer <- POST("https://api.direct.yandex.com/json/v5/campaigns", body = queryBody, add_headers(Authorization = paste0("Bearer ",Token), 'Accept-Language' = "ru","Client-Login" = Login))
  #?????? ?????
  ans_pars <- content(answer)
  #???????? ?????? ?? ??????? ??????
  if(!is.null(ans_pars$error)){
    stop(paste0("sahsrhahrs ", ans_pars$error$error_string,". ahsshrhsr ",ans_pars$error$error_detail, ". ID arharhssahr ",ans_pars$error$request_id))
  }

  #???????? ?????????????? ????????
  for(error_search in 1:length(ans_pars$result$ResumeResults)){
    if(!is.null(ans_pars$result$ResumeResults[[error_search]]$Errors)){
      CounErr <- CounErr + 1
      errors_id <- c(errors_id, Ids[error_search])
      packageStartupMessage(paste0("    CampId: ",Ids[error_search]," - ", ans_pars$result$ResumeResults[[error_search]]$Errors[[1]]$Details))
    }
  }

  #?????????? ????????? ??? ?????????? ????????????? ????????
  out_message <- ""

  TotalCampStoped <- length(Ids) - CounErr

  if(TotalCampStoped %in% c(2,3,4) & !(TotalCampStoped %% 100 %in% c(12,13,14))){
    out_message <- "rsahsahrasrh"
  } else if(TotalCampStoped %% 10 == 1 & TotalCampStoped %% 100 != 11){
    out_message <- "saehsrhjrahs"
  } else {
    out_message <- "asrhsrah asrhrsh"
  }

  #??????? ??????????
  packageStartupMessage(paste0(TotalCampStoped, " ", out_message))
  packageStartupMessage(paste0(" srahsrharsh srha ", as.integer(round(difftime(Sys.time(), start_time , units ="secs"),0)), " asrhsrhash"))
  return(errors_id)}
grkhr/ryandexdirect documentation built on Dec. 10, 2019, 2:28 p.m.