Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf8 -*-
- import pickle
- import os
- import twitter
- import sys
- import gdata
- import gdata.calendar.service
- import gdata.service
- import atom
- import time
- class twiTOs:
- home = os.path.expanduser('~')
- prog_conf = os.path.join(home, '.twiTOs', 'twiTOs.dat')
- api = twitter.Api(username='TWITTER_LOGIN', password='TWITTER_PASS')
- email = 'GMAIL_LOGIN'
- password = 'GMAIL_PASS'
- def load_data(self, spisok):
- # проверяем существование пути и файла с последним сообщением,
- # если что то не так - создаём новый
- if os.path.exists(os.path.join(self.home, '.twiTOs')) == True:
- try:
- fd = open(self.prog_conf, 'rb')
- last_message = pickle.load(fd)
- fd.close()
- except:
- fd = open(self.prog_conf, 'wb')
- last_message = spisok[0]
- pickle.dump(last_message, fd)
- fd.close()
- else:
- os.mkdir(os.path.join(self.home, '.twiTOs'))
- print "creating dir"
- self.load_data(spisok)
- self.compare(last_message, spisok)
- def compare(self, last_message, spisok):
- # Монструозная конструкция по сравнению последнего сообщения имеющегося у нас
- # и последней пятёрки из твиттера
- if last_message == spisok[0]:
- # ничего не изменилось - ничего не делаем
- delme = 3
- # тупая заглушка, чем заменить?
- else:
- for i in spisok:
- if i == last_message:
- # проверяем пятёрку сообщений на совпадение с имеющимся у нас, все сообщения до первого совпавшего смсим.
- # print i
- fd = open(self.prog_conf, 'wb')
- last_message = spisok[0]
- pickle.dump(last_message, fd)
- fd.close()
- break
- else:
- self.Cutter(self.email, self.password, i)
- else:
- fd = open(self.prog_conf, 'wb')
- last_message = spisok[0]
- pickle.dump(last_message, fd)
- fd.close()
- def receive_message(self, n):
- spisok = []
- statuses = self.api.GetFriendsTimeline(count=n)
- for s in statuses:
- uuser = s.user
- text = s.text
- message = uuser.screen_name + '|' + text
- spisok.append(message)
- return spisok
- def Cutter (self, email, password, text):
- # Резалка смсок, гугл отправляет только 57 символов, твиттер позволяет набрать 140 + имя пользователя
- # в 160 должны уложиться, не уложились - обрезаем. Иначе задолбают входящие смс.
- if len(text) > 57:
- text1 = '1/3' + text[0:54]
- text2 = '2/3' + text[54:108]
- if len(text) > 164:
- text3 = '3/3' + text[108:158] + 'BIGlen'
- else:
- text3 = '3/3' + text[108:164]
- full_text = [text1, text2, text3]
- for i in full_text:
- if len(i) > 3:
- self.SendSMS(email, password, i)
- elif len(text) > 3:
- self.SendSMS(email, password, text)
- def SendSMS(self, email, password, text):
- # Цельнотянутый код, криминала нет, описание у гугла в googleApi|calendar
- cal_client = gdata.calendar.service.CalendarService()
- cal_client.email = email
- cal_client.password = password
- cal_client.source = 'GCalendar notify'
- cal_client.ProgrammaticLogin()
- event = gdata.calendar.CalendarEventEntry()
- event.title = atom.Title(text=text)
- start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 30))
- event.when.append(gdata.calendar.When(start_time=start_time))
- reminder = gdata.calendar.Reminder(minutes='0')
- reminder.method = 'sms'
- event.when[0].reminder.append(reminder)
- cal_client.InsertEvent(event, '/calendar/feeds/default/private/full')
- def main():
- prog = twiTOs()
- spisok = prog.receive_message(5)
- prog.load_data(spisok)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement