Skip to content

Instantly share code, notes, and snippets.

@nichochar
Created August 6, 2016 17:10
Show Gist options
  • Save nichochar/cfcbdcc2ff113a87e6b70fcc32231b61 to your computer and use it in GitHub Desktop.
Save nichochar/cfcbdcc2ff113a87e6b70fcc32231b61 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import sys
JS = """
// DFP Static button for %s
(function() {
var currentScript = window.document.currentScript;
window.urxInit = function() {
var API_KEY = '%s';
// Get the macro'd DFP values
var dfpContentUrl = window['__dfpContentUrl'];
var dfpClickUrl = window['__dfpClickUrl'];
var client = new urx.Client(API_KEY, {
redirectResolution: true, // iframe units (such as DFP) should use redirects for resolutions
prefixedRedirectUrl: dfpClickUrl // If the ad server provides a click url to prefix, pass it here
});
var button = new urx.Button(
client, {
alternateReferer: dfpContentUrl,
unitId: '%s_dfp_web_button',
element: window.document.body
});
// Second parameter is optional headers, third is context url for autoquery building
button.search("whatever");
}
var urxButtonScriptElement = document.createElement('script');
var sdkBaseUrl = window.localStorage.getItem("BASE_SDK_URL_KEY") || "https://static.urx.io";
urxButtonScriptElement.setAttribute('src', sdkBaseUrl + '/sdk/latest/button.min.js');
urxButtonScriptElement.setAttribute('init', 'urxInit');
currentScript.parentNode.insertBefore(urxButtonScriptElement, currentScript);
})();
"""
def create_parser():
parser = argparse.ArgumentParser(description="URX DFP JS code generator")
parser.add_argument(
'name',
type=str,
help='the human readable name of the argument, VICE')
parser.add_argument(
'api_key',
type=str,
help='the api key')
parser.add_argument(
'unit_id',
type=str,
help='the unit_id: vice_1 will become vice_1_dfp_web_button')
return parser
def parse_args(parser):
"""
Parses the arguments from the parser passed in as an argument.
Validates them, raises errors if they don't match
Returns
-------
A tuple of 3 strings
(the query terms, the api key, the unit id)
"""
args = parser.parse_args()
if not args.api_key or not args.unit_id:
sys.exit('You need an api key and a unit id')
return args.name, args.api_key, args.unit_id
def main():
parser = create_parser()
name, api_key, unit_id = parse_args(parser)
return JS % (name, api_key, unit_id)
if __name__ == '__main__':
response = main()
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment