OSDN Git Service

modify fig: check image size and add width attribute
[otptools/otptools.git] / sdtools.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 otptools base module - retain session/login info
5 """
6
7 OTP_LOGIN_URL = "http://slashdot.jp/login.pl"
8 OTP_LOGIN_HOST = "slashdot.jp"
9 OTP_LOGIN_PATH = "/login.pl"
10
11 OTP_LOGIN_PARAM = {
12     "op":"userlogin",
13     "unickname":"",
14     "returnto":"http://slashdot.jp",
15     "upasswd":"",
16 #    "login_temp":0,
17     "userlogin":"ログイン",
18     }
19
20 OTP_LIST_PATH = "/admin.pl"
21
22 BROWSER = "Mozilla/5.0 (Windows; U; Windows NT 6.0; ja; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729) "
23
24 import sys
25 import os
26 import copy
27 import urllib, urllib2
28 import httplib
29
30 class otptools(object):
31     """
32     Open Tech Press management library core module.
33     """
34
35     def __init__(self, path_cookie, login_name="", login_password=""):
36         """
37         @param path_cookie: path of file which cookie's information stored.
38         @type  path_cookie: stinrg
39
40         @param login_name: OTP's login name for use.
41         @type  login_name: string
42
43         @param login_password: OTP's login password.
44         @type  login_password: string
45         """
46         self.path_cookie = path_cookie
47         self.unickname = login_name
48         self.upasswd = login_password
49         self.cookie = ""
50
51     def login(self):
52                 """
53                 login to OTP.
54                 """
55         login_param = copy.deepcopy(OTP_LOGIN_PARAM)
56         login_param["unickname"] = self.unickname
57         login_param["upasswd"] = self.upasswd
58
59         for item in login_param:
60             print "%s > %s" % (item, login_param[item])
61
62         encoded_data = urllib.urlencode(login_param)
63         print encoded_data
64
65         headers = {
66             "User-Agent": BROWSER,
67             "Content-type": "application/x-www-form-urlencoded",
68             "Accept": "text/plain",
69             }
70
71 #        obj = urllib.urlopen(OTP_LOGIN_URL, encoded_data)
72 #        print obj.info()
73
74         obj = httplib.HTTPConnection(OTP_LOGIN_HOST)
75         obj.request("POST", OTP_LOGIN_PATH, encoded_data, headers)
76         resp = obj.getresponse()
77         headers = resp.getheaders()
78
79         for item in headers:
80             print item
81
82 #        for header in headers:
83 #            if header[0] == "set-cookie":
84 #                str_cookie = header[1]
85 #                break
86 #        else:
87 #            return -1
88 #
89 #        self.cookie = str_cookie
90 #        return 1
91
92     def save_cookie(self):
93         file_obj = open(self.path_cookie, "w")
94         file_obj.write(self.cookie)
95         file_obj.close()
96
97     def load_cookie(self):
98                 """
99                 load session cookie from file.
100                 """
101         file_obj = open(self.path_cookie, "r")
102         self.cookie = file_obj.readline()
103         file_obj.close()
104
105     def get_list(self):
106                 """
107                 Access admin.pl and retrieve stories list.
108                 """
109         headers = {
110             "User-Agent": BROWSER,
111             "Content-type": "application/x-www-form-urlencoded",
112             "Accept": "text/plain",
113             "Cookie": self.cookie,
114             }
115         obj = httplib.HTTPConnection(OTP_LOGIN_HOST)
116         obj.request("POST", OTP_LIST_PATH, "", headers)
117         resp = obj.getresponse()
118         headers = resp.getheaders()
119
120         return resp.read()
121
122