Former Democratic congressman and political celebrity Anthony Weiner asked a federal judge late Wednesday to spare him prison for sexting with a teenager in a filing that came just hours after appearing with his estranged wife, Huma Abedin, for the couple’s first state divorce court hearing.

The memo to Manhattan U.S. District Judge Denise Cote, who will sentence onetime mayoral candidate Weiner next week, said the teen lured Weiner and plotted his demise to profit from a book, and included letters seeking leniency from both Weiner and Abedin, a top aide to Hillary Clinton.

Weiner’s heavily redacted six-page self-analytical letter said he was on the road to recovery through commitment to his 5-year-old son and treatment for an addiction to adulation he barely recognized before, and expressed deep regret for his behavior toward both the teenage girl he flirted with and Abedin.

“I have endangered the well-being of a 15-year-old girl who reached out to me on the internet,” he wrote. “My continued acting out over years crushed the aspirations of my wife and ruined our marriage.”

The publicly filed copy of Abedin’s shorter letter had even more blackouts and focused on their young son, saying that Weiner loved the boy and she was writing the letter to minimize the “negative impact” on their child.

“This is not a letter I ever imagined I would write,” she told the judge, “but, with Anthony, I have repeatedly found myself in circumstances I never imagined.”

Weiner, 52, pleaded guilty in May to sending obscene material to a minor, admitting that he sent sexually explicit material to the North Carolina teen and gave her directions to engage in sexual conduct through online messages and video chats.

The sexting occurred in early 2016, but the scandal led to the discovery of emails from Clinton last fall that caused the FBI to reopen its probe of the Democratic presidential candidate’s email practices, damaging her campaign.

Weiner faces up to 10 years in prison, and in his plea agreement prosecutors said they would take the position that a sentence of 21 to 27 months in prison would be “fair and appropriate.”

Arlo Devlin-Brown, Weiner’s lawyer, told Cote that the teen’s behavior was no excuse for Weiner — whose online exhibitionist behavior under the pseudonym Carlos Danger dated to his resignation from Congress in 2011 — but that she had precipitated his behavior.

Brown said the girl had admitted to investigators that she sought out Weiner online in hopes that she could profit from a book deal through the encounters, even anticipating that she might be able to influence the election, and eventually sold her story to the Daily Mail in Britain for $30,000.

The lawyer said Weiner had a sick craving for attention traceable to his “troubled” childhood but had “no deviant sexual interest in minors,” and the girl’s attitudes made the case dramatically different from one in which an older man lures a minor.

“We have seen no other federal prosecutions — anywhere — that involve minor victims seeking out a defendant and encouraging the defendant to engage in explicit conduct so that the defendant could be publicly exposed,” he said.

Earlier Wednesday, Abedin and Weiner appeared before a gauntlet of cameras at their first state court divorce hearing before Manhattan matrimonial judge Michael Katz, who told them that an amicable split would “certainly be better for your son.”

Their estrangement was triggered by the sexting scandal, but the couple arrived together, sat together and chatted during breaks at the divorce hearing.

Abedin’s lawyer Amy Donehower asked Katz to keep future proceedings sealed. “Because there is a child involved, we’d like to keep the proceedings secret to the extent your honor will allow,” she said.

Katz said he would rule on the secrecy motion in the future and told the couple he wanted to “hopefully help you work something out that works for your family” and “save you a lot of stress.”

Abedin and Weiner didn’t respond to reporters’ shouted questions as they left court.