| 
 | 1 | +from pprint import pprint  | 
 | 2 | +from pubnub.pubnub import PubNub  | 
 | 3 | +from pubnub.pnconfiguration import PNConfiguration  | 
 | 4 | + | 
 | 5 | +config = PNConfiguration()  | 
 | 6 | +config.publish_key = 'demo'  | 
 | 7 | +config.subscribe_key = 'demo'  | 
 | 8 | +config.user_id = 'example'  | 
 | 9 | + | 
 | 10 | +channel = "demo_example"  | 
 | 11 | +help_string = "\tTo exit type '/exit'\n\tTo show the current object type '/show'\n\tTo show this help type '/help'\n"  | 
 | 12 | + | 
 | 13 | +pubnub = PubNub(config)  | 
 | 14 | + | 
 | 15 | +print(f"We're setting the channel's {channel} additional info. \n{help_string}\n")  | 
 | 16 | + | 
 | 17 | +name = input("Enter the channel name: ")  | 
 | 18 | +description = input("Enter the channel description: ")  | 
 | 19 | + | 
 | 20 | +# Setting the basic channel info  | 
 | 21 | +set_result = pubnub.set_channel_metadata(  | 
 | 22 | +    channel,  | 
 | 23 | +    name=name,  | 
 | 24 | +    description=description,  | 
 | 25 | +).sync()  | 
 | 26 | +print("The channel has been created with name and description.\n")  | 
 | 27 | + | 
 | 28 | +# We start to iterate over the custom fields  | 
 | 29 | +while True:  | 
 | 30 | +    # First we have to get the current object to know what fields are already set  | 
 | 31 | +    current_object = pubnub.get_channel_metadata(  | 
 | 32 | +        channel,  | 
 | 33 | +        include_custom=True,  | 
 | 34 | +        include_status=True,  | 
 | 35 | +        include_type=True  | 
 | 36 | +    ).sync()  | 
 | 37 | + | 
 | 38 | +    # Gathering new data  | 
 | 39 | +    field_name = input("Enter the field name: ")  | 
 | 40 | +    if field_name == '/exit':  | 
 | 41 | +        break  | 
 | 42 | +    if field_name == '/show':  | 
 | 43 | +        pprint(current_object.result.data, indent=2)  | 
 | 44 | +        print()  | 
 | 45 | +        continue  | 
 | 46 | +    if field_name == '/help':  | 
 | 47 | +        print(help_string, end="\n\n")  | 
 | 48 | +        continue  | 
 | 49 | + | 
 | 50 | +    field_value = input("Enter the field value: ")  | 
 | 51 | + | 
 | 52 | +    # We may have to initialize the custom field  | 
 | 53 | +    custom = current_object.result.data.get('custom', {})  | 
 | 54 | +    if custom is None:  | 
 | 55 | +        custom = {}  | 
 | 56 | + | 
 | 57 | +    # We have to check if the field already exists and  | 
 | 58 | +    if custom.get(field_name):  | 
 | 59 | +        confirm = input(f"Field {field_name} already has a value. Overwrite? (y/n):").lower()  | 
 | 60 | +        while confirm not in ['y', 'n']:  | 
 | 61 | +            confirm = input("Please enter 'y' or 'n': ").lower()  | 
 | 62 | +        if confirm == 'n':  | 
 | 63 | +            print("Object will not be updated.\n")  | 
 | 64 | +            continue  | 
 | 65 | +        if confirm == 'y':  | 
 | 66 | +            custom[field_name] = field_value  | 
 | 67 | +    else:  | 
 | 68 | +        custom[field_name] = field_value  | 
 | 69 | + | 
 | 70 | +    # Writing the updated object back to the server  | 
 | 71 | +    set_result = pubnub.set_channel_metadata(  | 
 | 72 | +        channel,  | 
 | 73 | +        custom=custom,  | 
 | 74 | +        name=current_object.result.data.get('name'),  | 
 | 75 | +        description=current_object.result.data.get('description')  | 
 | 76 | +    ).sync()  | 
 | 77 | +    print("Object has been updated.\n")  | 
0 commit comments