Skip to content

Commit

Permalink
Fix currency dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
selesnow authored Dec 11, 2018
1 parent ce6ee8b commit d5dc110
Showing 1 changed file with 53 additions and 47 deletions.
100 changes: 53 additions & 47 deletions R/yadirGetDictionary.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ yadirGetDictionary <- function(DictionaryName = "GeoRegions",
Token = NULL,
AgencyAccount = NULL,
TokenPath = getwd()){
#Ïðîâåðêà íàëè÷èÿ ëîãèíà è òîêåíà

# start

#Àâòîðèçàöèÿ
# auth
Token <- tech_auth(login = Login, token = Token, AgencyAccount = AgencyAccount, TokenPath = TokenPath)

#Ïðîâåðêà âåðíî ëè óêàçàíî íàçâàíèå ñïðàâî÷íèêà
# Check dict name
if(!DictionaryName %in% c("Currencies",
"MetroStations",
"GeoRegions",
Expand All @@ -23,36 +24,36 @@ yadirGetDictionary <- function(DictionaryName = "GeoRegions",
stop("Error in DictionaryName, select one of Currencies, MetroStations, GeoRegions, TimeZones, Constants, AdCategories, OperationSystemVersions, ProductivityAssertions, SupplySidePlatforms, Interests")
}

#check stringAsFactor
factor_change <- FALSE
#check stringAsFactor
factor_change <- FALSE

#change string is factor if TRUE
if(getOption("stringsAsFactors")){
options(stringsAsFactors = F)
factor_change <- TRUE
}

queryBody <- paste0("{
\"method\": \"get\",
\"params\": {
\"DictionaryNames\": [ \"",DictionaryName,"\" ]
}
}")
#change string is factor if TRUE
if(getOption("stringsAsFactors")){
options(stringsAsFactors = F)
factor_change <- TRUE
}
queryBody <- paste0("{
\"method\": \"get\",
\"params\": {
\"DictionaryNames\": [ \"",DictionaryName,"\" ]
}
}")

#Îòïðàâêà çàïðîñà íà ñåðâåð
# send query
answer <- POST("https://api.direct.yandex.com/json/v5/dictionaries", body = queryBody, add_headers(Authorization = paste0("Bearer ",Token), 'Accept-Language' = Language, "Client-Login" = Login[1]))
#Ïðîâåðêà ðåçóëüòàòà íà îøèáêè
# check status
stop_for_status(answer)

# parse
dataRaw <- content(answer, "parsed", "application/json")

if(length(dataRaw$error) > 0){
stop(paste0(dataRaw$error$error_string, " - ", dataRaw$error$error_detail))
}

#Ïðåîáðàçóåì îòâåò â data frame
# convert to data.frame

#Ïàðñèíã ñïðàâî÷íèêà ðåãèîíîâ
# georegions
if(DictionaryName == "GeoRegions"){
dictionary_df <- data.frame()

Expand All @@ -65,26 +66,30 @@ if(getOption("stringsAsFactors")){

}}

#Ïàðñèíã ñïðàâî÷íèêà âàëþò
# cur
if(DictionaryName == "Currencies"){
dictionary_df <- data.frame()
for(dr in 1:length(dataRaw$result[[1]])){
dictionary_df_temp <- data.frame(Cur = dataRaw$result[[1]][[dr]]$Currency, as.data.frame(do.call(rbind.data.frame, dataRaw$result[[1]][[dr]]$Properties), row.names = NULL, stringsAsFactors = F))
dictionary_df <- rbind(dictionary_df, dictionary_df_temp)
}
dictionary_df_cur <- data.frame()
#Ïðåîáðàçóåì ñïðàâî÷íèê âàëþò
for(curlist in unique(dictionary_df$Cur)){
dictionary_df_temp <- data.frame(Cur = curlist,
FullName = dictionary_df[dictionary_df$Cur == curlist & dictionary_df$Name == "FullName",3],
Rate = dictionary_df[dictionary_df$Cur == curlist & dictionary_df$Name == "Rate",3],
RateWithVAT = dictionary_df[dictionary_df$Cur == curlist & dictionary_df$Name == "RateWithVAT",3])
dictionary_df_cur <- rbind(dictionary_df_cur,dictionary_df_temp)
for(dr in dataRaw$result$Currencies){
dictionary_df_temp <- data.frame(Currency = dr$Currency,
BidIncrement = as.numeric(dr$Properties[[1]]$Value) / 1000000,
FullName = dr$Properties[[2]]$Value,
MaximumBid = as.numeric(dr$Properties[[3]]$Value) / 1000000,
MinimumAverageCPA = as.numeric(dr$Properties[[4]]$Value) / 1000000,
MinimumAverageCPC = as.numeric(dr$Properties[[5]]$Value) / 1000000,
MinimumCPM = as.numeric(dr$Properties[[6]]$Value) / 1000000,
MaximumCPM = as.numeric(dr$Properties[[7]]$Value) / 1000000,
MinimumBid = as.numeric(dr$Properties[[8]]$Value) / 1000000,
MinimumDailyBudget = as.numeric(dr$Properties[[9]]$Value) / 1000000,
MinimumPayment = as.numeric(dr$Properties[[10]]$Value) / 1000000,
MinimumTransferAmount = as.numeric(dr$Properties[[11]]$Value) / 1000000,
MinimumWeeklySpendLimit = as.numeric(dr$Properties[[12]]$Value) / 1000000)

dictionary_df <- rbind(dictionary_df, dictionary_df_temp)
}
dictionary_df <- dictionary_df_cur

}

#Ïàðñèíã ñïðàâî÷íèêà Interests
# Interests
if(DictionaryName == "Interests"){
dictionary_df <- data.frame()
for(dr in 1:length(dataRaw$result[[1]])){
Expand All @@ -96,24 +101,25 @@ if(getOption("stringsAsFactors")){
}
}

#Ïàðñèíã îñòàëüíûõ ñïðàâî÷íèêîâ ñî ñòàíäàðòíîé ñòðóêòóðîé
# pars dict with standart structure
if(! DictionaryName %in% c("Currencies","GeoRegions","Interests")){
dictionary_df <- do.call(rbind.data.frame, dataRaw$result[[1]])
}

#back string as factor value
# back string as factor value
if(factor_change){
options(stringsAsFactors = T)
}
#Âûâîäèì èíôîðìàöèþ î ðàáîòå çàïðîñà è î êîëè÷åñòâå áàëëîâ
packageStartupMessage("Ñïðàâî÷íèê óñïåøíî çàãðóæåí!", appendLF = T)
packageStartupMessage(paste0("Áàëëû ñïèñàíû ñ : " ,answer$headers$`units-used-login`), appendLF = T)
packageStartupMessage(paste0("Ê-âî áàëëîâ èçðàñõîäîâàíûõ ïðè âûïîëíåíèè çàïðîñà: " ,strsplit(answer$headers$units, "/")[[1]][1]), appendLF = T)
packageStartupMessage(paste0("Äîñòóïíûé îñòàòîê ñóòî÷íîãî ëèìèòà áàëëîâ: " ,strsplit(answer$headers$units, "/")[[1]][2]), appendLF = T)
packageStartupMessage(paste0("Ñóòî÷íûé ëèìèò áàëëîâ: " ,strsplit(answer$headers$units, "/")[[1]][3]), appendLF = T)
packageStartupMessage(paste0("Óíèêàëüíûé èäåíòèôèêàòîð çàïðîñà êîòîðûé íåîáõîäèìî óêàçûâàòü ïðè îáðàùåíèè â ñëóæáó ïîääåðæêè: ",answer$headers$requestid), appendLF = T)

#Âîçâðàùàåì ðåçóëüòàò â âèäå Data Frame
# technical info message
packageStartupMessage("Directory successfully loaded!", appendLF = T)
packageStartupMessage(paste0("Points are deducted from: " ,answer$headers$`units-used-login`), appendLF = T)
packageStartupMessage(paste0("Number of API points spent when executing the request: " ,strsplit(answer$headers$units, "/")[[1]][1]), appendLF = T)
packageStartupMessage(paste0("Available balance of daily limit API points: " ,strsplit(answer$headers$units, "/")[[1]][2]), appendLF = T)
packageStartupMessage(paste0("Daily limit of API points:" ,strsplit(answer$headers$units, "/")[[1]][3]), appendLF = T)
packageStartupMessage(paste0("Reqiest ID that must be specified when contacting support:: ",answer$headers$requestid), appendLF = T)

# result
return(dictionary_df)
}

0 comments on commit d5dc110

Please sign in to comment.