Example #1
0
def rewrite_deploy_code(options, args):
  logging.getLogger().setLevel(-1)

  if options.name is None:
    error_exit('--name Could not be None.')

  if not os.path.exists(options.name):
    error_exit('no such file')

  name = options.name
  code, config = find_ad_config(open(name, 'rb').read())
  if code is None:
    error_exit('invalid file format, can not find var AD_CONFIG');

  only_dump_links = (options.stage == 2)
  config, links = _generate_rcv_link(config, options.link_id, only_dump_links)
  json_object = lib.json_loads(config)

  ad_canvas_id = None
  if json_object:
    ad_canvas_id = json_object.get('id')
    config = json.dumps(json_object, indent = 2).replace('\\u0001', '\x01')
  # config = zip_ad_config(config)

  # inline_script_code
  if options.stage == 2:
    # <script type="text/javascript">var LINKS_foobar12 = [...];</script>
    # <script type="text/javascript" src="ad.js"></script>
    # -- ad.js --
    # (function(){
    # var AD_CONFIG = {rcv_url:LINKS_foobar12[0], name:"HELLO"};
    # ...
    # ...
    # })();
    links_name = 'LINKS_' + lib.md5_sign(str(links))[0:8]
    code = code.replace('%AD_CONFIG%', config).replace('/** LINKS */[]', links_name)
    inline_script_code = 'var %s = %s;' % (links_name, str(links).replace('\\x01', '\x01'))
  elif options.stage == 3:
    # <script type="text/javascript">var AD_CONFIG_foobar12 = {};</script>
    # <script type="text/javascript" src="ad.js"></script>
    # -- ad.js --
    # (function(){
    # var AD_CONFIG = AD_CONFIG_foobar12;
    # ...
    # ...
    # })();
    ad_config_name = 'AD_CONFIG_' + lib.md5_sign(config)[0:8]
    code = code.replace('var AD_CONFIG = %AD_CONFIG%;', '').replace('/** AD_CONFIG */{}', ad_config_name)
    inline_script_code = 'var %s = %s;' % (ad_config_name, config)

  # external_script_src
  fp, fn = os.path.split(name)
  bcs_url = lib.upload_assets_raw(code, ".js")
  external_script_src = bcs_url if bcs_url else fn

  html_code = _get_final_html_code(ad_canvas_id, inline_script_code, external_script_src, options.async)
Example #2
0
def zip_ad_config(config):
  json_object = lib.json_loads(config)
  if json_object is None:
    return config
    """
    from Fzip import zip_js_string
    json_string = zip_js_string('var A=' + config)
    # 53是/** Copyright的长度 */
    return json_string[(53 + 7):]
    """

  return json.dumps(json_object).replace('\\u0001', '\x01')