Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
new version
  • Loading branch information
sleeperbus committed Feb 6, 2017
2 parents c3b33df + be0e92d commit 94e6a71
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 54 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
## 라이브러리
[Telepot Tutorial](http://telepot.readthedocs.io/en/latest/)을 보고 해당 라이브러리를 설치한다.

## 최근 변경사항
### v1.1(2017-02-06)
#### 기능 추가
* tvshow의 종료일을 설정할 수 있다. 명령어: /close_tvshow

#### 기능 변경
* tvshow 를 추가할 때 end_date 를 입력받지 않는다.
* /add_tvshow 보이스|6,0|2000|20170101|보이스

#### bug fix
* daily tv schedule 생성시 localtime 기준으로 row 를 생성한다.

## settings.json 설정
* token: 두 개의 토큰을 사용한다.
* torrent: 토렌트를 검색하고 다운받는 봇에서 사용한다.
Expand All @@ -20,7 +32,6 @@ python fileclassify.py

## 앞으로 개발할 기능
### 다운로드 봇
* 설정된 티비쇼 목록 확인 및 삭제
* 다운로드 대기 중인 티비쇼 에피소드 확인 및 삭제

### 분류 봇
Expand Down
100 changes: 59 additions & 41 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pprint import pprint
from random import randint
from datetime import datetime
import telepot
import telepot.helper
from telepot.delegate import (
Expand Down Expand Up @@ -38,13 +39,13 @@ def __init__(self, seed_tuple, search, db, server, **kwargs):
self.search = search
self.server = deluge()
self.db = db
self.torrents = []
self.items = []
self.mode = ''

# inline keyboards
self.edtTorrents = None
self.savedMsgIdentifier = None

self.kbdTorrents = InlineKeyboardMarkup(
self.inlineButtons = InlineKeyboardMarkup(
inline_keyboard=[]
)

Expand All @@ -59,14 +60,11 @@ def open(self, initial_msg, seed):
return
self.sender.sendMessage('Welcome back')

# Send inline buttons contain torrent title
def showTorrentsMenu(self, torrents):
l = [[InlineKeyboardButton(text=t['title'], callback_data=str(i))]
for i, t in enumerate(torrents) if t]
self.kbdTorrents = InlineKeyboardMarkup(inline_keyboard=l)
self.edtTorrents = self.sender.sendMessage('Choose ...',
reply_markup=self.kbdTorrents)

# user에게 선택 버튼을 보여준다.
def showItems(self, items):
l = [[InlineKeyboardButton(text=item['title'], callback_data=str(index))] for index, item in enumerate(items) if item]
self.inlineButtons = InlineKeyboardMarkup(inline_keyboard=l)
self.savedMsgIdentifier = self.sender.sendMessage('Choose ...', reply_markup=self.inlineButtons)

# Send torrent info string
def showTorrentsProgress(self):
Expand Down Expand Up @@ -111,17 +109,18 @@ def on_chat_message(self, msg):
elif msg['text'] == '/delete':
self.mode = 'delete'
# clear message identifier saved
if self.edtTorrents:
id = telepot.message_identifier(self.edtTorrents)
if self.savedMsgIdentifier:
id = telepot.message_identifier(self.savedMsgIdentifier)
self.bot.editMessageText(id, '...', reply_markup=None)

self.sender.sendMessage('Select torrent to delete ...')
self.torrents = self.ongoingList()
if len(self.torrents) == 0:
self.items = self.ongoingList()
if len(self.items) == 0:
self.sender.sendMessage('There is no downloading files.')
self.edtTorrents = None
self.savedMsgIdentifier = None
else:
self.showTorrentsMenu(self.torrents)
self.showItems(self.items)
# self.showTorrentsMenu(self.torrents)

# command - reboot
elif msg['text'] == '/reboot':
Expand All @@ -134,29 +133,44 @@ def on_chat_message(self, msg):

# command - new tv schedule
elif '/new_tvshow' in msg['text']:
name, weekday, time, start_date, end_date, keyword = msg['text'].split(' ')[1].split('|')
name, weekday, time, start_date, keyword = msg['text'].split(' ')[1].split('|')
keyword = keyword.replace(',', ' ')
for day in weekday.split(','):
for res in ['720', '1080']:
self.db.createTvShow(name, day, time, start_date, end_date, keyword+' '+res, self.chat_id)
self.db.createTvShow(name, day, time, start_date, keyword+' '+res, self.chat_id)

# 설정된 tvshow 목록을 확인한다.
elif '/close_tvshow' in msg['text']:
self.mode = 'close_tvshow'
if self.savedMsgIdentifier:
id = telepot.message_identifier(self.savedMsgIdentifier)
self.bot.editMessageText(id, '...', reply_markup=None)
self.sender.sendMessage('오늘부터 스케줄링을 하지 않을 티비쇼를 고르세요.')
self.logger.debug('chat_id is %d' % self.chat_id)
self.items = self.db.tvShowList(self.chat_id)
if len(self.items) == 0:
self.sender.sendMessage('활성화된 티비쇼가 없습니다.')
self.savedMsgIdentifier = None
else:
self.showItems(self.items)

# search torrents file using self.search function
else:
self.mode = 'search'
if self.edtTorrents:
id = telepot.message_identifier(self.edtTorrents)
if self.savedMsgIdentifier:
id = telepot.message_identifier(self.savedMsgIdentifier)
self.bot.editMessageText(id, '...', reply_markup=None)

self.sender.sendMessage('searching ...')
self.logger.debug('user try to search keyword: %s' % msg['text'])
self.torrents = self.search(unicode(msg['text']))
self.items = self.search(unicode(msg['text']))

if not len(self.torrents):
if not len(self.items):
self.sender.sendMessage('There is no files searched.')
self.logger.debug('can not find any torrent ...')
self.edtTorrents = None
self.savedMsgIdentifier = None
else:
self.showTorrentsMenu(self.torrents)
self.showItems(self.items)

else: self.sender.sendMessage('You can only send text message.')

Expand All @@ -165,24 +179,28 @@ def on_callback_query(self, msg):
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
self.logger.debug('in callback')

if not self.edtTorrents:
if not self.savedMsgIdentifier:
self.bot.answerCallbackQuery(query_id,
text='Overdue list, please search again')
return

id = telepot.message_identifier(self.edtTorrents)
torrent = self.torrents[int(data)]
id = telepot.message_identifier(self.savedMsgIdentifier)

if self.mode == 'search':
self.bot.editMessageText(id, 'Adding, %s' %
self.torrents[int(data)]['title'], reply_markup=None)
torrent = self.items[int(data)]
self.bot.editMessageText(id, 'Adding, %s' % torrent['title'], reply_markup=None)
self.addTorrent(torrent['magnet'], self.chat_id)

elif self.mode == 'delete':
self.bot.editMessageText(id, 'Deleting, %s' %
self.torrents[int(data)]['title'], reply_markup=None)
torrent = self.items[int(data)]
self.bot.editMessageText(id, 'Deleting, %s' % torrent['title'], reply_markup=None)
self.deleteTorrent(torrent['id'])

elif self.mode == 'close_tvshow':
tvshow = self.items[int(data)]
self.bot.editMessageText(id, '오늘부터 [%s]를 스케줄링 하지 않습니다.' % tvshow['title'], reply_markup=None)
self.db.unsubscribeTvShow(tvshow['program_id'], datetime.today().strftime('%Y%m%d'))

# add torrent magnet to torrent server and db server
def addTorrent(self, magnet, chat_id):
torrentInfo = self.server.add(magnet)
Expand Down Expand Up @@ -211,35 +229,35 @@ def __init__(self, seed_tuple, search, server, db):
self.sched.start()
self.sched.add_job(self.torrentMonitor, 'interval', minutes=3)
self.sched.add_job(self.keepAliveTorrentServer, 'interval', minutes=10)
self.sched.add_job(self.downloadTvShow, 'interval', minutes=120)
self.sched.add_job(self.downloadTvEpisodes, 'interval', minutes=120)
self.sched.add_job(self.createDailyTvSchedule, 'cron', hour=1, minute=0)

self.createDailyTvSchedule()
self.downloadTvShow()
# self.createDailyTvSchedule()
# self.downloadTvEpisodes()

self.logger.debug('JobMonitor logger init ...')

def createDailyTvSchedule(self):
self.db.createDailySchedule()
self.logger.debug('daily schedule created')

def downloadTvShow(self):
self.logger.debug('downloadTvShow started')
def downloadTvEpisodes(self):
self.logger.debug('downloadTvEpisodes started')
# 완료되지 않은 스케줄을 가져온다.
schedule = self.db.uncompletedSchedule()
schedule = self.db.uncompletedTvEpisode()
for episode in schedule:
torrents = self.search(unicode(episode['keyword']))
if not len(torrents):
self.logger.info('can not find tvshow ' + str(episode))
self.db.increaseTvShowCount(episode['program_id'], episode['download_date'])
self.db.increaseTvEpisodeCount(episode['program_id'], episode['download_date'])
else:
# torrent 를 발견한다면 deluge 에 magnet 을 던진다.
torrentInfo = self.server.add(torrents[0]['magnet'])
torrentInfo['chat_id'] = episode['chat_id']
self.logger.info('new tvshow added ' + str(episode))
if torrentInfo:
# 스케줄을 완료처리하고 사용자에게 db 에도 토렌트 정보를 입력한다.
self.db.completedTvSchedule(episode['program_id'], episode['download_date'])
# 스케줄을 완료처리하고 db 토렌트 정보를 입력한다.
self.db.completeTvEpisode(episode['program_id'], episode['download_date'])
torrentInfo['chat_id'] = episode['chat_id']
self.db.addTorrent(torrentInfo)

Expand Down
Loading

0 comments on commit 94e6a71

Please sign in to comment.