-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoTwitspaceDLX.py
132 lines (114 loc) · 6.28 KB
/
AutoTwitspaceDLX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Written by ef1500
# Automatic Twitter Space Downloader
# Normal Imports
import os
import re
import json
import time
import glob
import subprocess
import asyncio
import TransferEngine as mel
import multiprocessing as choco
from multiprocessing import Pipe
import DiscordNotifEngine
import AutoSpaceEngine
import AuthEngine as kronii
import AutoSpaceEngineHandler as Yuzu # Yes I'm using waifu names for imports
from twspace_dl import Twspace
from twspace_dl import TwspaceDL
#Let's define some important variables
BASE_PATH = "D:/Spaces/" # Base Directory (MUST END WITH SLASH)
NOTIF_URL = "Your Webhook URL" # Discord Webhook url for the notification
INTERVAL = 5 # The interval for the monitor to sleep (In Seconds)
# First we want to be able to read a file that contains all of the
# Users that we would like to monitor for a twitter space. We can do this with the help of a text file
# This will also save us time when we are preparing for the discord integration to send an embed.
# Make sure that your config file is in the same place where all of your spaces are going to be downloaded.
def LoadData(file):
with open(file, 'r') as f:
data = [line.strip() for line in f if not (line.startswith('$') or line.startswith('\n'))] # Use $ As a comment, so it's a bit more detailed (if you want)
f.close()
return data
# Great. Now we've got our little "Mini" Data Loader Ready, now lets begin monitoring for a twitter space.
# If a twitter space is starting, send a discord message via webhook signaling that the specified user is now live.
# When that user is no longer live, begin uploading the file to a temporary storage site
# Once completed, Return the link via webhook (This should be replaced by the autoTorrent program to autoUpload to Holopirates)
def CheckLive_(user):
isSpace = Yuzu.CheckIfSpace_(user, kronii.GetToken(kronii.LoadTokens(BASE_PATH+'tokens.txt')))
if isSpace == False:
return False
if isinstance(isSpace, str) == True:
isLive = Yuzu.CheckIfLive_(isSpace)
if isLive == True:
xSpace = Twspace._metadata(isSpace)
spaceUrl = "**Space Url:** " + '\n' + 'https://twitter.com/i/spaces/'+xSpace['data']['audioSpace']['metadata']["rest_id"] + '\n' + '\n'
try:
spaceTitleStr = "**Space Title:** " + '\n' + xSpace['data']['audioSpace']['metadata']["title"] + '\n' + '\n'
except:
spaceTitleStr = "**Space Title:** " + '\n' + user[20:] +'\'s'+" Space" + '\n' + '\n'
spaceIDstr = "**Space ID:**" + '\n' + xSpace['data']['audioSpace']['metadata']["rest_id"] + '\n'
des = ''.join(spaceTitleStr + spaceUrl + spaceIDstr)
DiscordNotifEngine.GenerateEmbed(NOTIF_URL, " ", user[20:] + " Is hosting a twitter space!", des, user[20:], 'https://imgur.com/E2vh4aa.png', user[20:])
return True
else:
return False
async def Monitor(user, path):
# token = Yuzu.getGuest() # Let's change this so it's now only a one-time process and it's not called all the time.
# UserID = lambda user : Yuzu.GetUserID(user) # Slap the old function in a lambda, it's nicer that way!
space_id = Yuzu.CheckIfSpace_(user, kronii.GetToken(kronii.LoadTokens(BASE_PATH+'tokens.txt')))
TwitSpace = TwspaceDL(space_id, format_str="(%(creator_name)s)%(title)s-%(id)s")
isLive = CheckLive_(user) # Initial Check to define the variable
# Now we begin writing the actual monitor of the program
while isLive != True:
isLive = CheckLive_(user) # Check if the user is live (as always)
await asyncio.sleep(INTERVAL) # Now Just sleep for the specified interval before doing it again. (Should I use async here because I'm working with threads?) Fixed!
if isLive == True:
continue # Fixed!
# Open A Subprocess to begin the downloading process
monitor = subprocess.Popen("twspace_dl -o [%(creator_screen_name)s_%(id)s]-%(start_date)s -U "+user, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=path) #Open the process in the specific directory so that way we don't have any interference when uploading to tempupload.
#Monitor = multiprocessing.Process(target=TwitSpace.download, args=(path,))
#Monitor.start()
# Small note - We have to make the twsapce filename weird because if there's hangul, sometimes windows will throw an error and then the downloader will fail to pull through. I've got no idea how to fix this
# Put the output and error in their own variables (Communicate returns two values.)
ox, ex = monitor.communicate()
try:
AutoUpload(path, user) # Begin The Autoupload process
except:
pass
# Now to create the function that automatically uploads the most recent file in the folder to the tempupload service
# Let's start by making the function that finds the most recent file in a folder
def FindLatest(path, user):
lf = glob.glob(path+'/'+'*') # Path must end in /
nwf = max(lf, key=os.path.getctime)
return str(nwf) # Return The latest file
#Autoupload function
def AutoUpload(path, user):
# Find the latest file in the folder
file = FindLatest(path, user)
#Str(file) can cause an error to get thrown if the filename isn't supported by windows, and transfersh upload won't occur.
# upload = subprocess.Popen("transfersh "+str(file), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=path)
print("Upload started!")
uploadUrl = mel.transfersh_upload(str(file), None, None)
print(uploadUrl)
# print(str(oy))
# print(str(ey))
# upload.wait()
# if not ey:
# # Regex The link out
# urlReg = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
# u = urlReg.search(str(oy))
# ux = str(u.group())
#
# # Create Strings for the embed
# uploadUrl = ux[:-6]
up_user = user[20:]
#
# Define some extra strings and then construct the embed
upStr = "**Upload URL:**" + '\n' + uploadUrl + '\n' + '\n'
noteStr = "*Note: All links will expire after 300 hours*" + '\n'
des = ''.join(upStr + noteStr)
# Notify Via Discord
DiscordNotifEngine.GenerateEmbed(NOTIF_URL, " ", up_user + "'s Twitter Space is Ready for download", des, up_user, "https://imgur.com/nGQWo3C.png", user[20:])
# else:
# pass