add current source files
[codereview/com/tweet2pocket.git] / service / main.qml
1 import Silk.Utils 1.0
2 import QtQml 2.1
3 import QtQuick.LocalStorage 2.0
4 import '../api' as API
5
6 Timer {
7     id: root
8     running: true
9     interval: 60 * 1000
10     repeat: true
11     triggeredOnStart: true
12
13     property var config: SilkConfig {
14         id: config
15         property variant twitter: {'consumerKey': '', 'consumerSecret': ''}
16         property variant pocket: {'consumerKey': '', 'redirectUri': ''}
17     }
18     
19     property var twitter: API.Twitter {
20         id: twitter
21         consumerKey: config.twitter.consumerKey
22         consumerSecret: config.twitter.consumerSecret
23     }
24     
25     property var pocket: API.Pocket {
26         id: pocket
27         consumerKey: config.pocket.consumerKey
28     }
29
30     function retrieve(data) {
31         console.debug('+ retrieve', data.twitter_screen_name, data.pocket_username, data.twitter_since_id)
32         var request = new XMLHttpRequest
33         request.onreadystatechange = function() {
34             switch (request.readyState) {
35             case XMLHttpRequest.DONE: {
36                 pocket.token = data.pocket_token
37                 var reply = JSON.parse(request.responseText)
38                 for (var i in reply) {
39                     var id_str = reply[i].id_str
40                     var text = reply[i].text
41                     switch (true) {
42                     case /^あとで/.test(text):
43                     case /^後で/.test(text):
44                         console.debug(data.twitter_screen_name, text)
45                         pocket.add({'url': 'https://twitter.com/%1/status/%2'.arg(data.twitter_screen_name).arg(id_str), 'title': text, 'tweet_id': id_str})
46                         break
47                     }
48                 }
49                 if (reply.length > 0) {
50                     data.twitter_since_id = reply[0].id_str
51
52                     var db = LocalStorage.openDatabaseSync('tweet2pocket', '1.0', '', 10000)
53                     var sql, params
54                     db.transaction(
55                                 function(tx) {
56                                     try {
57                                         sql = 'UPDATE Account SET twitter_since_id = ? WHERE id = ?'
58                                         params = [data.twitter_since_id, data.id]
59                                         var rs = tx.executeSql(sql, params)
60                                     } catch(e) {
61                                         console.debug('retrieve', e, sql, params)
62                                     }
63                                 })
64                     if (reply.length === 100) {
65                         retrieve(data)
66                     }
67                 }
68                 break }
69             }
70         }
71         var api = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
72         twitter.token = data.twitter_token
73         twitter.tokenSecret = data.twitter_token_secret
74         twitter.user_id = data.twitter_user_id
75         twitter.screen_name = data.twitter_screen_name
76
77         var params = {
78             'count': 100
79             , 'user_id': twitter.user_id
80         }
81         if (data.twitter_since_id)
82             params.since_id = data.twitter_since_id
83
84         var header = twitter.authHeader('GET', api, params)
85         var query = []
86         for (var key in params) {
87             query.push('%1=%2'.arg(key).arg(params[key]))
88         }
89         console.debug('%1?%2'.arg(api).arg(query.join('&')), twitter.token, twitter.tokenSecret)
90         request.open('GET', '%1?%2'.arg(api).arg(query.join('&')))
91         request.setRequestHeader('Authorization', header)
92         request.send()
93         console.debug('- retrieve')
94     }
95     
96     onTriggered: {
97         console.debug('+ onTriggered')
98         var db = LocalStorage.openDatabaseSync('tweet2pocket', '1.0', '', 10000)
99         var sql, params
100         db.transaction(
101                     function(tx) {
102                         try {
103                             sql = '
104 SELECT id
105 , twitter_token
106 , twitter_token_secret
107 , twitter_user_id
108 , twitter_screen_name
109 , twitter_since_id
110 , pocket_token
111 , pocket_username
112 FROM Account
113 WHERE pocket_token IS NOT NULL'
114
115                             var rs = tx.executeSql(sql)
116
117                             for (var i = 0; i < rs.rows.length; i++) {
118                                 var item = rs.rows.item(i)
119                                 retrieve(item)
120                             }
121                         } catch(e) {
122                             console.debug('onTriggered', e, sql)
123                         }
124                     })
125         console.debug('- onTriggered')
126     }
127 }
128