11from .constants import *
22from .logging import Log
3+ from .exceptions import *
34
45from pathlib import Path
56from typing import Tuple , Optional
@@ -35,6 +36,24 @@ def __init__(
3536
3637 self .log .debug ("Firefox is now running" )
3738
39+ def click (self , element ):
40+ element .click ()
41+ sleep (self .timeout )
42+ return element
43+
44+ def send (self , element , text : str ) -> None :
45+ element .clear ()
46+ sleep (self .timeout )
47+ element .send_keys (text )
48+ sleep (self .timeout )
49+
50+ def click_next (self , modal ) -> None :
51+ modal .find_element_by_id (NEXT_BUTTON ).click ()
52+ sleep (self .timeout )
53+
54+ def not_uploaded (self , modal ) -> bool :
55+ return modal .find_element_by_xpath (STATUS_CONTAINER ).text .find (UPLOADED ) != - 1
56+
3857 def upload (
3958 self ,
4059 file : str ,
@@ -60,44 +79,39 @@ def upload(
6079 modal = self .driver .find_element_by_css_selector (UPLOAD_DIALOG_MODAL )
6180 self .log .debug ("Found YouTube upload Dialog Modal" )
6281
63- if title :
64- if len (title ) <= 100 :
65- self .log .debug (f'Trying to set "{ title } " as title...' )
66- title_field = modal .find_element_by_id (TEXTBOX )
67- title_field .click ()
68- sleep (self .timeout )
69-
70- # clearing out title which defaults to filename
71- for i in range (len (title_field .text ) + 10 ):
72- title_field .send_keys (Keys .BACKSPACE )
73- sleep (0.1 )
74-
75- sleep (self .timeout )
76- title_field .send_keys (title )
77- sleep (self .timeout )
78- else :
79- self .log .debug (
80- "Did not set title. Title cannot be longer than 100 characters"
81- )
82+ self .log .debug (f'Trying to set "{ title } " as title...' )
83+
84+ # TITLE
85+ title_field = self .click (modal .find_element_by_id (TEXTBOX ))
86+
87+ # get file name (default) title
88+ title = title if title else title_field .text
89+
90+ if len (title ) > TITLE_COUNTER :
91+ raise ExceedsCharactersAllowed (
92+ f"Title was not set due to exceeding the maximum allowed characters ({ len (title )} /{ TITLE_COUNTER } )"
93+ )
94+
95+ # clearing out title which defaults to filename
96+ for i in range (len (title_field .text ) + 10 ):
97+ # more backspaces than needed just to be sure
98+ title_field .send_keys (Keys .BACKSPACE )
99+ sleep (0.1 )
100+
101+ self .send (title_field , title )
82102
83103 if description :
84- if len (description ) <= 5000 :
85- self .log .debug (f'Trying to set "{ description } " as description...' )
86- container = modal .find_element_by_xpath (DESCRIPTION_CONTAINER )
87- description_field = container .find_element_by_id (TEXTBOX )
88- description_field .click ()
89- sleep (self .timeout )
90-
91- description_field .clear ()
92- sleep (self .timeout )
93-
94- description_field .send_keys (description )
95- sleep (self .timeout )
96- else :
97- self .log .debug (
98- "Did not set description. Description cannot be longer than 5000 characters"
104+ if len (description ) > DESCRIPTION_COUNTER :
105+ raise ExceedsCharactersAllowed (
106+ f"Description was not set due to exceeding the maximum allowed characters ({ len (description )} /{ DESCRIPTION_COUNTER } )"
99107 )
100108
109+ self .log .debug (f'Trying to set "{ description } " as description...' )
110+ container = modal .find_element_by_xpath (DESCRIPTION_CONTAINER )
111+ description_field = self .click (container .find_element_by_id (TEXTBOX ))
112+
113+ self .send (description_field , description )
114+
101115 if thumbnail :
102116 self .log .debug (f'Trying to set "{ thumbnail } " as thumbnail...' )
103117 modal .find_element_by_xpath (INPUT_FILE_THUMBNAIL ).send_keys (
@@ -111,64 +125,44 @@ def upload(
111125 sleep (self .timeout )
112126
113127 if tags :
114- modal .find_element_by_xpath (MORE_OPTIONS_CONTAINER ).click ()
115- sleep (self .timeout )
128+ self .click (modal .find_element_by_xpath (MORE_OPTIONS_CONTAINER ))
116129
117130 tags = "," .join (str (tag ) for tag in tags )
118131
119- if len (tags ) <= 500 :
120- self .log .debug (f'Trying to set "{ tags } " as tags...' )
121- container = modal .find_element_by_xpath (TAGS_CONTAINER )
122- tags_field = container .find_element_by_id (TEXT_INPUT )
123- tags_field .click ()
124- sleep (self .timeout )
125-
126- tags_field .clear ()
127- sleep (self .timeout )
128-
129- tags_field .send_keys (tags )
130- sleep (self .timeout )
131- else :
132- self .log .debug (
133- "Did not set tags. Tags cannot be longer than 500 characters"
132+ if len (tags ) > TAGS_COUNTER :
133+ raise ExceedsCharactersAllowed (
134+ f"Tags were not set due to exceeding the maximum allowed characters ({ len (tags )} /{ TAGS_COUNTER } )"
134135 )
135136
136- modal .find_element_by_id (NEXT_BUTTON ).click ()
137- sleep (self .timeout )
138-
139- modal .find_element_by_id (NEXT_BUTTON ).click ()
140- sleep (self .timeout )
137+ self .log .debug (f'Trying to set "{ tags } " as tags...' )
138+ container = modal .find_element_by_xpath (TAGS_CONTAINER )
139+ tags_field = self .click (container .find_element_by_id (TEXT_INPUT ))
140+ self .send (tags_field , tags )
141141
142142 # sometimes you have 4 tabs instead of 3
143143 # this handles both cases
144- try :
145- modal . find_element_by_id ( NEXT_BUTTON ). click ()
146- sleep ( self .timeout )
147- except :
148- pass
144+ for _ in range ( 3 ) :
145+ try :
146+ self .click_next ( modal )
147+ except :
148+ pass
149149
150150 self .log .debug ("Trying to set video visibility to public..." )
151151 public_main_button = modal .find_element_by_name (PUBLIC_BUTTON )
152152 public_main_button .find_element_by_id (RADIO_LABEL ).click ()
153153 video_id = self .get_video_id (modal )
154154
155- status_container = modal .find_element_by_xpath (STATUS_CONTAINER )
156-
157- while True :
158- in_process = status_container .text .find (UPLOADED ) != - 1
159- if in_process :
160- sleep (self .timeout )
161- else :
162- break
155+ while self .not_uploaded (modal ):
156+ self .log .debug ("Still uploading..." )
157+ sleep (1 )
163158
164159 done_button = modal .find_element_by_id (DONE_BUTTON )
165160
166161 if done_button .get_attribute ("aria-disabled" ) == "true" :
167162 error_message = self .driver .find_element_by_xpath (ERROR_CONTAINER ).text
168163 return False , None
169164
170- done_button .click ()
171- sleep (self .timeout )
165+ self .click (done_button )
172166
173167 return True , video_id
174168
@@ -182,8 +176,7 @@ def get_video_id(self, modal) -> Optional[str]:
182176
183177 video_id = video_url_element .get_attribute (HREF ).split ("/" )[- 1 ]
184178 except :
185- self .log .debug ("Exception getting video ID" )
186- pass
179+ raise VideoIDError ("Could not get video ID" )
187180
188181 return video_id
189182
0 commit comments