def main(): login = input('Enter vk.com username: '******'Enter vk.com password: '******'3742196') res = vk.login(login, password) if not res: print('Incorrect login or password') exit() if not vk.oauth(): print("Can't get access token") exit() cnf = configparser.ConfigParser() cnf['main'] = { 'token': vk.token, 'uids': vk.my_uid, } savePath = '{}/Music'.format(os.getenv('HOME')) d = input('Directory for saving audios [{}]: '.format(savePath)) if d: savePath = d cnf['main']['path'] = savePath if not os.path.exists(savePath): os.makedirs(savePath) res = input('Do you want to add another uids except {}? [y/n]: '.format( vk.my_uid)) if res.lower() == 'y': ids = input('Enter uids seperate by comma: ') cnf['main']['uids'] += ',' + ids.strip() with open('config.ini', 'w') as f: cnf.write(f) return 0
class VkDaemon(daemon): client_id = ''#сюда client_id созданного приложения def __init__(self, pid): try: self.log_f = open('/var/log/vk_audio.log', 'a') except Exception as e: print("Can't open log file: {}".format(e)) return super().__init__(pid) def __del__(self): try: self.log_f.close() except: pass def log(self, text): try: self.log_f.write('{} {}\n'.format(time.strftime('%d.%m.%Y %H:%M:%S'), text)) f.flush() except: pass def start(self): self.cnf = configparser.ConfigParser() try: self.cnf.read('config.ini') except: self.log('не смогли открыть файл конфига') return return super().start() def run(self): if 'main' in self.cnf and 'token' in self.cnf['main']: self.vk = Vk(self.client_id, self.cnf['main']['token']) else: self.log('не задан токен в конфиге, запустите config.py') return try: uids = [x.strip() for x in self.cnf['main']['uids'].split(',')] except: uids = [''] try: path = self.cnf['main']['path'] except: path = '{}/music'.format(os.getenv('HOME')) if not os.path.exists(path): os.makedirs(path) if not self.vk.oauth(): self.log('не смогли авторизоваться с указанным токеном, запустите config.py') return delay = 30 if 'main' in self.cnf and 'delay' in self.cnf['main']: try: delay = int(self.cnf['main']['delay']) except: pass while True: for uid in uids: audios = self.vk.get_audios(uid) if not audios: continue for a in audios['response']: fname = '{}/{}_{}.mp3'.format(path, a['owner_id'], a['aid']) if os.path.exists(fname): continue self.log('загружаем аудио {} - {}'.format(a['artist'], a['title'])) try: r = self.vk.request.get(a['url']) with open(fname, 'wb') as f: f.write(r.content) tag = stagger.tags.Tag23() tag.artist = a['artist'] tag.title = a['title'] tag.write(fname) except Exception as e: self.log('не смогли загузить {}: {}'.format(a['url'], e)) time.sleep(delay)