Skip to content

Commit 6787fbc

Browse files
committed
Remove old api.py module
1 parent 173d074 commit 6787fbc

File tree

2 files changed

+96
-334
lines changed

2 files changed

+96
-334
lines changed

bin/gpo

Lines changed: 96 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ is_single_command = False
128128
from gpodder import log
129129
log.setup(verbose)
130130

131-
from gpodder import api
131+
from gpodder import core
132+
from gpodder import download
132133
from gpodder import my
133134
from gpodder import opml
134135
from gpodder import util
135136
from gpodder.config import config_value_to_string
136137

137138
def incolor(color_id, s):
138-
if have_ansi and cli.config.ui.cli.colors:
139+
if have_ansi and cli._config.ui.cli.colors:
139140
return '\033[9%dm%s\033[0m' % (color_id, s)
140141
return s
141142

@@ -196,8 +197,10 @@ class gPodderCli(object):
196197
EXIT_COMMANDS = ('quit', 'exit', 'bye')
197198

198199
def __init__(self):
199-
self.client = api.PodcastClient()
200-
self.config = self.client._config
200+
self.core = core.Core()
201+
self._db = self.core.db
202+
self._config = self.core.config
203+
self._model = self.core.model
201204

202205
self._current_action = ''
203206
self._commands = dict((name.rstrip('_'), func)
@@ -206,7 +209,7 @@ class gPodderCli(object):
206209
self._prefixes, self._expansions = self._build_prefixes_expansions()
207210
self._prefixes.update({'?': 'help'})
208211
self._valid_commands = sorted(self._prefixes.values())
209-
gpodder.user_extensions.on_ui_initialized(self.client.core.model,
212+
gpodder.user_extensions.on_ui_initialized(self.core.model,
210213
self._extensions_podcast_update_cb,
211214
self._extensions_episode_download_cb)
212215

@@ -276,7 +279,7 @@ class gPodderCli(object):
276279
self._current_action = ''
277280

278281
def _atexit(self):
279-
self.client.finish()
282+
self.core.shutdown()
280283

281284
# -------------------------------------------------------------------
282285

@@ -285,39 +288,54 @@ class gPodderCli(object):
285288
self.subscribe(channel['url'], channel.get('title'))
286289

287290
def export(self, filename):
288-
podcasts = self.client._model.get_podcasts()
291+
podcasts = self._model.get_podcasts()
289292
opml.Exporter(filename).write(podcasts)
290293

291-
def subscribe(self, url, title=None):
294+
def get_podcast(self, url, create=False):
295+
"""Get a specific podcast by URL
296+
297+
Returns a podcast object for the URL or None if
298+
the podcast has not been subscribed to.
299+
"""
292300
url = util.normalize_feed_url(url)
293301
if url is None:
294-
self._error(_('Invalid URL.'))
295-
return True
296-
297-
if self.client.get_podcast(url) is not None:
298-
self._info(_('You are already subscribed to %s.') % url)
299-
return True
302+
self._error(_('Invalid url: %s') % url)
303+
return None
304+
305+
podcast = self._model.load_podcast(url, create=create, \
306+
max_episodes=self._config.max_episodes_per_feed)
307+
if podcast is None:
308+
self._error(_('You are not subscribed to %s.') % url)
309+
return None
310+
311+
return podcast
300312

313+
def subscribe(self, url, title=None):
301314
try:
302-
if self.client.create_podcast(url, title) is None:
315+
podcast = self.get_podcast(url, create=True)
316+
if podcast is None:
303317
self._error(_('Cannot subscribe to %s.') % url)
304318
return True
319+
320+
if title is not None:
321+
podcast.rename(title)
322+
podcast.save()
305323
except Exception, e:
306324
if hasattr(e, 'strerror'):
307325
self._error(e.strerror)
308326
else:
309327
self._error(str(e))
310328
return True
311329

312-
self.client.commit()
330+
self._db.commit()
313331

314332
self._info(_('Successfully added %s.' % url))
315333
return True
316334

317335
def _print_config(self, search_for):
318-
for key in self.config.all_keys():
336+
for key in self._config.all_keys():
319337
if search_for is None or search_for.lower() in key.lower():
320-
value = config_value_to_string(self.config._lookup(key))
338+
value = config_value_to_string(self._config._lookup(key))
321339
safe_print(key, '=', value)
322340

323341
def set(self, key=None, value=None):
@@ -326,7 +344,7 @@ class gPodderCli(object):
326344
return
327345

328346
try:
329-
current_value = self.config._lookup(key)
347+
current_value = self._config._lookup(key)
330348
current_type = type(current_value)
331349
except KeyError:
332350
self._error(_('This configuration option does not exist.'))
@@ -336,19 +354,17 @@ class gPodderCli(object):
336354
self._error(_('Can only set leaf configuration nodes.'))
337355
return
338356

339-
self.config.update_field(key, value)
357+
self._config.update_field(key, value)
340358
self.set(key)
341359

342360
@FirstArgumentIsPodcastURL
343361
def rename(self, url, title):
344-
podcast = self.client.get_podcast(url)
362+
podcast = self.get_podcast(url)
345363

346-
if podcast is None:
347-
self._error(_('You are not subscribed to %s.') % url)
348-
else:
364+
if podcast is not None:
349365
old_title = podcast.title
350366
podcast.rename(title)
351-
self.client.commit()
367+
self._db.commit()
352368
self._info(_('Renamed %(old_title)s to %(new_title)s.') % {
353369
'old_title': util.convert_bytes(old_title),
354370
'new_title': util.convert_bytes(title),
@@ -358,40 +374,52 @@ class gPodderCli(object):
358374

359375
@FirstArgumentIsPodcastURL
360376
def unsubscribe(self, url):
361-
podcast = self.client.get_podcast(url)
377+
podcast = self.get_podcast(url)
362378

363379
if podcast is None:
364380
self._error(_('You are not subscribed to %s.') % url)
365381
else:
366382
podcast.delete()
367-
self.client.commit()
383+
self._db.commit()
368384
self._error(_('Unsubscribed from %s.') % url)
369385

370386
return True
387+
388+
def is_episode_new(self, episode):
389+
return (episode.state == gpodder.STATE_NORMAL and episode.is_new)
371390

372391
def _episodesList(self, podcast):
373392
def status_str(episode):
374-
if episode.is_new:
393+
# is new
394+
if self.is_episode_new(episode):
375395
return u' * '
376-
if episode.is_downloaded:
396+
# is downloaded
397+
if (episode.state == gpodder.STATE_DOWNLOADED):
377398
return u' ▉ '
378-
if episode.is_deleted:
399+
# is deleted
400+
if (episode.state == gpodder.STATE_DELETED):
379401
return u' ░ '
380402

381403
return u' '
382404

383405
episodes = (u'%3d. %s %s' % (i+1, status_str(e), e.title)
384-
for i, e in enumerate(podcast.get_episodes()))
406+
for i, e in enumerate(podcast.get_all_episodes()))
385407
return episodes
386408

387409
@FirstArgumentIsPodcastURL
388410
def info(self, url):
389-
podcast = self.client.get_podcast(url)
411+
podcast = self.get_podcast(url)
390412

391413
if podcast is None:
392414
self._error(_('You are not subscribed to %s.') % url)
393415
else:
394-
title, url, status = podcast.title, podcast.url, podcast.feed_update_status_msg()
416+
def feed_update_status_msg(podcast):
417+
if podcast.pause_subscription:
418+
return "disabled"
419+
return "enabled"
420+
421+
title, url, status = podcast.title, podcast.url, \
422+
feed_update_status_msg(podcast)
395423
episodes = self._episodesList(podcast)
396424
episodes = u'\n '.join(episodes)
397425
self._pager(u"""
@@ -408,7 +436,7 @@ class gPodderCli(object):
408436
@FirstArgumentIsPodcastURL
409437
def episodes(self, url=None):
410438
output = []
411-
for podcast in self.client.get_podcasts():
439+
for podcast in self._model.get_podcasts():
412440
podcast_printed = False
413441
if url is None or podcast.url == url:
414442
episodes = self._episodesList(podcast)
@@ -422,8 +450,8 @@ class gPodderCli(object):
422450
return True
423451

424452
def list(self):
425-
for podcast in self.client.get_podcasts():
426-
if podcast.update_enabled():
453+
for podcast in self._model.get_podcasts():
454+
if not podcast.pause_subscription:
427455
safe_print('#', ingreen(podcast.title))
428456
else:
429457
safe_print('#', inred(podcast.title),
@@ -449,13 +477,13 @@ class gPodderCli(object):
449477
def update(self, url=None):
450478
count = 0
451479
safe_print(_('Checking for new episodes'))
452-
for podcast in self.client.get_podcasts():
480+
for podcast in self._model.get_podcasts():
453481
if url is not None and podcast.url != url:
454482
continue
455483

456-
if podcast.update_enabled():
484+
if not podcast.pause_subscription:
457485
self._update_podcast(podcast)
458-
count += sum(1 for e in podcast.get_episodes() if e.is_new)
486+
count += sum(1 for e in podcast.get_all_episodes() if self.is_episode_new(e))
459487
else:
460488
self._start_action(_('Skipping %(podcast)s') % {
461489
'podcast': podcast.title})
@@ -467,11 +495,11 @@ class gPodderCli(object):
467495
@FirstArgumentIsPodcastURL
468496
def pending(self, url=None):
469497
count = 0
470-
for podcast in self.client.get_podcasts():
498+
for podcast in self._model.get_podcasts():
471499
podcast_printed = False
472500
if url is None or podcast.url == url:
473-
for episode in podcast.get_episodes():
474-
if episode.is_new:
501+
for episode in podcast.get_all_episodes():
502+
if self.is_episode_new(episode):
475503
if not podcast_printed:
476504
safe_print('#', ingreen(podcast.title))
477505
podcast_printed = True
@@ -483,17 +511,22 @@ class gPodderCli(object):
483511

484512
def _download_episode(self, episode):
485513
self._start_action('Downloading %s', episode.title)
486-
episode.download(self._update_action)
514+
515+
task = download.DownloadTask(episode, self._config)
516+
task.add_progress_callback(self._update_action)
517+
task.status = download.DownloadTask.QUEUED
518+
task.run()
519+
487520
self._finish_action()
488521

489522
@FirstArgumentIsPodcastURL
490523
def download(self, url=None):
491524
count = 0
492-
for podcast in self.client.get_podcasts():
525+
for podcast in self._model.get_podcasts():
493526
podcast_printed = False
494527
if url is None or podcast.url == url:
495-
for episode in podcast.get_episodes():
496-
if episode.is_new:
528+
for episode in podcast.get_all_episodes():
529+
if self.is_episode_new(episode):
497530
if not podcast_printed:
498531
safe_print(inblue(podcast.title))
499532
podcast_printed = True
@@ -505,33 +538,39 @@ class gPodderCli(object):
505538

506539
@FirstArgumentIsPodcastURL
507540
def disable(self, url):
508-
podcast = self.client.get_podcast(url)
541+
podcast = self.get_podcast(url)
509542

510543
if podcast is None:
511544
self._error(_('You are not subscribed to %s.') % url)
512545
else:
513-
podcast.disable()
514-
self.client.commit()
546+
if not podcast.pause_subscription:
547+
podcast.pause_subscription = True
548+
podcast.save()
549+
self._db.commit()
515550
self._error(_('Disabling feed update from %s.') % url)
516551

517552
return True
518553

519554
@FirstArgumentIsPodcastURL
520555
def enable(self, url):
521-
podcast = self.client.get_podcast(url)
556+
podcast = self.get_podcast(url)
522557

523558
if podcast is None:
524559
self._error(_('You are not subscribed to %s.') % url)
525560
else:
526-
podcast.enable()
527-
self.client.commit()
561+
if podcast.pause_subscription:
562+
podcast.pause_subscription = False
563+
podcast.save()
564+
self._db.commit()
528565
self._error(_('Enabling feed update from %s.') % url)
529566

530567
return True
531568

532569
def youtube(self, url):
533-
yurl = self.client.youtube_url_resolver(url)
570+
fmt_ids = youtube.get_fmt_ids(self._config.youtube)
571+
yurl = youtube.get_real_download_url(url, fmt_ids)
534572
safe_print(yurl)
573+
535574
return True
536575

537576
def webui(self, public=None):
@@ -541,9 +580,9 @@ class gPodderCli(object):
541580
# interfaces, which could lead to problems.
542581
# Only use this on a trusted, private network!
543582
self._warn(_('Listening on ALL network interfaces.'))
544-
webui.main(only_localhost=False, core=self.client.core)
583+
webui.main(only_localhost=False, core=self.core)
545584
else:
546-
webui.main(core=self.client.core)
585+
webui.main(core=self.core)
547586

548587
def search(self, *terms):
549588
query = ' '.join(terms)
@@ -604,7 +643,7 @@ class gPodderCli(object):
604643

605644
@FirstArgumentIsPodcastURL
606645
def rewrite(self, old_url, new_url):
607-
podcast = self.client.get_podcast(old_url)
646+
podcast = self.get_podcast(old_url)
608647
if podcast is None:
609648
self._error(_('You are not subscribed to %s.') % old_url)
610649
else:
@@ -706,7 +745,7 @@ class gPodderCli(object):
706745

707746
def _tab_completion_podcast(self, text, count):
708747
"""Tab completion for podcast URLs"""
709-
urls = [p.url for p in self.client.get_podcasts() if text in p.url]
748+
urls = [p.url for p in self._model.get_podcasts() if text in p.url]
710749
if count < len(urls):
711750
return urls[count]
712751

0 commit comments

Comments
 (0)