Advertisement
Guest User

ads.brs

a guest
Jan 19th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ads()
  2.    
  3.     G = conf()
  4.    
  5.     this = {}
  6.    
  7.     this.G = G
  8.     this.adIface = Roku_Ads()
  9.    
  10.     if G.ENV = "development"
  11.         this.adIface.setDebugOutput(true)
  12.     end if
  13.    
  14.     if G.NIELSEN_APP_ID <> invalid
  15.         this.adIface.enableNielsenDAR(true)
  16.         this.adIface.setNielsenAppId(G.NIELSEN_APP_ID)
  17.     end if
  18.    
  19.     this.adIface.setAdPrefs(false)
  20.    
  21.     this.set_video_content = set_video_content
  22.     this.ads_to_play = ads_to_play
  23.     this.play_ads = play_ads
  24.     this.site_uuid = "INVALID"
  25.    
  26.     return this
  27.  
  28. end function
  29.  
  30. function set_video_content(video_content)
  31.  
  32.     this = m
  33.  
  34.     this.video_content = video_content
  35.    
  36.     if this.video_content.site_uuid <> invalid
  37.         this.site_uuid = this.video_content.site_uuid
  38.     end if
  39.    
  40.     if this.video_content.cuepoints <> invalid and this.video_content.cuepoints.count() > 0
  41.         this.cuepoints = this.video_content.cuepoints
  42.     else
  43.         ?"There are no cuepoints!"
  44.         backtraces = []
  45.         backtraces.Push("video_content.cuepoints: " + any_to_string(video_content.cuepoints))
  46.         backtraces.Push("video_content.video_uuid: " + any_to_string(video_content.video_uuid))
  47.         backtraces.Push("video_content: " + any_to_string(video_content))
  48.         airbrake().add_error("no_cuepoints", "no cuepoints when trying to play ads", "/source/ads.brs", "set_video_content()", backtraces).send()
  49.     end if
  50.    
  51.     this.consume_cuepoints = []
  52.     for each cp in this.cuepoints
  53.         this.consume_cuepoints.push(cp)
  54.     end for
  55.    
  56.     if this.video_content.ovg_ads <> invalid and this.video_content.ovg_ads.vast_tag <> invalid
  57.         this.vast_tag = this.video_content.ovg_ads.vast_tag
  58.     else if this.video_content.ovg_ads <> invalid and this.video_content.ovg_ads.provider <> invalid
  59.         this.vast_tag = this.video_content.ovg_ads.provider.vast_tag
  60.     else
  61.         ?"There was no vast tag!"
  62.         backtraces = []
  63.         if this.video_content.ovg_ads <> invalid
  64.             backtraces.Push("this.video_content.ovg_ads.provider: " + any_to_string(this.video_content.ovg_ads.provider))
  65.             backtraces.Push("video_content.ovg_ads.vast_tag: " + any_to_string(video_content.ovg_ads.vast_tag))
  66.         end if
  67.         backtraces.Push("video_content.video_uuid: " + any_to_string(video_content.video_uuid))
  68.         backtraces.Push("video_content: " + any_to_string(video_content))
  69.         airbrake().add_error("no_vast_tag", "no vast tag when trying to play ads", "/source/ads.brs", "set_video_content()", backtraces).send()
  70.        
  71.     end if
  72.    
  73.     if this.video_content.nielsen_genre <> invalid and len(this.video_content.nielsen_genre) > 0
  74.         this.adIface.setNielsenGenre(this.video_content.nielsen_genre)
  75.     end if
  76.    
  77.     if this.video_content.Categories <> invalid and this.video_content.Categories.count() > 0
  78.         genres = this.video_content.Categories
  79.         g_string = ""
  80.         total_genres = genres.count()
  81.         loop_i = 0
  82.         for each genre in genres
  83.             loop_i = loop_i + 1
  84.             if g_string.instr(genre) = -1
  85.                 g_string = g_string + genre
  86.                 if loop_i <> total_genres
  87.                     g_string = g_string + ","
  88.                 end if
  89.             end if
  90.         end for
  91.         this.adIface.setContentGenre(g_string)
  92.     end if
  93.    
  94.     this.adIface.setNielsenProgramId(this.video_content.title)
  95.     this.adIface.setContentLength(any_to_int(this.video_content.duration))
  96.    
  97.     if this.video_content.ovg_ads <> invalid
  98.         if this.video_content.ovg_ads.n_midroll_ads <> invalid
  99.             this.n_midroll_ads = this.video_content.ovg_ads.n_midroll_ads
  100.         else if this.video_content.ovg_ads.n_ads <> invalid
  101.             this.n_midroll_ads = this.video_content.ovg_ads.n_ads
  102.         else
  103.             ?"There was no n_midroll_ads!"
  104.             backtraces = []
  105.             backtraces.Push("this.video_content.ovg_ads.n_midroll_ads: " + any_to_string(this.video_content.ovg_ads.n_midroll_ads))
  106.             backtraces.Push("this.video_content.ovg_ads.n_ads: " + any_to_string(this.video_content.ovg_ads.n_ads))
  107.             backtraces.Push("video_content.video_uuid: " + any_to_string(video_content.video_uuid))
  108.             backtraces.Push("video_content: " + any_to_string(video_content))
  109.             airbrake().add_error("no_midroll_ads", "no n_midroll_ads when trying to play ads", "/source/ads.brs", "set_video_content()", backtraces).send()
  110.         end if
  111.     else
  112.         ?"There was no n_midroll_ads!"
  113.         backtraces = []
  114.         backtraces.Push("this.video_content.ovg_ads: " + any_to_string(this.video_content.ovg_ads))
  115.         backtraces.Push("video_content.video_uuid: " + any_to_string(video_content.video_uuid))
  116.         backtraces.Push("video_content: " + any_to_string(video_content))
  117.         airbrake().add_error("no_ovg_ads", "no ovg_ads when trying to play ads", "/source/ads.brs", "set_video_content()", backtraces).send()  
  118.    
  119.     end if
  120.    
  121.     if this.video_content.ovg_ads <> invalid and this.video_content.ovg_ads.n_preroll_ads <> invalid
  122.         this.n_preroll_ads = this.video_content.ovg_ads.n_preroll_ads
  123.     else if this.video_content.ovg_ads <> invalid and this.video_content.ovg_ads.n_ads <> invalid
  124.         this.n_preroll_ads = this.video_content.ovg_ads.n_ads
  125.     else
  126.         ?"There was no n_preroll_ads!"
  127.         backtraces = []
  128.         if this.video_content.ovg_ads <> invalid
  129.             backtraces.Push("this.video_content.ovg_ads.n_preroll_ads: " + any_to_string(this.video_content.ovg_ads.n_preroll_ads))
  130.             backtraces.Push("this.video_content.ovg_ads.n_ads: " + any_to_string(this.video_content.ovg_ads.n_ads))
  131.         end if
  132.         backtraces.Push("video_content.video_uuid: " + any_to_string(video_content.video_uuid))
  133.         backtraces.Push("video_content: " + any_to_string(video_content))
  134.         airbrake().add_error("no_preroll_ads", "no n_preroll_ads when trying to play ads", "/source/ads.brs", "set_video_content()", backtraces).send()
  135.     end if
  136.    
  137.     this.ad_received = false
  138.    
  139.     if this.video_content.ContentType = 4
  140.         this.ga_label = this.video_content.TitleSeason + " | " + this.video_content.Title + " | " + this.video_content.ShortDescriptionLine2
  141.     else
  142.         this.ga_label = this.video_content.Title
  143.     end if
  144.  
  145.     return this
  146.  
  147. end function
  148.  
  149. function ads_to_play(position)
  150.    
  151.     this = m
  152.    
  153.     if this.video_content <> invalid
  154.        
  155.         i = 0
  156.         play_ad = false
  157.         for each cuepoint in this.consume_cuepoints
  158.             if position = cuepoint
  159.                 play_ad = true
  160.                 ' detete this to prevent users from seeing the same ad break if they rewind
  161.                 ' the Roku will only resume at a segment boundary, so you can't seek into a segment.
  162.                 ' if the segment starts at 19 seconds and is 10 seconds long, if you seek to anything between 19 and 29 seconds, it will resume at 19.
  163.                 ' to fix this we need to delete the cue point that we are about to play so it doesn't play again
  164.                 ' http://forums.roku.com/viewtopic.php?f=34&t=93694&p=525516&hilit=seek#p525516
  165.                 this.consume_cuepoints.delete(i)
  166.                 exit for
  167.             end if
  168.             i = i + 1
  169.         end for
  170.        
  171.         if play_ad
  172.             'check the queue points
  173.             if position = 0
  174.                 'pre roll
  175.                
  176.                 'if we have preroll ads
  177.                 if this.n_preroll_ads <> invalid and this.n_preroll_ads > 0
  178.                     return this.n_preroll_ads
  179.                 end if
  180.                
  181.             else
  182.                 'mid/post roll
  183.                
  184.                 'if we have midroll ads
  185.                 if this.n_midroll_ads <> invalid and this.n_midroll_ads > 0
  186.                     return this.n_midroll_ads
  187.                 end if
  188.    
  189.             end if
  190.         end if
  191.     end if
  192.  
  193.     return invalid
  194.    
  195. end function
  196.  
  197. function play_ads(total_ads_in_pod)
  198.     'Request the ad
  199.    
  200.     this = m
  201.    
  202.     facade = CreateObject("roScreen")
  203.    
  204.     for ad_number = 0 to total_ads_in_pod-1
  205.    
  206.         updated_vast = vast_updateParams(this.vast_tag)
  207.         this.adIface.setAdUrl(updated_vast)
  208.         ' Analytics = Ad Requested
  209.         ga_track_event("Ads", "Ad Requested", this.ga_label)
  210.  
  211.         adPods = this.adIface.getAds()
  212.        
  213.         if adPods <> invalid and adPods.count() > 0
  214.             ' Analytics = Ad Recieved
  215.             ga_track_event("Ads", "Ad Received - All Providers", this.ga_label)
  216.             ad = adPods[0].ads[0]
  217.            
  218.             'Fix the ad count overlay just add invalid ads to the beginning of the pod
  219.             for i=0 to total_ads_in_pod-1
  220.                 if i = ad_number
  221.                     adPods[0].ads[i] = ad
  222.                 else
  223.                     adPods[0].ads[i] = invalid
  224.                 end if
  225.             end for
  226.  
  227.             ad_completed = this.adIface.showAds(adPods)
  228.            
  229.             if ad_completed
  230.                 ' Analytics = Ad Played To Completion
  231.                 ga_track_event("Ads", "Ad Played to Completion - All Providers", this.ga_label)
  232.                
  233.                 'track ad plays by site
  234.                 ga_track_event("Ads by Site", this.site_uuid, this.ga_label)
  235.                
  236.             else
  237.                 ' the user backed out of the ad so hide the player and return false
  238.                 ' Analytics = User Exited Ad
  239.                 ga_track_event("Ads", "Ad Exited by User", this.ga_label)
  240.                 'We want to be able to skip ads in dev env
  241.                 facade = invalid
  242.                 if this.G.ENV = "development"
  243.                     return true
  244.                 else
  245.                     return false
  246.                 end if
  247.                
  248.             end if
  249.            
  250.         else
  251.             'We didn't get any ads so just continue
  252.             ' Analytics = Didn't get any Ads
  253.             ga_track_event("Ads", "Ad NOT Received - All Providers", this.ga_label)
  254.         end if
  255.    
  256.     end for
  257.    
  258.     'if we made it here, the user didn't back out of the ads so lets play the video
  259.     facade = invalid
  260.     return true
  261.    
  262. end function
  263.  
  264.  
  265. function vast_updateParams(url as string) as string
  266.  
  267.     print "=== Updating VAST Parameters..." url
  268.     'Update VAST Parameters
  269.     vast_parameters = url.Tokenize("&")
  270.     updated_vast = ""
  271.  
  272.     roUrlTransfer_http = createobject("roUrlTransfer")
  273.  
  274.     for each param in vast_parameters
  275.  
  276.         print "param: "param
  277.  
  278.         if instr(1, param, "description_url=[description_url]")
  279.             param = "description_url=" + roUrlTransfer_http.escape("http://www.ovguide.com")
  280.         else if instr(1, param, "refUrl=[Ref_Url]")
  281.             param = "refUrl=" + roUrlTransfer_http.escape("http://www.ovguide.com")
  282.         else if 0 < instr(1, param, "url=[referrer_url]")
  283.             param = "url=" + roUrlTransfer_http.escape("http://www.ovguide.com")
  284.         else if 0 < instr(1, param, "correlator=")
  285.             now = createobject("rodatetime")
  286.             param = "correlator=" + any_to_string(now.asseconds() + rnd(1000))
  287.             print "*** CORRELATOR: " + param
  288.         end if      
  289.  
  290.         if 0 < instr(1, param, "[")
  291.             param = strReplace(param, "[", "%5b")
  292.         else if 0 < instr(1, param, "]")
  293.             param = strReplace(param, "]", "%5d")
  294.         end if
  295.  
  296.         if 1 = instr(1, left(param, 7), "http://")
  297.             updated_vast = param
  298.         else if 1 = instr(1, left(param, 8), "https://")
  299.             updated_vast = param
  300.         else
  301.             updated_vast = updated_vast + "&" + param
  302.         end if
  303.  
  304.     next
  305.  
  306.     if 0 < instr(1, updated_vast, ";cb=[timestamp]")
  307.         now = createobject("rodatetime")
  308.         updatedParam = ";cb=" + any_to_string(now.asseconds())
  309.         updated_vast = strreplace(updated_vast, ";cb=[timestamp]", updatedParam)
  310.     end if
  311.  
  312.     sambaPlatFormIdRegEx = CreateObject("roRegex", "SAMBA_PLATFORM_ID", "")
  313.     updated_vast = sambaPlatFormIdRegEx.ReplaceAll(updated_vast, "roku")
  314.  
  315.     print "=== UPDATED VAST "updated_vast
  316.  
  317.     return updated_vast
  318.  
  319. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement