In the console, entering the first line and hitting the tab button on the keyboard:
Test if the object is selected. The selection state is per view layer
Note that the ob.select_get() returned a boolean, so my bet is that is what needs to take the place of the “if obj.select_set:” statement.
As for the node issue, I am not so knowledgeable on nodes, the code runs fine in the text editor after modifying the if statement, so I am not sure why the node type would not be changing. One thing you could try is manually changing the node, and watching in the info window what code it uses to do the switch, if it uses different code, that is likely the issue, if it does not, there is a deeper issue, also, Blender is extremely picky about what it can do based on the MODE it is in, so it may just be that the mode needs to be different.