Skip to content
Browse files

best message storer type sms can input successful

  • Loading branch information...
1 parent 8c2c37b commit f6a44fa2caa518952d6b073a1555ea0ed446881f @alswl committed
Showing with 57 additions and 32 deletions.
  1. +1 −1 .gitignore
  2. +0 −2 model/phone.py
  3. +2 −10 model/sms.py
  4. +38 −12 parser/best_message_storer_parser.py
  5. +7 −1 parser/parser.py
  6. +9 −6 smsir.py
View
2 .gitignore
@@ -27,5 +27,5 @@ pip-log.txt
.mr.developer.cfg
#myself
-data.db
+data/data.db
tags
View
2 model/phone.py
@@ -16,8 +16,6 @@ class Phone(Base):
id = Column(Integer, primary_key=True)
number = Column(String(20), nullable=False)
contact_id = Column(Integer, ForeignKey('contact.id'))
- #contact = relationship('Contact',
- #primaryjoin='Contact.id == Phone.contact_id')
create_at = Column(TIMESTAMP)
def __init__(self, number=None):
View
12 model/sms.py
@@ -21,16 +21,8 @@ class Sms(Base):
'Phone',
primaryjoin='Phone.id == Sms.phone_id',
)
- from_contact_id = Column(Integer, ForeignKey('contact.id'))
- to_contact_id = Column(Integer, ForeignKey('contact.id'))
- from_contact = relationship(
- 'Contact',
- primaryjoin='Contact.id == Sms.from_contact_id',
- )
- to_contact = relationship(
- 'Contact',
- primaryjoin='Contact.id == Sms.to_contact_id',
- )
+ contact_id = Column(Integer, ForeignKey('contact.id'))
+ contact = relationship('Contact', backref='sms')
def __init__(self, from_contact=None, to_contact=None):
self.from_contact = from_contact
View
50 parser/best_message_storer_parser.py
@@ -12,7 +12,7 @@
from model.sms import Sms
from model.contact import Contact
from model.phone import Phone
-from parser import Parser, FormatError
+from parser import Parser, FormatError, DuplicateError
from config import Base, session
logger = logging.getLogger(__name__)
@@ -22,7 +22,7 @@ class Best_message_storer_parser(Parser):
ur'发件人: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n' + '|' \
ur'收件人: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n'
)
- phone_re = re.compile(
+ number_re = re.compile(
ur'发件人详情: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n' + '|' + \
ur'收件人详情: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n'
)
@@ -33,6 +33,7 @@ class Best_message_storer_parser(Parser):
)
def __init__(self, text):
+ super(Best_message_storer_parser, self).__init__()
self.text = text
self.smses = []
@@ -40,17 +41,36 @@ def parse(self):
for block in self.text.split('======================================================================'):
try:
self.process_block(block)
+ self.success_count += 1
except FormatError, e:
#logger.info('%s is not illegal format' %block) # FIXME
+ self.format_error_count += 1
+ self.fail_count +=1
+ continue
+ except DuplicateError, e:
+ self.duplicate_error_count +=1
+ self.fail_count +=1
continue
return self.smses
+ def print_result(self):
+ print '# Success:\t%d' %self.success_count
+ print '# Failed:\t%d' %self.fail_count
+ print '# duplicate error\t%d' %self.duplicate_error_count
+ print '# format error\t%d' %self.format_error_count
+
def process_block(self, text):
splits = text.split('----------------------------------------------------------------------')
if len(splits) != 2:
raise FormatError()
meta = splits[0]
content = splits[1].strip()
+ if self.type_re.search(meta) is None or \
+ self.name_re.search(meta) is None or \
+ self.number_re.search(meta) is None or \
+ self.create_at_re.search(meta) is None:
+ raise FormatError()
+
type = self.type_re.search(meta).group(1)
name = self.name_re.search(meta).group(1) or \
self.name_re.search(meta).group(2)
@@ -58,12 +78,18 @@ def process_block(self, text):
if name is None:
logger.error(meta)
- create_at = self.create_at_re.search(meta).group(1)
+ create_at_text = self.create_at_re.search(meta).group(1)
+ create_at = datetime.datetime.strptime(
+ create_at_text.replace(' am', '').replace(' pm', '').encode('utf-8'),
+ u'%Y年%m月%d日 / %I:%M:%S'.encode('utf-8')
+ )
+ if create_at_text.find('pm') > 0:
+ create_at += datetime.timedelta(hours=12)
if type == u'发出的信息':
- number = self.phone_re.search(meta).group(1)
+ number = self.number_re.search(meta).group(1)
type_number = 2
elif type == u'收件箱':
- number = self.phone_re.search(meta).group(1)
+ number = self.number_re.search(meta).group(1)
type_number = 1
else:
raise FormatError()
@@ -83,16 +109,16 @@ def process_block(self, text):
session.add(contact)
session.commit()
+ sms = session.query(Sms).filter_by(number=number,
+ create_at=create_at).first()
+ if not sms is None: # 重复的短信
+ raise DuplicateError
+
sms = Sms()
- sms.create_at = datetime.datetime.strptime(
- create_at.replace(' am', '').replace(' pm', '').encode('utf-8'),
- u'%Y年%m月%d日 / %I:%M:%S'.encode('utf-8')
- )
- if create_at.find('pm') > 0:
- sms.create_at += datetime.timedelta(hours=12)
+ sms.create_at = create_at
sms.number = number
sms.phone = phone
sms.content = content
sms.type = type_number
- #sms.update_contact()
+ sms.contact = contact
self.smses.append(sms)
View
8 parser/parser.py
@@ -9,7 +9,10 @@
class Parser(object):
def __init__(self):
- pass
+ self.success_count = 0
+ self.fail_count = 0
+ self.format_error_count = 0
+ self.duplicate_error_count = 0
def parse(self, text):
pass
@@ -18,3 +21,6 @@ class FormatError(ValueError):
"""非法格式"""
pass
+class DuplicateError(ValueError):
+ """重复内容"""
+ pass
View
15 smsir.py
@@ -39,6 +39,7 @@ def run(self):
parser.add_argument('--import-best-message-storer', '-B',
type=argparse.FileType('r'),
metavar='FILE',
+ nargs='+',
help='import messages from Best MessageStorer backups')
args = parser.parse_args()
@@ -49,12 +50,14 @@ def run(self):
elif args.import_sms_backup_restore != None:
pass
elif args.import_best_message_storer != None:
- best_message_storer_parser = Best_message_storer_parser(
- args.import_best_message_storer.read().decode('utf-16le')
- )
- smses = best_message_storer_parser.parse()
- session.add_all(smses)
- session.commit()
+ for text in args.import_best_message_storer:
+ best_message_storer_parser = Best_message_storer_parser(
+ text.read().decode('utf-16le')
+ )
+ smses = best_message_storer_parser.parse()
+ session.add_all(smses)
+ session.commit()
+ best_message_storer_parser.print_result()
else:
parser.print_help()

0 comments on commit f6a44fa

Please sign in to comment.
Something went wrong with that request. Please try again.