Skip to content

Instantly share code, notes, and snippets.

@beer2011
Created May 28, 2015 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beer2011/c56da4dda98ca3a2c47e to your computer and use it in GitHub Desktop.
Save beer2011/c56da4dda98ca3a2c47e to your computer and use it in GitHub Desktop.
#coding: utf-8
# RSS Reader
# GitHub dlo/PythonistaRSSReader
# original -- dlo on Jun 22, 2014 Update
#
# feedリスト削除機能追加 -- beer2011 2015-5-13
# 課題 -- feedリストを削除した後の表示で、削除したものが表示されてしまう。
# アプリの起動しなおしが必要なこと。
# -- feedリストの順序を変えることができない
#
import ui
import sqlite3
import console
import feedparser
import clipboard
import urlparse
from dateutil.parser import parse as parse_date
class FeedListController(object):
def __init__(self, feeds=[]):
self.feeds = feeds
def tableview_did_select(self, tableview, section, row):
console.show_activity()
feed = feedparser.parse(self.feeds[row]['url'])
rss_controller = RSSController(feed)
tv = ui.TableView()
tv.name = self.feeds[row]['title']
tv.allows_multiple_selection_during_editing = True
tv.data_source = rss_controller
tv.delegate = rss_controller
table_view.navigation_view.push_view(tv)
console.hide_activity()
def tableview_number_of_sections(self, tableview):
return 1
def tableview_number_of_rows(self, tableview, section):
return len(self.feeds)
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = self.feeds[row]['title']
return cell
class RSSController(object):
def __init__(self, feed):
index = 0
self.feed = feed
self.dates = {}
self.date_indices = []
if feed is None:
return
for entry in self.feed['entries']:
dt = parse_date(entry['published'])
if dt.date() in self.dates:
self.dates[dt.date()].append(entry)
else:
self.dates[dt.date()] = [entry]
self.date_indices.append(dt.date())
index += 1
def tableview_did_select(self, tableview, section, row):
entry = self.dates[self.date_indices[section]][row]
webview = ui.WebView()
webview.name = entry['title']
webview.load_url(entry['link'])
tableview.navigation_view.push_view(webview)
def tableview_number_of_sections(self, tableview):
return len(self.dates)
def tableview_number_of_rows(self, tableview, section):
return len(self.dates[self.date_indices[section]])
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.text_label.text = self.dates[self.date_indices[section]][row]['title']
return cell
def tableview_title_for_header(self, tableview, section):
return str(self.date_indices[section])
class FeedOperate(object):
def __init__(self):
#---削除用TableView--------
self.tv = ui.TableView()
self.feed = ui.ListDataSource(feeds)
def refresh(self):
conn = sqlite3.connect('feeds.db')
feeds = []
for title, url in conn.execute('SELECT * FROM feeds ORDER BY title'):
feeds.append({'title': title, 'url': url })
conn.close()
self.feed = ui.ListDataSource(feeds)
def refresh2(self):
conn = sqlite3.connect('feeds.db')
feeds = []
for title, url in conn.execute('SELECT * FROM feeds ORDER BY title'):
feeds.append({'title': title, 'url': url })
conn.close()
feed_list_controller.feeds = feeds
table_view.reload()
@ui.in_background
def add_feed(self, sender):
url = console.input_alert('', "Enter RSS feed URL:", 'http://www.macstories.net/feed/')
result = urlparse.urlparse(url)
if result.netloc == '':
url = 'http://www.macstories.net/feed/'
indicator = ui.ActivityIndicator()
indicator.center = navigation_view.center
navigation_view.add_subview(indicator)
indicator.bring_to_front()
indicator.start()
feed = feedparser.parse(url)
title = feed['feed']['title']
conn = sqlite3.connect('feeds.db')
conn.execute('INSERT INTO feeds VALUES (?, ?)', (title, url))
conn.commit()
feeds = []
for title, url in conn.execute('SELECT * FROM feeds ORDER BY title'):
feeds.append({'title': title, 'url': url })
conn.close()
feed_list_controller.feeds = feeds
table_view.reload()
indicator.stop()
navigation_view.remove_subview(indicator)
#-----
@ui.in_background
def delete_feed(self, sender):
self.refresh()
self.tv.name = ''
self.tv.allows_multiple_selection_during_editing = True
self.tv.data_source = self.tv.delegate = self.feed
add_button_item = ui.ButtonItem('削除', None, self.delete_exec)
self.nv = ui.NavigationView(self.tv)
self.nv.right_button_items = [add_button_item]
self.nv.name = 'Delete Lists'
self.nv.present('sheet')
#-----
@ui.in_background
def delete_exec(self, sender):
ans = console.alert('title', '選択項目を削除しますか?', 'OK')
if ans == 1:
del_feed = self.feed.items[self.tv.selected_row[1]]['url']
print 'delete', del_feed
conn = sqlite3.connect('feeds.db')
conn.execute('DELETE FROM feeds WHERE url = (?)', [del_feed])
#conn.execute('SELECT * FROM feeds')
conn.commit()
conn.close()
console.alert('title', '削除しました', 'OK', hide_cancel_button=True )
self.nv.close()
#フリーズする
#self.refresh2()
else:
console.alert('削除を中止しました', 'OK', hide_cansel_button=True )
#self.tv.close()
#feed_list_controller = FeedListController(feeds)
@ui.in_background
def tool_alert(sender):
alert_result = console.alert('Feed Lists', 'フィードリストを追加しますか?、それとも、削除しますか?', '追加', '削除')
if alert_result == 1:
feed_op.add_feed(sender)
elif alert_result == 2:
feed_op.delete_feed(sender)
#-------------------
# main routine
#-------------------
conn = sqlite3.connect('feeds.db')
cursor = conn.execute('pragma user_version')
version = cursor.fetchone()[0]
if version == 0:
conn.execute('BEGIN TRANSACTION')
conn.execute('PRAGMA USER_VERSION=1')
conn.execute('CREATE TABLE feeds (title TEXT UNIQUE, url TEXT UNIQUE)')
conn.commit()
feeds = []
for title, url in conn.execute('SELECT * FROM feeds ORDER BY title'):
feeds.append({'title': title, 'url': url })
conn.close()
feed_list_controller = FeedListController(feeds)
feed_op = FeedOperate()
add_button_item = ui.ButtonItem('編集', None, tool_alert)
table_view = ui.TableView()
table_view.name = 'Feeds'
table_view.allows_multiple_selection_during_editing = True
table_view.data_source = feed_list_controller
table_view.delegate = feed_list_controller
navigation_view = ui.NavigationView(table_view)
navigation_view.right_button_items = [add_button_item]
navigation_view.name = 'RSS Reader'
navigation_view.present('fullscreen')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment