Get position of node socket in python
You can get the location of a Node as well as the width and height, but the position of the socket does not seem to be available. This is likely to be because nodes can be in collapsed or expanded states and so a particular socket may or may not be visible at any given time.

You should be able to get pretty far knowing the dimensions and location of the node itself though. You know that all inputs are on the left side and all outputs on the right, and you can find out which nodes are linked to which, so you could do a topological sort and arrange the nodes with some minimum amount of spacing.