Advertisement
Guest User

Untitled

a guest
Aug 13th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import feedparser
  4. import collections
  5. import shelve
  6. import os.path
  7. from urllib import request
  8.  
  9.  
  10.  
  11. LINK = 'http://torrentrss.net/getrss.php?rsslink=Lxye6Z' #вставляем свою ссылку с http://torrentrss.net/
  12. folder_to_save = '~/TDownload/torrent'                  #куда сохранять торрент файлы
  13. folder_to_save =  os.path.expanduser(folder_to_save)
  14. data_file = 'data.she'                                  #название файла shelve
  15.  
  16.  
  17. TORRENTS = collections.namedtuple('Torrents', 'serial series torrent_url torrent_name')
  18.  
  19. def torrent_nametuple(f):
  20.     serial = f['summary_detail']['value']
  21.     series = f['title']
  22.     series = series.replace('Финал','').replace('Сезона','').replace('сезона','').replace('WEBDLRip','').replace('|','').replace('/','').strip()
  23.     torrent_name = "{}_{}.torrent".format(serial,series.replace('.',''))
  24.     torrent_url = f['link']
  25.     return TORRENTS(serial,series,torrent_url,torrent_name)
  26.  
  27.  
  28. def main():
  29.     print('\n'*10)
  30.     d = shelve.open(data_file)
  31.    
  32.     feed = feedparser.parse( LINK )
  33.     for f in feed['entries'][::-1]:             #делаем итерацию по данным в rss
  34.         torrent = torrent_nametuple(f)     
  35.         try:
  36.             serial_dict = d[torrent.serial] #переходим к dict сериала
  37.         except KeyError:                    #если нету ещо такого сериала
  38.             d[torrent.serial] = {}          #создаем новый dict
  39.             serial_dict = d[torrent.serial] #и даем сылку на него
  40.            
  41.         if  serial_dict.pop(torrent.series, None) is not None:
  42.             continue                        #значит уже ету серию скачало
  43.         else:
  44.             print("качаю новый файл:\n  {}\n      {}".format(torrent.serial,torrent.series))
  45.             request.urlretrieve( torrent.torrent_url , os.path.join(folder_to_save,torrent.torrent_name) )
  46.             print("файл скачан добавляю его данные в базу...")
  47.             serial_dict[torrent.series] = torrent
  48.        
  49.     d.close()
  50.     return 0
  51.  
  52. if __name__ == '__main__':
  53.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement