Last active
January 18, 2022 19:38
-
-
Save xthesaintx/5ae1f614d2a4e66c9788d8868d02f265 to your computer and use it in GitHub Desktop.
Headless Raspberry Pi Python script to grab torrent files from a Google Drive Folder and load them into qbittorrent. Uses PyDrive2 GetContentIOBuffer() to aviod downloading a torrent file. Logs the files grabbed into a log file and places that on the GDrive folder as well as locally, also logs the contents (only 1 day old) of a local folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
## | |
## Requires pydrive2, qbittorrent-api and setting up Google API | |
## client_secrets.json put in the same folder as script | |
## Must be run on a machine with a browser first to generate mycreds.txt | |
## | |
import os | |
import requests | |
import filecmp | |
import shutil | |
import datetime | |
from pydrive2.auth import GoogleAuth | |
from pydrive2.drive import GoogleDrive | |
import qbittorrentapi | |
## DEFINE VARS ## | |
qbit_ip = "http://localhost:8080/" | |
gdrive_parent_folder ='DIRECTORY ID' | |
gdrive_done_folder ='DIRECTORY ID' | |
local_log ='DIRECTORY' | |
local_media_directory = 'DIRECTORY' | |
today = datetime.datetime.now() | |
dtime = today.strftime("%a, %d %b %Y - %H:%M:%S") | |
### QBIT WEB UI ### | |
# instantiate a Client using the appropriate WebUI configuration | |
qbt_client = qbittorrentapi.Client(host=qbit_ip, username='admin', password='adminadmin') | |
try: | |
qbt_client.auth_log_in() | |
except qbittorrentapi.LoginFailed as e: | |
print(e) | |
def add_torrent (file_in): | |
adding = qbt_client.torrents_add(torrent_files=file_in) | |
### OAUTH ### | |
gauth = GoogleAuth() | |
# Try to load saved client credentials | |
if os.path.exists("mycreds.txt"): | |
gauth.LoadCredentialsFile("mycreds.txt") | |
if gauth.credentials is None: | |
# Authenticate if they're not there | |
gauth.LocalWebserverAuth() | |
elif gauth.access_token_expired: | |
# Refresh them if expired | |
gauth.Refresh() | |
else: | |
# Initialize the saved creds | |
gauth.Authorize() | |
# Save the current credentials to a file | |
gauth.SaveCredentialsFile("mycreds.txt") | |
drive = GoogleDrive(gauth) | |
## FUNCTIONS ## | |
# CONVERT LIST TO STR with NEWLINES | |
def list_to_string (list_in): | |
string_out = "\n".join(list_in) | |
return string_out | |
#returns a list of file in a GDRIVE dir (id) with in title file | |
def get_list_gdrive (directory,file_type): | |
file_list = drive.ListFile({'q': "'"+directory+"' in parents and trashed=false and title contains '"+file_type+"'"}).GetList() | |
return file_list | |
def move_file_gdrive (file_in,destination): | |
file3 = drive.CreateFile({'id': file_in['id']}) | |
file3.Upload() | |
file3['parents'] = [{"kind": "drive#parentReference", "id": destination}] | |
file3.Upload() | |
def process_list (keyword,search_directory,completed_directory): | |
files_list = get_list_gdrive (search_directory, keyword) | |
files_log = [] | |
for file1 in files_list: | |
files_log.append(file1['title']) | |
file_tmp = drive.CreateFile({'id': file1['id']}) | |
content = file_tmp.GetContentIOBuffer() | |
add_torrent(content) | |
move_file_gdrive (file1,completed_directory) | |
return files_log | |
def log_gdrive_upload (log_file,destination): | |
## UPLOADING THE LOG ## | |
# checks GDrive for exsisting logs or duplicates and moves to trash | |
file_list = drive.ListFile({'q': "'"+destination+"' in parents and trashed=false and title ='"+log_file+"' "}).GetList() | |
# overwrites the exsisting log if it is the only one | |
if len(file_list) == 1: | |
for f in file_list: | |
file5 = drive.CreateFile({'id': f['id']}) | |
file5.SetContentFile(log_file) | |
file5.Upload() | |
# If the file does not exsist or there are multiple copies because something broke, delete everything and make a new one | |
if len(file_list) == 0 or len(file_list) > 1: | |
for f in file_list: | |
file6 = drive.CreateFile({'id': f['id']}) | |
file6.Trash() | |
# uploads the log to gdrive | |
file7 = drive.CreateFile() | |
file7['parents'] = [{"kind": "drive#parentReference", "id": destination}] | |
file7.SetContentFile(log_file) | |
file7.Upload() | |
def log_local_output (log_file,log_text,mode): | |
#logs to drive file | |
# Opens log file and writes contents of the watch folder and time | |
logfile = open (log_file,mode) | |
logfile.write(log_text) | |
logfile.close() | |
## MAIN CODE ## | |
# Auto-iterate through all files in the root folder. | |
torrent_list = process_list (".torrent",gdrive_parent_folder,gdrive_done_folder) | |
#output last days worth of media to a file | |
stream = os.popen('find '+local_media_directory+' -maxdepth 1 -type d -mtime -7 -printf "%f\n"') | |
local_media_list = stream.read() | |
log_string = dtime+"\n\n-- GDRIVE --\n-- .torrent --\n "+list_to_string(torrent_list)+"\n\n-- MEDIA UPDATED TODAY --\n"+local_media_list | |
log_local_output("drop.log",log_string,"w") | |
log_gdrive_upload ("drop.log",gdrive_parent_folder) | |
# Below just puts the log in another location defined by local_log | |
# log_local_output(local_log+"drop.log",log_string,"w") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment