File: advogato.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (48 lines) | stat: -rw-r--r-- 1,216 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
This example demonstrates how to logon to a remote server and post a diary.

Usage:
    $ python advogato.py <name> <diary entry file>
"""

import sys
from getpass import getpass

from twisted.internet import reactor
from twisted.web.xmlrpc import Proxy


class AddDiary:
    def __init__(self, name, password):
        self.name = name
        self.password = password
        self.proxy = Proxy(b"http://advogato.org/XMLRPC")

    def __call__(self, filename):
        with open(filename) as f:
            self.data = f.read()
        d = self.proxy.callRemote("authenticate", self.name, self.password)
        d.addCallbacks(self.login, self.noLogin)

    def noLogin(self, reason):
        print("could not login")
        reactor.stop()

    def login(self, cookie):
        d = self.proxy.callRemote("diary.set", cookie, -1, self.data)
        d.addCallbacks(self.setDiary, self.errorSetDiary)

    def setDiary(self, response):
        reactor.stop()

    def errorSetDiary(self, error):
        print("could not set diary", error)
        reactor.stop()


diary = AddDiary(sys.argv[1], getpass())
diary(sys.argv[2])
reactor.run()