def setup(bot): global api bot.config.define_section('apollo', ApolloSection) try: if bot.config.apollo.api == True and api == None: if os.path.isfile(bot.config.apollo.cookie_file): try: cookies = pickle.load( open(bot.config.apollo.cookie_file, 'rb')) api = whatapi.WhatAPI(username=bot.config.apollo.username, password=bot.config.apollo.password, server=bot.config.apollo.server, cookies=cookies) except: api = whatapi.WhatAPI(username=bot.config.apollo.username, password=bot.config.apollo.password, server=bot.config.apollo.server) else: api = whatapi.WhatAPI(username=bot.config.apollo.username, password=bot.config.apollo.password, server=bot.config.apollo.server) except: api = None if not bot.memory.contains('help'): bot.memory['help'] = sopel.tools.SopelMemory() bot.memory['help']['apollo'] = sopel.tools.SopelMemory() bot.memory['help']['apollo']['short'] = 'Feature parity with APOLLO' bot.memory['help']['apollo']['long'] = { helptext('all', '!sandwich', 'Requests a sandwich'), helptext('all', '!slap <target>', 'Slaps somebody around'), helptext('all', '!apollo stats', 'Prints site stats'), helptext('all', '!apollo user <id>', 'Prints user info'), helptext('all', '!apollo user <name>', 'Prints user search results'), helptext('all', '!apollo loadavg', 'Prints loadavg for some reason'), helptext( 'all', '!apollo top10 torrents day|week|overall|snatched|data|seeded', 'Searches torrent top 10'), helptext('all', '!apollo top10 tags ut|ur|v', 'Searches tag top 10'), helptext('all', '!apollo top10 users ul|dl|numul|uls|dls', 'Searches user top 10'), helptext('admin', '!sudoslap <count> <target>', 'Slaps somebody around. A bunch'), helptext('admin', '!sudosandwich', 'Requests a sandwich'), helptext( 'admin', '!simulate', 'Engage in an authentic replication of the day to day life of an APOLLO' ) } if not bot.memory.contains('url_callbacks'): bot.memory['url_callbacks'] = sopel.tools.SopelMemory() bot.memory['url_callbacks'][regex] = title
def main(): ss = Preferences() currentline = 0 #to resume a broken download. set this to the last SUCCESSFUL number (due to 1 starting at 0) that you see was outputted to console try: cookies = pickle.load( open(ss.getwpath("cookiesfile"), 'rb')) #cookies speed up the HTTP (supposedly) except: cookies = None #if we cant load it, don't use it. credentials = open(ss.getwpath("credentialsfile"), 'rb').readlines( ) #store credentials in another file and .git-ignore it username = credentials[0].strip() password = credentials[1].strip() apihandle = whatapi.WhatAPI(config_file=None, username=username, password=password, cookies=cookies) filenamewithIDs = ss.getwpath( "outpath1") # ("1seeding_ID+Hash+Filename.txt") hashdir = ss.getwpath("script2destdir") #output dir openedfile = open(filenamewithIDs, 'r', encoding='utf-8').readlines() for eachline in islice(openedfile, currentline, None): #will continue where it left off idandhash = eachline.strip().split(' / ') currentID = idandhash[0] currentHash = idandhash[1] if not os.path.exists(os.path.join(hashdir, currentHash)): #currentHash = "E7A5718EC52633FCCB1EA85656AA0622543994D7" #test hash for debugging try: response = apihandle.request(1.75, "torrent", id=currentID)[ "response"] #talk to server and receive a response except whatapi.RequestException as e: currentline += 1 print(currentline, " ERROR. Your search did not match anything.") continue with open(os.path.join(hashdir, currentHash), 'w') as outfile: json.dump(response, outfile, sort_keys=True) currentline += 1 print(currentline, ": ", currentID) pickle.dump(apihandle.session.cookies, open(ss.getwpath("cookiesfile"), 'wb')) #store cookies when script ends, for next-run. print("Download Complete.")
def handle(): global apihandle try: setting = settings.get('what_credentials') domain = settings.get('domain')[1] if apihandle != None: return apihandle apihandle = whatapi.WhatAPI(username=setting[1], password=setting[2], domain=domain) return apihandle except: raise Exception( 'Something went wrong connecting to WhatCD. Ensure that it is up and running, and that your credentials are correct.' )
return float(cost) except: return 0.0 def to_date(dt): match = re.match( r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}) (?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})', dt) d = match.groupdict() return datetime(year=int(d['year']), month=int(d['month']), day=int(d['day']), hour=int(d['hour']), minute=int(d['min']), second=int(d['sec'])) if __name__ == '__main__': config = ConfigParser.ConfigParser() config.read('account.ini') try: apihandle = whatapi.WhatAPI(username=config.get('Account', 'Username'), password=config.get('Account', 'Password')) print 'Authentication success!' except: print 'Authentication failure! Ensure correct login information in account.ini' retrieve_pages(apihandle)
import whatapi from googlevoice import Voice from modules import extractInput from sender import checkInbox import responder import time config = open ('settings.conf', 'r') settings = config.read().split('\n') voice = Voice() voice.login() #access what.CD API apihandle = whatapi.WhatAPI(username=settings[0], password=settings[1]) responder = responder.textResponses(apihandle, settings[0]) print "Ok, checking your inbox!" prevId = 0 prevResponse = '' while True: newMessage = checkInbox(apihandle) #statements to let user know of new forum threads / etc. if prevId != newMessage[3] and settings[0] != newMessage[0]: prevId = newMessage[3] messageText = "%s - %s\n%s" % newMessage[0:3] print "Sending message: ", messageText
def __init__(self, config_file=None, state_file=None, lock_file=None): self.config_file = config_file or WhatFreeGrab.CONFIG_FILE self.state_file = state_file or WhatFreeGrab.STATE_FILE self.lock_file = lock_file or WhatFreeGrab.LOCK_FILE self.instance = SingleInstance(self.lock_file) self.start_time = time.time() self.config = ConfigParser.RawConfigParser(WhatFreeGrab.defaults) try: self.config.read(self.config_file) except: self.quit("Unable to read configuration file.", error=True) # This is necessary because otherwise we get 'NoSectionError' even if # the value is set in the defaults. try: self.config.add_section('output') except ConfigParser.DuplicateSectionError: pass self.quiet = self.config.getboolean('output', 'quiet') self.message(WhatFreeGrab.IDENT) self.message("-" * len(WhatFreeGrab.IDENT)) self.message("Startup time: %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_time))) self.username = self.config.get('login', 'username') self.password = self.config.get('login', 'password') if not (self.username and self.password): self.quit("No username or password specified in configuration.", error=True) self.target = self.config.get('download', 'target') if not self.target: self.quit("No target directory specified in configuration.", error=True) self.target = os.path.realpath(os.path.expanduser(self.target)) if not os.path.exists(self.target): os.makedirs(self.target) self.max_torrents = self.config.getint('download', 'max_torrents') self.skip_downloads = '--skip-downloads' in sys.argv self.template_music = self.config.get('download', 'template_music') self.template_other = self.config.get('download', 'template_other') if not '${torrentId}' in (self.template_music and self.template_other): self.quit("Naming templates in configuration MUST contain ${torrentId}", error=True) self.template_music = string.Template(self.template_music) self.template_other = string.Template(self.template_other) # Look out! No default values available from here on. self.config._defaults.clear() self.filters = [] for section in self.config.sections(): if section.startswith("filter-"): self.filters.append(dict(self.config.items(section))) if not self.filters: self.filters = [{}] try: self.state = pickle.load(open(self.state_file, 'rb')) except: self.state = {} cookies = self.state.get('cookies') try: self.what = whatapi.WhatAPI(config_file=self.config_file, cookies=cookies) except whatapi.whatapi.LoginException: self.quit("Unable to login. Check your configuration.", error=True) self.state['cookies'] = self.what.session.cookies self.save_state() if not 'history' in self.state: self.state['history'] = set() self.torrent_list = [] self.counter = {} for key in 'total', 'downloaded', 'skipped', 'exists', 'error': self.counter[key] = 0
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog='orpheusbetter') parser.add_argument('release_urls', nargs='*', help='the URL where the release is located') parser.add_argument( '-s', '--single', action='store_true', help= 'only add one format per release (useful for getting unique groups)') parser.add_argument('-j', '--threads', type=int, help='number of threads to use when transcoding', default=max(cpu_count() - 1, 1)) parser.add_argument('--config', help='the location of the configuration file', \ default=os.path.expanduser('~/.orpheusbetter/config')) parser.add_argument('--cache', help='the location of the cache', \ default=os.path.expanduser('~/.orpheusbetter/cache')) parser.add_argument( '-U', '--no-upload', action='store_true', help='don\'t upload new torrents (in case you want to do it manually)') parser.add_argument( '-E', '--no-24bit-edit', action='store_true', help='don\'t try to edit 24-bit torrents mistakenly labeled as 16-bit') parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) parser.add_argument( '-m', '--mode', help= 'mode to search for transcode candidates; snatched, uploaded, both, seeding, or all' ) parser.add_argument('-S', '--skip', action='store_true', help="treats a torrent as already processed") parser.add_argument('-t', '--totp', help="time based one time password for 2FA", default=None) parser.add_argument( '-o', '--source', help="the value to put in the source flag in created torrents") args = parser.parse_args() config = configparser.ConfigParser(interpolation=None) try: open(args.config) config.read(args.config) except: if not os.path.exists(os.path.dirname(args.config)): os.makedirs(os.path.dirname(args.config)) config.add_section('orpheus') config.set('orpheus', 'username', '') config.set('orpheus', 'password', '') config.set('orpheus', 'data_dir', '') config.set('orpheus', 'output_dir', '') config.set('orpheus', 'torrent_dir', '') config.set('orpheus', 'formats', 'flac, v0, 320, v2') config.set('orpheus', 'media', ', '.join(whatapi.lossless_media)) config.set('orpheus', '24bit_behaviour', '0') config.set('orpheus', 'tracker', 'https://home.opsfet.ch/') config.set('orpheus', 'mode', 'both') config.set('orpheus', 'api', 'https://orpheus.network') config.set('orpheus', 'source', 'OPS') config.write(open(args.config, 'w')) print("Please edit the configuration file: {0}".format(args.config)) sys.exit(2) finally: username = config.get('orpheus', 'username') password = config.get('orpheus', 'password') do_24_bit = config.get('orpheus', '24bit_behaviour') data_dir = [ os.path.expanduser(d) for d in config.get('orpheus', 'data_dir').split(';') ] try: output_dir = os.path.expanduser(config.get('orpheus', 'output_dir')) except configparser.NoOptionError: output_dir = None if not output_dir: output_dir = data_dir[0] torrent_dir = os.path.expanduser(config.get('orpheus', 'torrent_dir')) supported_formats = [ format.strip().upper() for format in config.get('orpheus', 'formats').split(',') ] try: media_config = config.get('orpheus', 'media') if not media_config: supported_media = whatapi.lossless_media else: supported_media = set([ medium.strip().lower() for medium in media_config.split(',') ]) if not supported_media.issubset(set(whatapi.lossless_media)): print( 'Unsupported media type "{0}", edit your configuration' .format( (supported_media - whatapi.lossless_media).pop())) print("Supported types are: {0}".format(', '.join( whatapi.lossless_media))) sys.exit(2) except configparser.NoOptionError: supported_media = whatapi.lossless_media if not config.has_option('orpheus', 'tracker'): config.set('orpheus', 'tracker', 'https://home.opsfet.ch/') config.write(open(args.config, 'w')) print("Tracker added to config file") if not config.has_option('orpheus', 'mode'): config.set('orpheus', 'mode', 'both') config.write(open(args.config, 'w')) print("Candidate mode set to both") if not config.has_option('orpheus', 'api'): config.set('orpheus', 'api', 'https://orpheus.network') config.write(open(args.config, 'w')) print("API endpoint set to https://orpheus.network") version_path = os.path.join(os.path.expanduser('~'), '.orpheusbetter', '.version') if os.path.isfile(version_path): with open(version_path, 'r') as f: version = f.read() else: version = "1.0.0" if StrictVersion(version) < StrictVersion(__version__): updated = False if StrictVersion(version) < StrictVersion('2.0.1'): updated = True config.set('orpheus', 'source', 'OPS') if updated: config.write(open(args.config, 'w')) print("Migrated version {0} config".format(version)) with open(version_path, 'w') as f: f.write(__version__) tracker = config.get('orpheus', 'tracker') mode = config.get('orpheus', 'mode') endpoint = config.get('orpheus', 'api') source = None if config.has_option('orpheus', 'source'): source = config.get('orpheus', 'source') upload_torrent = not args.no_upload print("Logging in to Orpheus Network...") api = whatapi.WhatAPI(username, password, endpoint, args.totp) try: seen = pickle.load(open(args.cache, 'rb')) except: seen = set() pickle.dump(seen, open(args.cache, 'wb')) if args.skip: skip = [int(query['torrentid']) for query in\ [dict(parse_qsl(urlparse(url).query)) for url in args.release_urls]] for id in skip: print("Skipping torrent {0}".format(str(id))) seen.add(str(id)) pickle.dump(seen, open(args.cache, 'wb')) return print("Searching for transcode candidates...") if args.release_urls: if len(args.release_urls) == 1 and os.path.isfile( args.release_urls[0]): print( "You supplied a url list, ignoring your configuration's media types." ) with open(args.release_urls[0]) as f: candidates = [(int(query['id']), int(query['torrentid'])) for query in\ [dict(parse_qsl(urlparse(url).query)) for url in f]] else: print( "You supplied one or more release URLs, ignoring your configuration's media types." ) candidates = [(int(query['id']), int(query['torrentid'])) for query in\ [dict(parse_qsl(urlparse(url).query)) for url in args.release_urls]] else: if args.mode is None: if mode == 'none': print("No urls provided and scraping is disabled") else: candidates = api.get_candidates(mode, skip=seen, media=supported_media) elif args.mode == 'none': print("No urls provided and scraping is disabled") else: candidates = api.get_candidates(args.mode, skip=seen, media=supported_media) #candidates = api.snatched(skip=seen, media=supported_media) for groupid, torrentid in candidates: group = api.request('torrentgroup', id=groupid) torrent = [t for t in group['torrents'] if t['id'] == torrentid][0] release = "Release found: {0} ({1})".format( whatapi.unescape(group['group']['name']), group['group']['year']) releaseurl = "Release URL: {0}".format(api.release_url(group, torrent)) print("") print(release) print(releaseurl) if not torrent['filePath']: for d in data_dir: flac_file = os.path.join( d, whatapi.unescape(torrent['fileList']).split('{{{')[0]) if not os.path.exists(flac_file): continue flac_dir = os.path.join( d, "{0} ({1}) [FLAC]".format( whatapi.unescape(group['group']['name']), group['group']['year'])) if not os.path.exists(flac_dir): os.makedirs(flac_dir) shutil.copy(flac_file, flac_dir) break if not os.path.exists(flac_file): print("Path not found - skipping: {0}".format(flac_file)) continue else: for d in data_dir: flac_dir = os.path.join(d, whatapi.unescape(torrent['filePath'])) if os.path.exists(flac_dir): break #flac_dir = flac_dir.encode(sys.getfilesystemencoding()) if int(do_24_bit): try: if transcode.is_24bit( flac_dir) and torrent['encoding'] != '24bit Lossless': # A lot of people are uploading FLACs from Bandcamp without realizing # that they're actually 24 bit files (usually 24/44.1). Since we know for # sure whether the files are 24 bit, we might as well correct the listing # on the site (and get an extra upload in the process). if args.no_24bit_edit: print("Release is actually 24-bit lossless, skipping.") continue if int(do_24_bit) == 1: confirmation = raw_input( "Mark release as 24bit lossless? y/n: ") if confirmation != 'y': continue print("Marking release as 24bit lossless.") api.set_24bit(torrent) group = api.request('torrentgroup', id=groupid) torrent = [ t for t in group['torrents'] if t['id'] == torrentid ][0] except Exception as e: print( "Error: can't edit 24-bit torrent - skipping: {0}".format( e)) continue if transcode.is_multichannel(flac_dir): print( "This is a multichannel release, which is unsupported - skipping" ) continue needed = formats_needed(group, torrent, supported_formats) print("Formats needed: {0}".format(', '.join(needed))) # if needed: # # Before proceeding, do the basic tag checks on the source # # files to ensure any uploads won't be reported, but punt # # on the tracknumber formatting; problems with tracknumber # # may be fixable when the tags are copied. # broken_tags = False # for flac_file in transcode.locate(flac_dir, transcode.ext_matcher('.flac')): # # (ok, msg) = tagging.check_tags(flac_file, check_tracknumber_format=False) # # if not ok: # # print("A FLAC file in this release has unacceptable tags - skipping: {0}".format(msg)) # # print("You might be able to trump it.") # # broken_tags = True # # break # if broken_tags: # continue for format in needed: if os.path.exists(flac_dir): print("Adding format {0}...".format(format), end=" ") tmpdir = tempfile.mkdtemp() try: local_output_dir = config.get( 'orpheus', 'output_dir_{}'.format(format)) except: local_output_dir = output_dir try: local_torrent_dir = config.get( 'orpheus', 'torrent_dir_{}'.format(format)) except: local_torrent_dir = torrent_dir try: transcode_dir = transcode.transcode_release( flac_dir, local_output_dir, format, max_threads=args.threads) new_torrent = transcode.make_torrent( transcode_dir, tmpdir, tracker, api.passkey, source) if upload_torrent: permalink = api.permalink(torrent) description = create_description( torrent, flac_dir, format, permalink) api.upload(group, torrent, new_torrent, format, description) shutil.copy(new_torrent, local_torrent_dir) print("done!") if args.single: break except Exception as e: print("Error adding format {0}: {1}".format(format, e)) traceback.print_exc() finally: shutil.rmtree(tmpdir) else: print("Path not found - skipping: {0}".format(flac_dir)) break seen.add(str(torrentid)) pickle.dump(seen, open(args.cache, 'wb'))
import whatapi from ConfigParser import SafeConfigParser config = SafeConfigParser() config.read('../config.ini') username = config.get('redacted', 'username') password = config.get('redacted', 'password') apihandle = whatapi.WhatAPI(username = username, password = password, , server='https://redacted.ch') print
def init(): log.info("Trying to load config file config.cfg") config = configparser.ConfigParser(interpolation=None) config.read("config.cfg") # Ensure config file exists and is valid if not 'Settings' in config: log.error( "Invalid config file. It must contains a Settings section. Please refer to config.cfg.example" ) sys.exit() # Ensure music directory is valid settings['MusicDirectory'] = config['Settings']['MusicDirectory'] if os.path.exists(settings['MusicDirectory']): log.success(f"Music directory {settings['MusicDirectory']} found") else: log.error(f"Music directory {settings['MusicDirectory']} not found") sys.exit() # Default LogsDirectory value settings['LogsDirectory'] = "./logs" # Overwrite LogsDirectory value if provided in config if 'LogsDirectory' in config: settings['LogsDirectory'] = config['Settings']['LogDirectory'] # If logs directory doesn't exist. Create it if not os.path.exists(settings['LogsDirectory']): log.warning( f"Logs directory {settings['LogsDirectory']} doesn't exist. Creating it" ) os.mkdir(settings['LogsDirectory']) trackers = [ tracker for tracker in config.sections() if tracker != "Settings" ] # List all music trackers to use for section in trackers: # Fallback name is section name name = config.get(section, 'Name', fallback=section) log.info(f"Loading {name} tracker settings") settings['Trackers'][section] = {'name': section} # Ensure config is correct if not 'URL' in config[section]: log.error( f"Section {section} is invalid. URL of the tracker must be provided" ) sys.exit() URL = config[section]['URL'] ############################################# ##### TORRENT DIRECTORY INITIALISATION ##### ############################################# torrent_directory = "./torrents/" + section if 'TorrentDirectory' in config[section]: torrent_directory = config[section]['TorrentDirectory'] # If torrent directory doesn't exists. Create it if not os.path.exists(torrent_directory): log.warning(f"{torrent_directory} doesn't exist. Creating it") os.makedirs(torrent_directory) settings['Trackers'][section]['TorrentDirectory'] = torrent_directory ############################################# ##### LOG FILES INITIALISATION ##### ############################################# settings['Trackers'][section]['logs'] = { 'FoundOn': open( os.path.join(settings['LogsDirectory'], f"FoundOn{section}.log"), "a+"), 'NotFoundOn': open( os.path.join(settings['LogsDirectory'], f"NotFoundOn{section}.log"), "a+"), 'FoundHereButNotOn': {} } settings['Trackers'][section]['IgnoreAlreadyFound'] = True if 'IgnoreAlreadyFound' in config[section]: settings['Trackers'][section][ 'IgnoreAlreadyFound'] = True if config[section][ 'IgnoreAlreadyFound'] else False settings['Trackers'][section]['logs']['FoundOn'].seek(0) to_skip[section] = [ line.strip() for line in settings['Trackers'][section]['logs'] ['FoundOn'].readlines() ] settings['Trackers'][section]['IgnoreAlreadyNotFound'] = True if 'IgnoreAlreadyNotFound' in config[section]: settings['Trackers'][section][ 'IgnoreAlreadyNotFound'] = True if config[section][ 'IgnoreAlreadyNotFound'] else False settings['Trackers'][section]['logs']['NotFoundOn'].seek(0) to_skip[section] += [ line.strip() for line in settings['Trackers'][section]['logs'] ['NotFoundOn'].readlines() ] for tracker in trackers: if tracker != section: settings['Trackers'][section]['logs']['FoundHereButNotOn'][ tracker] = open( os.path.join(settings['LogsDirectory'], f"FoundOn{section}ButNotOn{tracker}.log"), "a") ############################################# ##### API CONNECTION INITIALISATION ##### ############################################# # Set config for Throttler NumberOfRequests = 5 PerXSeconds = 10 if 'NumberOfRequests' in config[section]: NumberOfRequests = int(config[section]['NumberOfRequests']) if 'PerXSeconds' in config[section]: PerXSeconds = int(config[section]['PerXSeconds']) throttler = whatapi.Throttler(num_requests=NumberOfRequests, per_seconds=PerXSeconds) log.info( f"Setting up throttler at {NumberOfRequests} every {PerXSeconds} maximum" ) # Check wether the authentication will use API Token or username / password if 'ApiToken' in config[section]: log.info( f"{name} tracker connection will use API Token authentication") apiToken = config[section]['ApiToken'] settings['Trackers'][section]['api'] = whatapi.WhatAPI( apiKey=apiToken, server=URL, throttler=throttler) elif 'Username' in config[section] and 'Password' in config[section]: log.warning( f"{name} tracker connection will use username / password combination for authentication\nThis can lead to some errors if some endpoints are only accessible with API token. Use at your own risks" ) username = config[section]['Username'] password = config[section]['Password'] settings['Trackers'][section]['api'] = whatapi.WhatAPI( username=username, password=password, server=URL, throttler=throttler) else: log.error("Missing credentials for connection to the tracker") sys.exit() log.success(f"Successfuly connected to {name}")