The equation of motion is
$$(1+e\cos f)\frac{d^2\theta}{df^2}-2e\sin f\frac{d\theta}{df}-\frac{w^2}{2}\sin 2(f-\theta)=0,$$ where $0<e<1$, $w\in const$. Taking $q=\theta$ and $p=\frac{d\theta}{df}$, with the use of the Hamilton equations
$$\frac{dq}{df}=\frac{\partial H}{\partial p},\qquad \frac{dp}{df}=-\frac{\partial H}{\partial q},$$
how can I find the Hamiltonian $H=H(p,q,f)$?