Advertisement
Guest User

Untitled

a guest
Oct 4th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #! /usr/bin/env python-pyrocore
  2. # -*- coding: utf-8 -*-
  3.  
  4. from pyrocore import config
  5. from pyrocore.scripts import base
  6.  
  7.  
  8. class StuckTrackers(base.ScriptBaseWithConfig):
  9. """
  10. List started items whose announces are stuck, i.e. where
  11. last activity is older than the normal announce interval.
  12. """
  13.  
  14. # argument description for the usage information
  15. ARGS_HELP = ""
  16.  
  17.  
  18. def add_options(self):
  19. """ Add program options.
  20. """
  21. super(StuckTrackers, self).add_options()
  22.  
  23. # basic options
  24. self.add_bool_option("-a", "--all",
  25. help="list ALL items, not just stuck ones")
  26. self.add_bool_option("-s", "--stuck-only",
  27. help="list just stuck items / skip 'no enabled trackers' check")
  28. self.add_bool_option("-t", "--to-tagged",
  29. help="add stuck items to 'tagged' view")
  30.  
  31.  
  32. def mainloop(self):
  33. import time
  34. #from urlparse import urlparse
  35. from urllib.parse import urlparse
  36.  
  37. from collections import namedtuple, Counter
  38.  
  39. from pyrobase import fmt
  40. from pyrocore.util import xmlrpc
  41.  
  42. proxy = config.engine.open()
  43. now = int(time.time())
  44. fields = ('is_enabled is_busy url min_interval normal_interval'
  45. ' activity_time_last success_counter failed_counter scrape_counter').split()
  46. t_multicall = namedtuple('multicall', fields)
  47. rows = proxy.d.multicall('started', 'd.hash=', 't.multicall=,{}'.format(
  48. ','.join(['t.{}='.format(i) for i in fields])))
  49. stuck = Counter()
  50.  
  51. view = 'tagged'
  52. if self.options.to_tagged and view not in proxy.view.list():
  53. proxy.view.add(xmlrpc.NOHASH, view)
  54.  
  55. print('{:>5s} {:>2s} {:>5s} {:>5s} {:>6s} {:>13s} {:40s} {}'
  56. .format('S#', 'T#', 'OK', 'Error', 'Scrape', 'Last Announce',
  57. 'Infohash', 'Tracker Domain'))
  58. for idx, (infohash, trackers) in enumerate(rows, 1):
  59. trackers = [t_multicall(*t) for t in trackers]
  60.  
  61. if not any(t.is_enabled for t in trackers):
  62. if self.options.stuck_only:
  63. continue
  64. if self.options.to_tagged:
  65. proxy.d.views.push_back_unique(infohash, view)
  66. proxy.view.set_visible(infohash, view)
  67. domain = 'ALL TRACKERS DISABLED' if trackers else 'NO TRACKERS'
  68. stuck[domain] += 1
  69. print('{i:5d} {n:>2s} {n:>5s} {n:>5s} {n:>5s} {delta:>13s} {hash} {domain}'
  70. .format(i=idx, n='-', hash=infohash, delta='N/A', domain=domain))
  71. continue
  72.  
  73. for num, t in enumerate(trackers, 1):
  74. if not t.is_enabled:
  75. continue
  76.  
  77. delta = now - t.activity_time_last
  78. if self.options.all or delta > t.normal_interval:
  79. if self.options.to_tagged:
  80. proxy.d.views.push_back_unique(infohash, view)
  81. proxy.view.set_visible(infohash, view)
  82. domain = urlparse(t.url).netloc.split(':')[0]
  83. stuck[domain] += 1
  84.  
  85. print('{i:5d}; {n:2d}; '
  86. '{t.success_counter:5d}; {t.scrape_counter:5d}; {t.failed_counter:5d}; '
  87. '{delta}; {hash}; {domain};'
  88. .format(t=t, i=idx, n=num, hash=infohash, domain=domain,
  89. delta=fmt.human_duration(t.activity_time_last,
  90. precision=2, short=True)))
  91.  
  92. if sum(stuck.values()):
  93. if self.options.to_tagged:
  94. proxy.ui.current_view.set(view)
  95. self.LOG.info("Stuck items: TOTAL={}, {}".format(sum(stuck.values()),
  96. ', '.join(['{}={}'.format(*i) for i in stuck.most_common()])))
  97. self.LOG.debug("XMLRPC stats: %s" % proxy)
  98.  
  99.  
  100. if __name__ == "__main__":
  101. base.ScriptBase.setup()
  102. StuckTrackers().run()
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement