| Changes to /trunk/v3/GenerateAdCodeSnippet.py |
r0 vs. r2
Edit
|
r2
|
| /trunk/v3/GenerateAdCodeSnippet.py r0 | /trunk/v3/GenerateAdCodeSnippet.py r2 | ||
| 1 | #!/usr/bin/python2.2 | ||
|---|---|---|---|
| 2 | # | ||
| 3 | # Copyright 2006, Google Inc. All Rights Reserved. | ||
| 4 | |||
| 5 | ##################################################################### | ||
| 6 | # Sample code to add a generate ad code snippet through Adsense API # | ||
| 7 | ##################################################################### | ||
| 8 | |||
| 9 | import SOAPpy | ||
| 10 | |||
| 11 | server = "https://sandbox.google.com" | ||
| 12 | |||
| 13 | dev_email = "REPLACE WITH DEVELOPER EMAIL" | ||
| 14 | dev_password = "REPLACE WITH DEVELOPER PASSWORD" | ||
| 15 | client_id = "REPLACE WITH CLIENT ID" | ||
| 16 | |||
| 17 | # Set headers | ||
| 18 | headers = SOAPpy.Types.headerType() | ||
| 19 | headers.developer_email = dev_email | ||
| 20 | headers.developer_password = dev_password | ||
| 21 | headers.client_id = client_id | ||
| 22 | |||
| 23 | # Set service connection | ||
| 24 | service = SOAPpy.SOAPProxy( | ||
| 25 | server + "/api/adsense/v3/AccountService", | ||
| 26 | namespace="http://www.google.com/api/adsense/v3", | ||
| 27 | header=headers) | ||
| 28 | # To view xml request/response set service.config.debug = 1 | ||
| 29 | service.config.debug = 0 | ||
| 30 | |||
| 31 | syn_service_type = SOAPpy.Types.structType(name="synServiceTypes") | ||
| 32 | syn_service_type._addItem(name="value",value="ContentAds") | ||
| 33 | |||
| 34 | # Get the Syndication ID | ||
| 35 | # Might throw a soap exception on user input or error upon trying to connect | ||
| 36 | response = service.getSyndicationService(syn_service_type) | ||
| 37 | syn_id = response["id"] | ||
| 38 | print "syn id :", syn_id | ||
| 39 | |||
| 40 | |||
| 41 | # Set another service connetion | ||
| 42 | service = SOAPpy.SOAPProxy( | ||
| 43 | server + "/api/adsense/v3/AdSenseForContentService", | ||
| 44 | namespace="http://www.google.com/api/adsense/v3", | ||
| 45 | header=headers) | ||
| 46 | service.config.debug = 0 | ||
| 47 | |||
| 48 | # Set up the ad style | ||
| 49 | ad_style_template = """ | ||
| 50 | <backgroundColor>%s</backgroundColor> | ||
| 51 | <borderColor>%s</borderColor> | ||
| 52 | <name>%s</name> | ||
| 53 | <textColor>%s</textColor> | ||
| 54 | <titleColor>%s</titleColor> | ||
| 55 | <urlColor>%s</urlColor> | ||
| 56 | """ | ||
| 57 | |||
| 58 | ad_style = ad_style_template % ('#FFFFFF', '#0000FF', 'demoStyle', | ||
| 59 | '#00FF00', '#FF0000', '#FFFF00') | ||
| 60 | ad_style = SOAPpy.Types.untypedType(name="adStyle",data=ad_style) | ||
| 61 | #ad_style._setAttr( | ||
| 62 | # 'xmlns:impl', | ||
| 63 | # 'http://www.google.com/api/adsense/v3') | ||
| 64 | #ad_style._setAttr('xsi:type', 'impl:AdStyle') | ||
| 65 | |||
| 66 | # Set up other parameters | ||
| 67 | syn_service_id = SOAPpy.Types.untypedType(name="synServiceId",data=syn_id) | ||
| 68 | ad_unit_type = SOAPpy.Types.structType(name="adUnitType") | ||
| 69 | ad_unit_type._addItem(name="value",value="TextOnly") | ||
| 70 | layout = SOAPpy.Types.structType(name="adLayout") | ||
| 71 | layout._addItem(name="value",value="728x90") | ||
| 72 | alternate = SOAPpy.Types.untypedType(name="alternate",data="#FFFFFF") | ||
| 73 | is_framed_page = SOAPpy.Types.untypedType(name="isFramedPage",data="False") | ||
| 74 | channel_name = SOAPpy.Types.untypedType(name="channelName",data="") | ||
| 75 | corner_styles = SOAPpy.Types.structType(name="cornerStyles") | ||
| 76 | corner_styles._addItem(name="value",value="DEFAULT") | ||
| 77 | |||
| 78 | # Generate ad code, might throw a soap exception | ||
| 79 | # on user input or error upon trying to connect | ||
| 80 | response = service.generateAdCode(syn_service_id, ad_style, ad_unit_type, | ||
| 81 | layout, alternate, is_framed_page, | ||
| 82 | channel_name,corner_styles) | ||
| 83 | |||
| 84 | print "Ad code snippet :" | ||
| 85 | print response | ||