~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/cleaner/historyclean.py

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
 
### BEGIN LICENSE
4
 
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
5
 
# This program is free software: you can redistribute it and/or modify it
6
 
# under the terms of the GNU General Public License version 3, as published
7
 
# by the Free Software Foundation.
8
 
#
9
 
# This program is distributed in the hope that it will be useful, but
10
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
 
# PURPOSE.  See the GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License along
15
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
### END LICENSE
17
 
 
18
 
import os.path
19
 
import sqlite3
20
 
from common import process_pid
21
 
from common import get_mozilla_path
22
 
 
23
 
 
24
 
class HistoryClean():
25
 
    def __init__(self, homedir):
26
 
        self.filename = 'places.sqlite'
27
 
        self.path = get_mozilla_path(homedir)
28
 
 
29
 
    def scan_the_records(self):
30
 
        save = []
31
 
        if os.path.exists(self.path):
32
 
            scan_browser_conn = sqlite3.connect(self.path + self.filename)
33
 
            scan_browser_cur = scan_browser_conn.cursor()
34
 
            sql_select = "SELECT moz_historyvisits.place_id, moz_places.url, moz_places.title, count(*) FROM moz_historyvisits, moz_places WHERE moz_historyvisits.place_id=moz_places.id GROUP BY moz_historyvisits.place_id"
35
 
            scan_browser_cur.execute(sql_select)
36
 
            for eachvisit in scan_browser_cur.fetchall():
37
 
 
38
 
                tmp = list(eachvisit)
39
 
                tmp[0], tmp[-1] = str(tmp[0]), str(tmp[-1])
40
 
                if not isinstance(tmp[2], unicode):
41
 
                    tmp[2] = str(tmp[2])
42
 
                tmp_str = '<2_2>'.join(tmp)
43
 
                save.append(tmp_str)
44
 
            scan_browser_cur.close()
45
 
            scan_browser_conn.close()
46
 
        return save
47
 
 
48
 
    def scan_firefox_history_records(self, filepath):
49
 
        result = []
50
 
        if os.path.exists(filepath):
51
 
            scan_browser_conn = sqlite3.connect(filepath)
52
 
            scan_browser_cur = scan_browser_conn.cursor()
53
 
            sql_select = "SELECT moz_historyvisits.place_id, moz_places.url, count(*) FROM moz_historyvisits, moz_places WHERE moz_historyvisits.place_id=moz_places.id GROUP BY moz_historyvisits.place_id"
54
 
            scan_browser_cur.execute(sql_select)
55
 
            result = scan_browser_cur.fetchall()
56
 
            #result = ["%s<2_2>%s<2_2>%s" % (str(each[0]), each[1], str(each[2])) for each in allvisit]
57
 
            scan_browser_cur.close()
58
 
            scan_browser_conn.close()
59
 
        return result
60
 
 
61
 
    def scan_chromium_history_records(self, filepath):
62
 
        result = []
63
 
        if os.path.exists(filepath):
64
 
            scan_chromium_conn = sqlite3.connect(filepath)
65
 
            scan_chromium_cur = scan_chromium_conn.cursor()
66
 
            sql_select = "SELECT visits.url, urls.url, count(*) FROM visits, urls WHERE visits.url=urls.id GROUP BY visits.url"
67
 
            scan_chromium_cur.execute(sql_select)
68
 
            result = scan_chromium_cur.fetchall()
69
 
            #result = ["%s<2_2>%s<2_2>%s" % (str(each[0]), each[1], str(each[2])) for each in allvisit]
70
 
            scan_chromium_cur.close()
71
 
            scan_chromium_conn.close()
72
 
 
73
 
        return result
74
 
 
75
 
    def clean_firefox_all_records(self, filepath):
76
 
        if os.path.exists(filepath):
77
 
            clean_browser_conn = sqlite3.connect(filepath)
