First fully functional commit
This commit is contained in:
199
PDMameUpdate.py
199
PDMameUpdate.py
@ -4,78 +4,197 @@ Checks available MAME Torrents on PleasureDome
|
|||||||
and updates the local versions if more recent
|
and updates the local versions if more recent
|
||||||
versions are detected
|
versions are detected
|
||||||
|
|
||||||
|
Basically what it does is:
|
||||||
|
* Real all torrents in a directory and filters MAME torrents
|
||||||
|
* Get all torrents from PleasureDome RSS
|
||||||
|
* Get all torrents currently active in Transmission
|
||||||
|
* Mix the three lists to get updatable torrents
|
||||||
|
* And for each updatable torrent:
|
||||||
|
- remove the old torrent from Transmission,
|
||||||
|
- rename the local directory,
|
||||||
|
- add the new torrent
|
||||||
|
|
||||||
Work in progress…
|
Work in progress…
|
||||||
|
* TODO: implement some error handling
|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
* Python3 with the libraries below
|
|
||||||
* Transmission for Bitorrent
|
* Transmission for Bitorrent
|
||||||
* A PleasureDome account
|
* A PleasureDome account
|
||||||
|
* A proper config.json file (see config.template.json)
|
||||||
|
* Python3 with the libraries below
|
||||||
|
* Linux (untested on other OS, but it might work)
|
||||||
|
|
||||||
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
|
Notes
|
||||||
/!\ Provided with no warranty whatsoever. /!\
|
* This script logs in PleasureDome to get the proper cookies.
|
||||||
/!\ Use with caution. /!\
|
It seems you can also set your cookies in Transmission using
|
||||||
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
|
a cookies.txt file in the .config/transmission directory
|
||||||
|
https://forum.transmissionbt.com/viewtopic.php?t=7468
|
||||||
|
|
||||||
|
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
|
||||||
|
/!\ Provided with no warranty whatsoever. /!\
|
||||||
|
/!\ Make sure you understand what the script /!\
|
||||||
|
/!\ does and adapt it to your context /!\
|
||||||
|
/!\ Use with caution. /!\
|
||||||
|
/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
|
||||||
'''
|
'''
|
||||||
import feedparser
|
import feedparser
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import requests
|
||||||
from clutch.core import Client
|
from clutch.core import Client
|
||||||
|
from collections import defaultdict
|
||||||
|
from pprint import pformat
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def open_config_file():
|
||||||
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
|
"""Reads configuration from config.json file"""
|
||||||
re_mame_version = re.compile('MAME 0.(\d+) (.*)')
|
|
||||||
|
|
||||||
logging.info('Opening config file: config.json')
|
logging.info('Opening config file: config.json')
|
||||||
with open('config.json') as config_file:
|
with open('config.json') as config_file:
|
||||||
config = json.load(config_file)
|
config = json.load(config_file)
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_local_torrents():
|
||||||
|
"""Fetches local torrents versions"""
|
||||||
|
|
||||||
logging.info('Fetching current MAME versions')
|
logging.info('Fetching current MAME versions')
|
||||||
files = os.listdir(config['mame_directory'])
|
directories = os.listdir(config['mame_directory'])
|
||||||
local_torrents = {}
|
for directory in directories:
|
||||||
for file in files:
|
match = re_mame_version.match(directory)
|
||||||
match = re_mame_version.match(file)
|
|
||||||
if match:
|
if match:
|
||||||
local_torrents[match[2]] = int(match[1])
|
torrents[match[2]]['local-version'] = int(match[1])
|
||||||
logging.info('Found the following Torrent version on disk: %s', local_torrents)
|
torrents[match[2]]['local-name'] = directory
|
||||||
|
logging.debug('Found the local torrent versions: %s', pformat(torrents))
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_remote_terrents():
|
||||||
|
"""Fetches PleasureDome torrents versions"""
|
||||||
|
|
||||||
logging.info('Opening PleasureDome RSS feed')
|
logging.info('Opening PleasureDome RSS feed')
|
||||||
d = feedparser.parse('http://www.pleasuredome.org.uk/rss.xml')
|
d = feedparser.parse('http://www.pleasuredome.org.uk/rss.xml')
|
||||||
remote_torrents = {}
|
|
||||||
for post in d.entries:
|
for post in d.entries:
|
||||||
match = re_mame_version.match(post.title)
|
match = re_mame_version.match(post.title)
|
||||||
if match:
|
if match:
|
||||||
remote_torrents[match[2]] = int(match[1])
|
torrents[match[2]]['remote-version'] = int(match[1])
|
||||||
logging.info('Found the following Torrent version on PleasureDome: %s', remote_torrents)
|
torrents[match[2]]['remote-link'] = post.link
|
||||||
|
torrents[match[2]]['remote-name'] = post.title
|
||||||
|
logging.debug('Found the remote torrent versions: %s', pformat(torrents))
|
||||||
|
|
||||||
for torrent, version in local_torrents.items():
|
|
||||||
if torrent in remote_torrents:
|
|
||||||
if version < remote_torrents[torrent]:
|
|
||||||
print('Torrent {}: {} -> {}'.format(torrent, version, remote_torrents[torrent]))
|
|
||||||
|
|
||||||
print('Should I update the torrents listed above? (y/N)')
|
def filter_updatable_torrents():
|
||||||
answer = input()
|
"""Checks if newer versions are available and prompt for update"""
|
||||||
if answer.lower() == 'y':
|
|
||||||
logging.info('Updating…')
|
|
||||||
|
|
||||||
logging.info('Connecting to Transmission Remote Control')
|
for torrent, data in list(torrents.items()):
|
||||||
client = Client(
|
keys_to_check = {'local-version','remote-version','transmission-id'}
|
||||||
username=config['transmission-user'],
|
if not ( keys_to_check.issubset(data.keys())
|
||||||
password=config['transmission-password'],
|
and data['local-version'] < data['remote-version']):
|
||||||
port=config['transmission-port']
|
del torrents[torrent]
|
||||||
)
|
logging.info('The following torrents can be updated: %s', pformat(torrents))
|
||||||
|
|
||||||
logging.info('Listing Transmission torrents')
|
|
||||||
for torrent in client.list().values():
|
|
||||||
print(torrent['name']);
|
|
||||||
|
|
||||||
|
def prompt_for_update():
|
||||||
|
"""Ask for user confirmation before updating"""
|
||||||
|
|
||||||
|
if len(torrents) > 0:
|
||||||
|
for torrent, data in torrents:
|
||||||
|
print('Torrent {}: {} -> {}'.format(
|
||||||
|
torrent,
|
||||||
|
data['local-version'],
|
||||||
|
data['remote-version']
|
||||||
|
))
|
||||||
|
print('Should I update the torrents listed above? (y/N)')
|
||||||
|
answer = input()
|
||||||
|
if answer.lower() != 'y':
|
||||||
|
logging.info('Quitting: user cancelled update')
|
||||||
|
print('Quitting…')
|
||||||
|
exit(0)
|
||||||
else:
|
else:
|
||||||
logging.info('Quitting: user cancelled update.')
|
logging.info('Quitting: no update candidate')
|
||||||
|
print('No update found…')
|
||||||
|
exit(0)
|
||||||
|
logging.info('User chose to update torrents')
|
||||||
|
|
||||||
|
|
||||||
# TODO: connect to PleasureDome
|
def get_cookies_from_pleasuredome():
|
||||||
# TODO: remove local torrent from Transmission
|
"""Connects to PleasureDome to retrieve Cookies"""
|
||||||
# TODO: rename local folder
|
|
||||||
# TODO: add new torrent to Transmission
|
logging.info('Logging in PleasureDome')
|
||||||
# TODO: implement some error handling
|
data = {
|
||||||
|
'uid':config['pleasuredome-user'],
|
||||||
|
'pwd':config['pleasuredome-password']
|
||||||
|
}
|
||||||
|
r = requests.post('http://www.pleasuredome.org.uk/login2.php', data=data)
|
||||||
|
if r.status_code == 200:
|
||||||
|
logging.info('Connected to PleasureDome')
|
||||||
|
else:
|
||||||
|
logging.error(
|
||||||
|
'Connection to PleasureDome failed with status %s',
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
exit(1)
|
||||||
|
return {k: r.cookies[k] for k in ('uid','pass')}
|
||||||
|
|
||||||
|
|
||||||
|
def connect_to_transmission():
|
||||||
|
"""Connects to Transmission and return a Client object"""
|
||||||
|
|
||||||
|
logging.info('Connecting to Transmission Remote Control')
|
||||||
|
return Client(
|
||||||
|
username=config['transmission-user'],
|
||||||
|
password=config['transmission-password'],
|
||||||
|
port=config['transmission-port']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_transmission_torrents():
|
||||||
|
"""Gets the torrents id from Transmission"""
|
||||||
|
|
||||||
|
logging.info('Listing Transmission torrents')
|
||||||
|
for torrent in client.list().values():
|
||||||
|
match = re_mame_version.match(torrent['name'])
|
||||||
|
if match:
|
||||||
|
torrents[match[2]]['transmission-id'] = torrent['id']
|
||||||
|
logging.debug('Found the Transmission torrent ids: %s', pformat(torrents))
|
||||||
|
|
||||||
|
|
||||||
|
def update_torrents():
|
||||||
|
"""
|
||||||
|
Updates torrents:
|
||||||
|
* remove it from Transmission,
|
||||||
|
* rename the local directory,
|
||||||
|
* and add the new torrent
|
||||||
|
"""
|
||||||
|
logging.info('Updating torrents')
|
||||||
|
for torrent in torrents.values():
|
||||||
|
old_name = os.path.join(config['mame_directory'], torrent['local-name'])
|
||||||
|
new_name = os.path.join(config['mame_directory'], torrent['remote-name'])
|
||||||
|
|
||||||
|
client.torrent.remove(torrent['transmission-id']);
|
||||||
|
os.rename(old_name, new_name)
|
||||||
|
client.torrent.add(
|
||||||
|
filename=torrent['remote-link'],
|
||||||
|
cookies=cookies,
|
||||||
|
paused=True
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format=' %(asctime)s - %(levelname)s - %(message)s'
|
||||||
|
)
|
||||||
|
|
||||||
|
re_mame_version = re.compile('MAME 0.(\d+) (.*)')
|
||||||
|
config = open_config_file()
|
||||||
|
torrents = defaultdict(dict)
|
||||||
|
|
||||||
|
client = connect_to_transmission()
|
||||||
|
cookies = get_cookies_from_pleasuredome()
|
||||||
|
|
||||||
|
fetch_local_torrents()
|
||||||
|
fetch_remote_terrents()
|
||||||
|
fetch_transmission_torrents()
|
||||||
|
filter_updatable_torrents()
|
||||||
|
prompt_for_update()
|
||||||
|
update_torrents()
|
@ -2,5 +2,7 @@
|
|||||||
"mame_directory":"/path/to/your/locol/mame/torrent/target/folders",
|
"mame_directory":"/path/to/your/locol/mame/torrent/target/folders",
|
||||||
"transmission-user":"user",
|
"transmission-user":"user",
|
||||||
"transmission-password":"pass",
|
"transmission-password":"pass",
|
||||||
|
"pleasuredome-user":"user",
|
||||||
|
"pleasuredome-password":"pass",
|
||||||
"transmission-port":8080
|
"transmission-port":8080
|
||||||
}
|
}
|
Reference in New Issue
Block a user