Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

can import smss from txt, next I will ascioate contact with sms

  • Loading branch information...
commit 8c2c37b060b9b2807fa92270930a9bd79729f866 1 parent 2108761
@alswl authored
View
1  .gitignore
@@ -28,3 +28,4 @@ pip-log.txt
#myself
data.db
+tags
View
6 model/__init__.py
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+#coding=utf-8
+
+from model.contact import *
+from model.sms import *
+from model.phone import *
View
15 model/contact.py
@@ -1,10 +1,13 @@
#!/usr/bin/env python
# coding=utf8
+from datetime import datetime
+
from sqlalchemy import Column, Integer, String, Unicode, TIMESTAMP
from sqlalchemy.orm import relationship, backref
from config import Base, session
+from model.phone import Phone
class Contact(Base):
"""contact model"""
@@ -12,9 +15,15 @@ class Contact(Base):
id = Column(Integer, primary_key=True)
name = Column(Unicode(50), nullable=False)
- phone = Column(String(20), nullable=False)
create_at = Column(TIMESTAMP)
- def __init__(self, name=None, phone=None):
+ phones = relationship('Phone', backref='contact')
+
+ def __init__(self, name=None):
self.name = name
- self.phone = phone
+ self.create_at = datetime.now()
+
+ def adjust_name(self):
+ # TODO update for first name / last name
+ self.name = self.name.replace(' ', '')
+ self.name = self.name.replace('!', '')
View
19 model/phone.py
@@ -1,14 +1,27 @@
#!/usr/bin/env python
# coding=utf8
-from sqlalchemy import Column, Integer, String, Unicode
+from datetime import datetime
-from meta import Base, Session
+from sqlalchemy import Column, Integer, String, Unicode, TIMESTAMP
+from sqlalchemy import ForeignKey
+from sqlalchemy.orm import relationship, backref
+
+from config import Base, Session
class Phone(Base):
"""contact model"""
__tablename__ = 'phone'
id = Column(Integer, primary_key=True)
- number = Column(String(50), nullable=False)
+ 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):
+ self.number = number
+ self.create_at = datetime.now()
+number_rule = r'\+?\d{11, 13}'
View
21 model/sms.py
@@ -12,6 +12,15 @@ class Sms(Base):
__tablename__ = 'sms'
id = Column(Integer, primary_key=True)
+ content = Column(String(500))
+ type = Column(Integer, nullable=False) # sms type: 1=inbox, 2=sendbox
+ create_at = Column(TIMESTAMP)
+ number = Column(String(20), nullable=False)
+ phone_id = Column(Integer, ForeignKey('phone.id'))
+ phone = relationship(
+ '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(
@@ -22,14 +31,14 @@ class Sms(Base):
'Contact',
primaryjoin='Contact.id == Sms.to_contact_id',
)
-
- content = Column(String(500))
- create_at = Column(TIMESTAMP)
def __init__(self, from_contact=None, to_contact=None):
self.from_contact = from_contact
self.to_contact = to_contact
- def add(self):
- session.add(self)
- session.commit()
+ def update_contact(self, name):
+ """update the number / contact info of sms"""
+ contact = Session.query(Contact).filter_by(name=name).first()
+ if contact == None:
+ contact = Contact(name)
+ phone = Session.query(Phone).filter_by(number=self.number).first()
View
72 parser/best_message_storer_parser.py
@@ -7,15 +7,30 @@
import logging
import re
+import datetime
from model.sms import Sms
-from parser import Parser, IllegalFormatError
+from model.contact import Contact
+from model.phone import Phone
+from parser import Parser, FormatError
+from config import Base, session
logger = logging.getLogger(__name__)
class Best_message_storer_parser(Parser):
- name_re = re.compile(ur'[发件人|收件人]: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n')
+ name_re = re.compile(
+ ur'发件人: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n' + '|' \
+ ur'收件人: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n'
+ )
+ phone_re = re.compile(
+ ur'发件人详情: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n' + '|' + \
+ ur'收件人详情: (\+?[\u2e80-\uffff|\s|\w|\d]+)\n'
+ )
type_re = re.compile(ur'文件夹: ([\u2e80-\uffff|\s]+)\n')
+ create_at_re = re.compile(
+ ur'日期/时间: (\d{4}\d{1,2}\d{1,2}日 /' \
+ + ' \d{1,2}:\d{1,2}:\d{1,2} [ap]m)'
+ )
def __init__(self, text):
self.text = text
@@ -25,26 +40,59 @@ def parse(self):
for block in self.text.split('======================================================================'):
try:
self.process_block(block)
- except IllegalFormatError, e:
+ except FormatError, e:
#logger.info('%s is not illegal format' %block) # FIXME
continue
return self.smses
def process_block(self, text):
- sms = Sms()
splits = text.split('----------------------------------------------------------------------')
if len(splits) != 2:
- raise IllegalFormatError()
+ raise FormatError()
meta = splits[0]
content = splits[1].strip()
-
type = self.type_re.search(meta).group(1)
- name = self.name_re.search(meta).group(1) # TODO
- logger.debug(name)
+ name = self.name_re.search(meta).group(1) or \
+ self.name_re.search(meta).group(2)
+
+ if name is None:
+ logger.error(meta)
+
+ create_at = self.create_at_re.search(meta).group(1)
if type == u'发出的信息':
- pass
+ number = self.phone_re.search(meta).group(1)
+ type_number = 2
elif type == u'收件箱':
- pass
+ number = self.phone_re.search(meta).group(1)
+ type_number = 1
else:
- raise IllegalFormatError()
- # self.smses.append(sms)
+ raise FormatError()
+ if number.find('+86') < 0: # fix number don't has +86
+ number = '+86' + number
+
+ contact_phone_items = session.query(Contact, Phone). \
+ filter(Contact.id == Phone.contact_id). \
+ filter(Phone.number == number).all()
+ if len(contact_phone_items) > 0:
+ contact, phone = contact_phone_items[0]
+ else:
+ contact = Contact(name=name)
+ phone = Phone(number)
+ phone.contact = contact
+ contact.adjust_name()
+ session.add(contact)
+ session.commit()
+
+ 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.number = number
+ sms.phone = phone
+ sms.content = content
+ sms.type = type_number
+ #sms.update_contact()
+ self.smses.append(sms)
View
2  parser/parser.py
@@ -14,7 +14,7 @@ def __init__(self):
def parse(self, text):
pass
-class IllegalFormatError(ValueError):
+class FormatError(ValueError):
"""非法格式"""
pass
View
1  smsir.py
@@ -59,6 +59,7 @@ def run(self):
parser.print_help()
def create_all(self):
+ self.Base.metadata.drop_all(checkfirst=True, bind=session.bind)
self.Base.metadata.create_all(self.engine)
def list_messages(self):
Please sign in to comment.
Something went wrong with that request. Please try again.