78
 
            clean_browser_cur = clean_browser_conn.cursor()
79
 
            sql_deletehistory = 'DELETE FROM moz_historyvisits'
80
 
            clean_browser_cur.execute(sql_deletehistory)
81
 
            clean_browser_conn.commit()
82
 
 
83
 
            sql_selectplace = 'SELECT place_id FROM moz_annos UNION SELECT fk FROM moz_bookmarks UNION SELECT place_id FROM moz_inputhistory'
84
 
            clean_browser_cur.execute(sql_selectplace)
85
 
            delete_place_id = clean_browser_cur.fetchall()
86
 
            delete_place_id_str = ','.join([ str(one[0]) for one in delete_place_id if one[0]])
87
 
            sql_deleteplace = 'DELETE FROM moz_places WHERE id NOT IN (%s)' % delete_place_id_str
88
 
            clean_browser_cur.execute(sql_deleteplace)
89
 
            clean_browser_conn.commit()
90
 
 
91
 
            sql_selectfavicons = 'SELECT favicon_id FROM moz_places'
92
 
            clean_browser_cur.execute(sql_selectfavicons)
93
 
            delete_favicon_id = clean_browser_cur.fetchall()
94
 
            delete_favicon_id_str = ','.join([ str(one[0]) for one in delete_favicon_id if one[0]])
95
 
            sql_deletefavicons = 'DELETE FROM moz_favicons WHERE id NOT in (%s)' %  delete_favicon_id_str
96
 
            clean_browser_cur.execute(sql_deletefavicons)
97
 
            clean_browser_conn.commit()
98
 
 
99
 
            clean_browser_cur.close()
100
 
            clean_browser_conn.close()
101
 
 
102
 
    def clean_chromium_all_records(self, filepath):
103
 
        if os.path.exists(filepath):
104
 
            clean_chromium_conn = sqlite3.connect(filepath)
105
 
            clean_chromium_cur = clean_chromium_conn.cursor()
106
 
            tables = ['visits', 'urls', 'keyword_search_terms', 'segment_usage', 'segments']
107
 
            for tn in tables:
108
 
                sql_delete = 'DELETE FROM %s' % tn
109
 
                clean_chromium_cur.execute(sql_delete)
110
 
            clean_chromium_conn.commit()
111
 
 
112
 
            clean_chromium_cur.close()
113
 
            clean_chromium_conn.close()
114
 
 
115
 
    def clean_the_records(self, history):
116
 
        int_history = int(history)
117
 
        sql_exist = 'SELECT * FROM moz_historyvisits WHERE place_id=%s' % int_history
118
 
        self.browser_cur.execute(sql_exist)
119
 
        if self.browser_cur.fetchone():
120
 
            sql_delete = 'DELETE FROM moz_historyvisits WHERE place_id=%s ' % int_history
121
 
            self.browser_cur.execute(sql_delete)
122
 
            #self.browser_cur.execute('DELETE FROM moz_places WHERE visit_count=0')
123
 
            self.browser_conn.commit()
124
 
            return True
125
 
        else:
126
 
            return False
127
 
 
128
 
    def clean_all_records(self):
129
 
        if os.path.exists(self.path):
130
 
            clean_browser_conn = sqlite3.connect(self.path + self.filename)
131
 
            clean_browser_cur = clean_browser_conn.cursor()
132
 
            sql_deleteall = 'DELETE FROM moz_historyvisits'
133
 
            clean_browser_cur.execute(sql_deleteall)
134
 
            #self.browser_cur.execute('DELETE FROM moz_places WHERE visit_count=0')
135
 
            clean_browser_conn.commit()
136
 
            clean_browser_cur.close()
137
 
            clean_browser_conn.close()
138
 
 
139
 
if __name__ == "__main__":
140
 
    objc = HistoryClean()
141
 
    objc.scan_the_records()
142
 
    #objc.clean_the_records(['36'])
143
 
    del objc