Guest User

Untitled

a guest
Nov 22nd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 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.  
  12. LINKS = [ #вставляем свою ссылку с http://torrentrss.net/
  13.         'http://torrentrss.net/getrss.php?rsslink=Lxye6Z',
  14.         'http://torrentrss.net/getrss.php?rsslink=PALCu9'
  15.         ]
  16. folder_to_save = '~/TDownload/torrent'                  #куда сохранять торрент файлы
  17. folder_to_save =  os.path.expanduser(folder_to_save)
  18. data_file = 'data.she'                                  #название файла shelve
  19.  
  20. loadfmt = "\nкачаю новый файл:\n  {}\n      {}"
  21.  
  22. TORRENTS = collections.namedtuple('Torrents', 'serial series torrent_url torrent_name')
  23.  
  24. def torrent_nametuple(f):
  25.     serial = f['summary_detail']['value']
  26.     series = f['title']
  27.     series = series.replace('Финал','').replace('Сезона','').replace('сезона','').replace('WEBDLRip','').replace('|','').replace('/','').strip()
  28.     torrent_name = "{}_{}.torrent".format(serial,series.replace('.',''))
  29.     torrent_url = f['link']
  30.     return TORRENTS(serial,series,torrent_url,torrent_name)
  31.  
  32.  
  33. def main():
  34.  
  35.     print('\n'*10)
  36.     d = shelve.open(data_file, writeback=True)
  37.  
  38.     x= 0
  39.    
  40.     for LINK in LINKS:
  41.         feed = feedparser.parse( LINK )
  42.         for f in feed['entries'][::-1]:             #делаем итерацию по данным в rss
  43.             torrent = torrent_nametuple(f)
  44.          
  45.             if torrent.serial in d:
  46.                 serial_dict = d[torrent.serial]
  47.             else:
  48.                 serial_dict = d[torrent.serial] = {}
  49.  
  50.             if torrent.series not in serial_dict:
  51.                 print(loadfmt.format(torrent.serial, torrent.series))
  52.  
  53.                 request.urlretrieve(
  54.                                 torrent.torrent_url,
  55.                                 os.path.join(folder_to_save, torrent.torrent_name)
  56.                                 )
  57.                 serial_dict[torrent.series] = tuple(torrent)
  58.  
  59.                 print("файл скачан добавляю его данные в базу...")
  60.             else:
  61.                 x += 1
  62.            
  63.     d.close()
  64.     print('Старых файлов: {}'.format(x))
  65.     return 0
  66.  
  67. if __name__ == '__main__':
  68.     main()
Advertisement
Add Comment
Please, Sign In to add comment