Checks available MAME Torrents on PleasureDome and updates the local versions if more recent versions are detected
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
11KB

  1. #!/usr/bin/python3
  2. '''
  3. Checks available MAME Torrents on PleasureDome
  4. and updates the local versions if more recent
  5. versions are detected
  6. Basically what it does is:
  7. * Get all torrents in a directory and filter MAME torrents
  8. * Get all torrents from PleasureDome RSS
  9. * Get all torrents currently active in Transmission
  10. * Intersect the three lists to get updatable torrents
  11. * And for each updatable torrent:
  12. - remove the old torrent from Transmission,
  13. - rename the local directory,
  14. - add the new torrent
  15. Work in progress…
  16. * TODO: implement some error handling
  17. Requirements:
  18. * Transmission for Bitorrent
  19. * A PleasureDome account
  20. * A proper PDMameUpdate.json file (see PDMameUpdate.template.json)
  21. * Python3 with the libraries below
  22. - feedparser
  23. - transmission-clutch
  24. - tabulate
  25. * Linux (untested on other OS, but it might work)
  26. Notes
  27. * This script logs in PleasureDome to get the proper cookies.
  28. It seems you can also set your cookies in Transmission using
  29. a cookies.txt file in the .config/transmission directory
  30. See: https://forum.transmissionbt.com/viewtopic.php?t=7468
  31. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  32. !!! Provided with no warranty whatsoever. !!!
  33. !!! Make sure you understand what the script !!!
  34. !!! does and adapt it to your context !!!
  35. !!! Use with caution. !!!
  36. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  37. '''
  38. import argparse
  39. import feedparser
  40. import json
  41. import logging
  42. import os
  43. import re
  44. import requests
  45. import time
  46. import pathlib
  47. from clutch.core import Client
  48. from tabulate import tabulate
  49. from collections import defaultdict
  50. from pprint import pformat
  51. from urllib.parse import quote
  52. def open_config_file():
  53. """Reads configuration from PDMameUpdate.json file"""
  54. locations = (
  55. os.environ.get("PDMAMEUPDATE_CONF"),
  56. "/etc",
  57. os.path.expanduser("~"),
  58. os.curdir
  59. )
  60. for loc in locations:
  61. if loc is not None:
  62. logging.info("Searching for config file in '%s'", loc)
  63. config_path = loc + '/PDMameUpdate.json'
  64. if pathlib.Path(config_path).is_file():
  65. config_file_loc = config_path
  66. break
  67. if 'config_file_loc' not in locals():
  68. logging.error("Config file not found")
  69. raise FileNotFoundError("Config file not found")
  70. logging.info('Opening config file: '+config_file_loc)
  71. config_file = None
  72. with open(config_file_loc, 'r') as config_file:
  73. config = json.load(config_file)
  74. parameters = [
  75. "mame-directory",
  76. "pleasuredome-password",
  77. "pleasuredome-user",
  78. "torrent-directory",
  79. "transmission-password",
  80. "transmission-port",
  81. "transmission-user"
  82. ]
  83. for key in parameters:
  84. if key not in config:
  85. logging.error('Missing key in config file: %s', key)
  86. raise ValueError('Invalid config file.')
  87. return config
  88. def fetch_local_torrents():
  89. """Fetches local torrents versions"""
  90. logging.info('Fetching current MAME versions')
  91. directories = os.listdir(config['mame-directory'])
  92. for directory in directories:
  93. match = re_mame_version.match(directory)
  94. if match:
  95. torrents[match.group(2)]['local-version'] = int(match.group(1))
  96. torrents[match.group(2)]['local-name'] = directory
  97. logging.debug('Found the local torrent versions: %s', pformat(torrents))
  98. def fetch_remote_torrents():
  99. """Fetches PleasureDome torrents versions"""
  100. logging.info('Opening PleasureDome RSS feed')
  101. d = feedparser.parse('http://www.pleasuredome.org.uk/rss.xml')
  102. for post in d.entries:
  103. match = re_mame_version.match(post.title)
  104. if match:
  105. matched_version = int(match.group(1))
  106. matched_torrent = torrents[match.group(2)]
  107. if matched_version > matched_torrent.get('remote-version', 0):
  108. matched_torrent['remote-version'] = matched_version
  109. matched_torrent['remote-link'] = post.link
  110. matched_torrent['remote-name'] = post.title
  111. else:
  112. logging.info("Skipping '{}' version '{}'".format(
  113. match.group(0),
  114. matched_version
  115. ))
  116. logging.debug('Found the remote torrent versions: %s', pformat(torrents))
  117. def filter_updatable_torrents():
  118. """Checks if newer versions are available and prompt for update"""
  119. for torrent, data in list(torrents.items()):
  120. keys_to_check = {'local-version', 'remote-version', 'transmission-id'}
  121. if (
  122. keys_to_check.issubset(data.keys())
  123. and
  124. data['local-version'] < data['remote-version']
  125. ):
  126. check_and_rewrite_download_url(data)
  127. else:
  128. del torrents[torrent]
  129. logging.debug(
  130. 'The following torrents can be updated: %s',
  131. [t for t in torrents.keys()]
  132. )
  133. def check_and_rewrite_download_url(torrent_data):
  134. url_match = re.compile(
  135. r"https?://www.pleasuredome.org.uk/details.php\?id=(.*)"
  136. )
  137. match = url_match.match(torrent_data['remote-link'])
  138. if match:
  139. url = ("http://www.pleasuredome.org.uk/download.php"
  140. "?id={}&f={}.torrent&secure=no").format(
  141. match.group(1),
  142. quote('+'.join(torrent_data['remote-name'].split(' ')), safe='+')
  143. )
  144. logging.info('Changed url {} to {}'.format(
  145. torrent_data['remote-link'],
  146. url
  147. ))
  148. torrent_data['remote-link'] = url
  149. def prompt_for_update():
  150. """Ask for user confirmation before updating"""
  151. output_header = ["Torrent", "from", "to"]
  152. output = []
  153. if len(torrents) > 0:
  154. for torrent, data in torrents.items():
  155. output.append([
  156. torrent,
  157. data['local-version'],
  158. data['remote-version']
  159. ])
  160. print(tabulate(output, headers=output_header, tablefmt="simple"))
  161. print('Should I update the torrents listed above? (y/N)')
  162. answer = input()
  163. if answer.lower() != 'y':
  164. logging.info('Quitting: user cancelled update')
  165. print('Quitting…')
  166. exit(0)
  167. else:
  168. logging.info('Quitting: no update candidate')
  169. print('No update found…')
  170. exit(0)
  171. logging.info('User chose to update torrents')
  172. def get_cookies_from_pleasuredome():
  173. """Connects to PleasureDome to retrieve Cookies"""
  174. logging.info('Logging in PleasureDome')
  175. data = {
  176. 'uid': config['pleasuredome-user'],
  177. 'pwd': config['pleasuredome-password']
  178. }
  179. r = requests.post('http://www.pleasuredome.org.uk/login3.php', data=data)
  180. if r.status_code == 200:
  181. logging.info('Connected to PleasureDome')
  182. logging.info('Logging out')
  183. requests.get('http://www.pleasuredome.org.uk/logout.php')
  184. else:
  185. logging.error(
  186. 'Connection to PleasureDome failed with status %s',
  187. r.status_code
  188. )
  189. exit(1)
  190. return {k: r.cookies[k] for k in ('uid', 'pass')}
  191. def connect_to_transmission():
  192. """Connects to Transmission and return a Client object"""
  193. logging.info('Connecting to Transmission Remote Control')
  194. return Client(
  195. username=config['transmission-user'],
  196. password=config['transmission-password'],
  197. port=config['transmission-port']
  198. )
  199. def fetch_transmission_torrents():
  200. """Gets the torrents id from Transmission"""
  201. logging.info('Listing Transmission torrents')
  202. for torrent in client.list().values():
  203. match = re_mame_version.match(torrent['name'])
  204. if match:
  205. torrents[match.group(2)]['transmission-id'] = torrent['id']
  206. logging.debug('Found the Transmission torrent ids: %s', pformat(torrents))
  207. def update_torrents():
  208. """
  209. Updates torrents:
  210. * remove it from Transmission,
  211. * rename the local directory,
  212. * and add the new torrent
  213. """
  214. logging.info('Updating torrents')
  215. for torrent in torrents.values():
  216. logging.info('Updating torrent : %s', torrent['remote-name'])
  217. old_name = os.path.join(
  218. config['mame-directory'],
  219. torrent['local-name']
  220. )
  221. new_name = os.path.join(
  222. config['mame-directory'],
  223. torrent['remote-name']
  224. )
  225. new_torrent = os.path.join(
  226. config['torrent-directory'],
  227. torrent['remote-name']+'.torrent'
  228. )
  229. logging.debug('Removing from transmission : %s',torrent['transmission-id'])
  230. client.torrent.remove(torrent['transmission-id'])
  231. logging.debug('Renaming %s to %s',old_name, new_name)
  232. os.rename(old_name, new_name)
  233. logging.debug('Adding to transmission : %s', torrent['remote-link'])
  234. client.torrent.add(
  235. filename=torrent['remote-link'],
  236. download_dir=config['mame-directory'],
  237. cookies=cookies,
  238. paused=False
  239. )
  240. if args.keep:
  241. logging.debug('Downloading torrent : %s', new_torrent)
  242. t = requests.get(torrent['remote-link'], verify=False, cookies=cookies)
  243. open(new_torrent, 'wb').write(t.content)
  244. if __name__ == '__main__':
  245. logging.basicConfig(
  246. level=logging.WARNING,
  247. format=' %(asctime)s - %(levelname)s - %(message)s'
  248. )
  249. parser = argparse.ArgumentParser(
  250. description='Update PleasureDome MAME Torrents'
  251. )
  252. parser.add_argument(
  253. '-l', '--log',
  254. action='store_true',
  255. help='Display more log messages'
  256. )
  257. parser.add_argument(
  258. '-d', '--debug',
  259. action='store_true',
  260. help='Display debugging messages'
  261. )
  262. parser.add_argument(
  263. '-k', '--keep',
  264. action='store_true',
  265. help='Keep torrent files localy'
  266. )
  267. parser.add_argument(
  268. '-c', '--countdown',
  269. action='store_true',
  270. help='Start with a 5 second countdown'
  271. )
  272. args = parser.parse_args()
  273. if args.log:
  274. logging.getLogger().setLevel(logging.INFO)
  275. if args.debug:
  276. logging.getLogger().setLevel(logging.DEBUG)
  277. if args.countdown:
  278. print('PDMameUpdate is about to start')
  279. # Useful if you run this script when your machine boots
  280. for i in range(5, 0, -1):
  281. print('{}\r'.format(i), end=''),
  282. time.sleep(1)
  283. re_mame_version = re.compile(r'MAME 0.(\d+) (.*)')
  284. config = open_config_file()
  285. torrents = defaultdict(dict)
  286. client = connect_to_transmission()
  287. cookies = get_cookies_from_pleasuredome()
  288. fetch_local_torrents()
  289. fetch_remote_torrents()
  290. fetch_transmission_torrents()
  291. filter_updatable_torrents()
  292. prompt_for_update()
  293. update_torrents()