We present a new method to solve the holomorphic anomaly equations governing the free energies of type B topological strings. The method is based on direct integration with respect to the non-holomorphic dependence of the amplitudes, and relies on the interplay between non-holomorphicity and modularity properties of the topological string amplitudes. We develop a formalism valid for any Calabi-Yau manifold and we study in detail two examples, providing closed expressions for the amplitudes at low genus, as well as a discussion of the boundary conditions that fix the holomorphic ambiguity. The first example is the non-compact Calabi-Yau underlying Seiberg-Witten theory and its gravitational corrections. The second example is the Enriques Calabi-Yau, which we solve in full generality up to genus six. We discuss various aspects of this model: we obtain a new method to generate holomorphic automorphic forms on the Enriques moduli space, we write down a new product formula for the fiber amplitudes at all genus, and we analyze in detail the field theory limit. This allows us to uncover the modularity properties of SU(2), N = 2 super Yang-Mills theory with four massless hypermultiplets.