LoginSignup
0
1

More than 5 years have passed since last update.

Swift 2chへの書き込み

Last updated at Posted at 2018-12-01

Alamofireを使っての2chへの書き込み

    func postTo2ch(_ urlString: String, _ postText: String) {

        let url     = URL(string: urlString)
        let domain  = (url?.host)!  // ドメイン

        let subStrings  = domain.components(separatedBy: ".")
        let server      = subStrings[0] // サーバー

        let paths   = urlString.components(separatedBy: "/")
        let thread  = paths[paths.count-2]  // スレッド
        let board   = paths[paths.count-3]  // 掲示板種別

        let bbsUrl  = "https://\(server).5ch.net/test/bbs.cgi"  // 投稿先CGI

        let headers: HTTPHeaders = [
            "Referer": urlString,
            "Accept-Encoding": "gzip",
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "Accept-Language": "en-US,en;q=0.8",
            "Connection": "keep-alive"
        ]
        let parameters: [String: Any] = [
            "bbs": board,
            "key": thread,
            "time": Int(Date().timeIntervalSince1970) - 60,
            "FROM": "",
            "subject": "",
            "mail": "",
            "MESSAGE": postText,
            "submit": "書き込む"
        ]

        Alamofire.request(bbsUrl, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString { response in
            // 初回アクセス時のみクッキーを設定する
            if response.description.contains("書き込み確認") { 
                let res = response.response
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: res?.allHeaderFields as! [String : String], for: (res?.url!)!)
Alamofire.SessionManager.default.session.configuration.httpCookieStorage?.setCookies(cookies, for: response.response?.url, mainDocumentURL: nil)
                // クッキーを設定してもう一回投稿!
                Alamofire.request(bbsUrl, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseString { response in
                    print(response)
                }
            }
        }
    }

// 使い方
self.postTo2ch("http://awabi.2ch.sc/test/read.cgi/male/1514976917/", "テスト")
  • 改善すべき点
    • 掲示板のURLと書き込みCGIのURLが異なるようで、初回のクッキー保存が一度のアクセスでできなかった
    • 初回アクセス判定をレスポンス内の「書き込み確認」で行なっているのをやめたい
    • URLのパースももっとシンプルにしたい

https://github.com/katafuchix/2chPost

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1