-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entities.py
156 lines (122 loc) · 3.9 KB
/
Entities.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
###
# Created :Tue Jul 12 09:22:42 UTC 2011
# Modified :
###
import datetime
import re
import os
from pikaurdlib.util import decodeXML
from pikaurdlib.util import str2Time
from pikaurdlib.util import time2Str
from pikaurdlib.util import CacheIsEmptyError
dateFormat = '%Y-%m-%d %H:%M:%S'
class FeedItem:
def __init__(self, title, pubDate, link='', description='', fromId=0):
self.fromFeedId = fromId
self.title = decodeXML(title.strip())
self.pubDate = time2Str(str2Time(pubDate))
self.link = link
self.description = description
def insertStmt(self):
sql = 'insert into feeds (from_feed_id, title, link, time) values({!r},{!r},{!r},{!r})'
return sql.format(self.fromFeedId, self.title, self.pubDate, self.link)
# def _formalDate(self, pubDate):
def __eq__(self, x):
return self.title == x.title
def __str__(self):
return 'Title: {}\tPubDate: {}\tLink: {}'.format(self.title, self.pubDate, self.link)
def isUpdated(self, oldTime):
return oldTime < str2Time(self.pubDate, dateFormat)
def isNew(self):
return Cache.isNotExist(self)
class Tags:
def __init__(self, title, id=0):
self.title = title
self.id = id
class FeedResUpdateTime:
cache = {}
DATE = datetime.datetime(2011, 1, 1, 12, 0, 0)
def __init__(self, feedId, updateTime):
FeedResUpdateTime.cache[feedId] = str2Time(updateTime, dateFormat)
@staticmethod
def get(feedId):
updateTime = FeedResUpdateTime.cache.get(feedId)
if updateTime == None:
updateTime = FeedResUpdateTime.DATE
# updateTime = '2011-01-01 12:00:00'
return updateTime
class FeedRes:
def __init__(self, url, tag, description, id, pubDate=''):
self.url = url
self.tag = tag
self.desc = description
self.id = id
if type(pubDate) is str:
pubDate = str2Time(pubDate)
self.pubDate = pubDate
def __str__(self):
return 'ID: {}\turl: {}\t PubDate: {}'.format(self.id, self.url, self.pubDate)
def isUpdated(self, curTime):
return curTime > self.pubDate
class Cache:
_cache = {}
@staticmethod
def update(feed):
Cache._cache[feed.title] = True
@staticmethod
def isExist(feed):
if Cache.size() == 0:
raise CacheIsEmptyError('Cache is empty')
return Cache._cache.get(feed.title) != None
@staticmethod
def size():
return len(Cache._cache)
@staticmethod
def clear():
Cache._cache = {}
@staticmethod
def isEmpty():
return Cache.size() == 0
class FilterReason:
def __init__(self, content, type, createTime, id=0):
self.content = content
self.type = type
self.createTime = createTime
self.id = id
def isMustFilter(self):
return self.type == 1 or self.type == 4
# Mock
class MockFeedItem(FeedItem):
def __init__(self,title='', pubDate='Tue, 12 Jul 2011 22:23:26 +0800', link='',fromId=''):
FeedItem.__init__(self, title, pubDate, link, fromId)
# UnitTest
import unittest
class TestFeedResUpdateTime(unittest.TestCase):
def setUp(self):
FeedResUpdateTime(0, '2011-01-01 12:01:01')
def test_get(self):
self.assertEqual(datetime.datetime(2011,1,1,12,1,1), FeedResUpdateTime.get(0))
self.assertEqual(datetime.datetime(2011, 1, 1, 12, 0, 0), FeedResUpdateTime.get(1))
class TestCache(unittest.TestCase):
def test_updateCache(self):
Cache.clear()
self.assertEqual(0, Cache.size())
Cache.update(MockFeedItem('koi'))
self.assertEqual(1, Cache.size())
def test_isExist(self):
Cache.clear()
Cache.update(MockFeedItem('koi'))
self.assertTrue(Cache.isExist(MockFeedItem('koi')))
self.assertFalse(Cache.isExist(MockFeedItem('daisuki')))
def test_clear(self):
Cache.update(MockFeedItem('koi'))
self.assertLess(0, Cache.size())
Cache.clear()
self.assertEqual(0, Cache.size())
@unittest.expectedFailure
def test_isExistFailure(self):
Cache.isExist('')
if __name__ == '__main__':
unittest.main()