Advertisement
sxiii

R Script to check your voting power. Fixed to work on Linux

Feb 24th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.25 KB | None | 0 0
  1. library(shiny)
  2. library(RODBC)
  3.  
  4. # Copied from https://github.com/szilard/shinyvalidinp/blob/master/R/shinyvalidinp.R
  5. # Fixed by Security XIII to work on Linux
  6. validinp_character <- function(x, pattern="^[[:alnum:]. _-]*$", many=FALSE) {
  7.   if(many && is.null(x)) return(character(0))  ## hack for checkboxGroupInput
  8.   if(!( is.character(x) && (many || length(x)==1) &&
  9.         all(!is.na(x)) && all(grepl(pattern,x)) )) {
  10.     stop("Illegal characters in the user name")
  11.   }
  12.   x
  13. }
  14.  
  15. # Define UI for application that draws a histogram
  16. ui <- fluidPage(
  17.  
  18.   # Application title
  19.   titlePanel("Golos - check current post bandwidth"),
  20.  
  21.   # Sidebar with a slider input for number of bins
  22.   verticalLayout(
  23.     textInput("name",
  24.               "User name:",
  25.               width = '200px'),
  26.     actionButton("goButton", "Go!"),
  27.     # TODO make submit on Enter https://github.com/daattali/advanced-shiny/blob/master/proxy-click/app.R
  28.     textOutput("message"),
  29.     HTML("<br /><br />This app is developed by <a href=http://golos.io/@oxygendependant>@oxygendependant</A> and is using GolosSQL server, thanks to <a href=http://golos.io/@arcange>@arcange</a>.
  30.         <br /><a href=https://github.com/oxyen/golos-post-bandwidth>Source code at github</A>")
  31.     )
  32.   )
  33.  
  34.  
  35. # Define server logic required to draw a histogram
  36. server <- function(input, output) {
  37.   output$message<-renderText({
  38.     name<-validinp_character(input$name)
  39.     if (input$goButton == 0)
  40.       return()
  41.     msg<-isolate({
  42.       dbhandle <- odbcDriverConnect('Driver=FreeTDS;TDS_Version=7.0;Server=sql.golos.cloud;Port=1433;Uid=golos;Pwd=golos')
  43.       # TODO Need a check if the username exists at all (and there wasn't a typo)
  44.       last.created <- sqlQuery(dbhandle,
  45.                                paste0('SELECT Top 1 created FROM Comments WHERE depth=0
  46.                                      AND author=\'', name, '\'
  47.                                      ORDER BY Created DESC'))[[1]]
  48.       last.created <- sqlQuery(dbhandle,
  49.                                paste0('SELECT Top 1 created FROM Comments WHERE depth=0
  50.                                      AND author=\'', name, '\'
  51.                                      ORDER BY Created DESC'))[[1]]
  52.       in30min <- sqlQuery(dbhandle,
  53.                           paste0('SELECT COUNT(*) FROM Comments WHERE depth=0
  54.                                 AND author=\'', name, '\'
  55.                                 AND created >= DateAdd(mi, -30, GETUTCDATE())'))[[1]]
  56.       last.bandw <- sqlQuery(dbhandle,
  57.                              paste0('SELECT Top 1 post_bandwidth FROM Accounts
  58.                                    WHERE name=\'', name, '\''))[[1]]
  59.      
  60.       orig.time <- Sys.time()
  61.       gmttime <- function(x) as.character(as.POSIXlt(x, tz="GMT"))
  62.       curtime <- gmttime(orig.time)
  63.       passed <- as.integer(difftime(curtime, last.created, units="secs"))
  64.       bandw <- round(last.bandw * (1 - min(passed, 86400) / 86400))
  65.      
  66.       acceptable.age <- round(86400 - 2592000000 / max(last.bandw, 30000))
  67.       # 2592000000 = 30k * (60 * 60 * 24)
  68.      
  69.       extra.bandw <- max(bandw - 30000, 0)
  70.      
  71.       msg<-paste0("Currently consumed bandwidth is ",
  72.                   bandw,
  73.                   " (be sure that GolosSQL has catched up on the latest ones - it knows of ",
  74.                   in30min,
  75.                   " posts from the last 30 minutes).",
  76.                   if(extra.bandw > 0) {
  77.                     remaining_time_min <-  (acceptable.age - passed) / 60
  78.                     penalty <- round(100 * (1 - (40000 / (bandw + 10000)) ^ 2))
  79.                     paste0(" Remaining time is ",
  80.                            remaining_time_min%/%60,
  81.                            " hours ",
  82.                            round(remaining_time_min%%60),
  83.                            " minutes. If you were to post right now, penalty would be ",
  84.                            penalty,
  85.                            "%.")
  86.                   }
  87.                   else
  88.                     ". It's ok to post now."
  89.       )
  90.       odbcClose(dbhandle)
  91.       return(msg)
  92.     })
  93.   })
  94. }
  95. # Run the application
  96. shinyApp(ui = ui, server = server)
  97.  
  98. # For debug
  99. # sqlQuery(dbhandle, 'SELECT top 1 url FROM Comments WHERE reward_weight < 10000 ORDER BY Created DESC'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement