├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── megamanager
├── __init__.py
├── __main__.py
├── __version__.py
├── account.py
├── libs
│ ├── __init__.py
│ ├── compress_images_lib.py
│ ├── ffmpeg_lib.py
│ ├── lib.py
│ └── mega_tools_lib.py
├── mega_manager.py
├── path_mapping.py
├── syncprofile.py
└── tools
│ ├── __init__.py
│ └── compressImages
│ ├── README.md
│ ├── __init__.py
│ └── compressImages.py
├── requirements.txt
└── setup.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | env/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | wheels/
24 | =======
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .coverage
43 | .coverage.*
44 | .cache
45 | nosetests.xml
46 | coverage.xml
47 | *.cover
48 | =======
49 | *,cover
50 | .hypothesis/
51 |
52 | # Translations
53 | *.mo
54 | *.pot
55 |
56 | # Django stuff:
57 | *.log
58 | local_settings.py
59 |
60 | # Flask stuff:
61 | instance/
62 | .webassets-cache
63 |
64 | # Scrapy stuff:
65 | .scrapy
66 |
67 | # Sphinx documentation
68 | docs/_build/
69 |
70 | # PyBuilder
71 | target/
72 |
73 | # Jupyter Notebook
74 | =======
75 | # IPython Notebook
76 | .ipynb_checkpoints
77 |
78 | # pyenv
79 | .python-version
80 |
81 | # celery beat schedule file
82 | celerybeat-schedule
83 |
84 | # SageMath parsed files
85 | *.sage.py
86 |
87 | =======
88 | # dotenv
89 | .env
90 |
91 | # virtualenv
92 | .venv
93 | =======
94 | venv_old/
95 | ENV/
96 |
97 | # Spyder project settings
98 | .spyderproject
99 | .spyproject
100 | =======
101 |
102 | # Rope project settings
103 | .ropeproject
104 |
105 | # mkdocs documentation
106 | /site
107 |
108 | # mypy
109 | .mypy_cache/
110 |
111 | $ szmania
112 | *.cfg
113 | .idea/
114 | *.log
115 | *.tmp
116 | *.npz
117 | *.old
118 | *.ini
119 | testdata/
120 | megamanager/tools/megaTools_1_1_98
121 | megamanager/tools/
122 | megamanager/tools/gmail
123 |
124 |
125 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | ## Version 0.3.0 (2024-11-27)
4 | ### Bugs
5 | - Fixed various bugs.
6 |
7 |
8 | ## Version 0.3.0 (2024-01-03)
9 | ### Bugs
10 | - Fixed bug with reversed nice levels for ffmpeg process, for linux systems.
11 |
12 | ## Version 0.2.1 (2023-10-25)
13 | ### Bugs
14 | - Changed to 10 seconds between searches for finished threads
15 |
16 | ## Version 0.2.0 (2023-10-25)
17 | ### Bugs
18 | - Fixed logging handlers
19 | - Removed hardcoded timeout for waiting for threads
20 |
21 |
22 | ## Version 0.0.4 (2023-04-08)
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | ## Version 0.0.3 (2023-01-01)
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or solve
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to solve it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) solve the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to solve the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, solve and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | solve a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to solve, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise solve, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MEGA Manager
2 | Cloud syncing manager for multiple MEGA cloud storage accounts with syncing, data gathering, compresssion and optimization capabilities.
3 |
4 | MEGA Manager allows MEGA users to manage and maintain multiple MEGA accounts all in one place! MEGA Manager is the only app that allows syncing capabilities on multiple MEGA accounts. Included in MEGA Manager are in-depth syncing capabilities, file optimization and compression, remote and local account data output, a Command Line Interface, and the ability to sync multiple remote locations to multiple local locations in one account.
5 |
6 | * Allows users to manage multiple MEGA accounts all in one place
7 | * Syncing capabilites that allows for syncing several remote and local file locations in the same account
8 | * Optimization and compression capabilities for video and image file formats
9 | * Output remote and local file data for each account
10 | * Command Line Interface (CLI) for all the above features
11 |
12 | #### A little about MEGA.nz...
13 | ##### https://mega.nz/
14 | MEGA makes secure cloud storage simple. Create an account and get 50 GB free on MEGA's end-to-end encrypted cloud collaboration platform today!
15 |
16 |
17 |
18 |
19 | ## Usage
20 | ### Arguments
21 | `--compress`
22 |
23 | Compresses all images AND video files in local account locations.
24 |
25 | `--compress-images`
26 |
27 | Compresses all images in local account locations.
28 |
29 | `--compress-videos`
30 |
31 | Compresses all videos in local account locations.
32 |
33 | `--config `
34 |
35 | Set MEGA Manager config file location. Default: "\/.mega_manager/mega_manager.cfg".
36 |
37 | `--download`
38 |
39 | Download from MEGA account remote locations to corresponding local locations.
40 |
41 | `--download-speed `
42 |
43 | Set total download speed limit in Kb.
44 |
45 | `--log `
46 |
47 | Set log level. ie: "INFO", "WARN", "DEBUG", etc... Default: "INFO".
48 |
49 | `--profile-output-data`
50 |
51 | This will output all profile/account data to standard output.
52 |
53 | `--remove-oldest-file-version`
54 |
55 | This will remove outdated files locally or remotely that are older than their local/remote counterpart (syncing action).
56 |
57 | `--remove-remote`
58 |
59 | If set remote files that have no corresponding local file will be removed.
60 |
61 | `--sync`
62 |
63 | If true, local and remote files for accounts will be synced. Equivalent to using arguments "--download", "--remove_local",
64 | "--remote-outdated", "--remove_remote" and "--upload" all at once.
65 |
66 | `--upload`
67 |
68 | If set files will be uploaded to MEGA account.
69 |
70 | `--upload-speed `
71 |
72 | Set total upload speed limit in Kb.
73 |
74 |
75 | ### Examples
76 |
77 | Calling the package directly will suffice. Otherwise one could call "megamanger\__main__.py"
78 |
79 | `megamanager --upload --up-speed 500`
80 |
81 | Upload files AND limit total upload speed to 500kb.
82 |
83 | `megamanager\__main__.py --sync --down-speed 750`
84 |
85 | Sync files AND limit total download speed to 750kb.
86 |
87 | `megamanager --remove-oldest-file-version --config "dir\megamanager.cfg"`
88 |
89 | Set config file to be "dir\mega_manger.cfg" AND remove outdated local and remote files
90 |
91 |
92 |
93 | ### MEGA Manager Config File Format
94 | (by default lives in \/.mega_manager/mega_manger.cfg, unless specified otherwise)
95 | Command line arguments override corresponding config values. (ie: --compress via cli will override "COMPRESS_ALL=False" in config file)
96 |
97 | ```
98 | [ACTIONS]
99 | COMPRESS_ALL=True
100 | COMPRESS_IMAGES=False
101 | COMPRESS_VIDEOS=False
102 | DOWNLOAD=False
103 | UPLOAD=False
104 | SYNC=True
105 | LOCAL_IS_TRUTH=True
106 |
107 | [PROPERTIES]
108 | LOG_LEVEL="DEBUG"
109 | LOG_RETENTION="midnight"
110 | LOG_RETNETION_BACKUP_COUNT=5
111 | MEGA_MANAGER_CONFIG_DIR_PATH="{HOME_DIRECTORY}{sep}.mega_manager"
112 | MEGA_MANAGER_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}logs{sep}mega_manager_log.log"
113 | MEGA_MANAGER_CONFIG_DIR_DATA_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}data"
114 | MEGA_MANAGER_STDOUT_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}logs{sep}mega_stdout.log"
115 | MEGA_MANAGER_STDERR_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}logs{sep}mega_stderr.log"
116 | MEGA_MANAGER_OUTPUT_PROFILE_DATA_PATH=""
117 | SLEEP_TIME_BETWEEN_RUNS_SECONDS=300
118 | REMOVE_OLDEST_FILE_VERSION=False
119 | PROCESS_SET_PRIORITY_TIMEOUT=60
120 |
121 | [IMAGE_COMPRESSION]
122 | COMPRESSED_IMAGES_FILE_PATH ="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}{sep}compressed_images.npy"
123 | UNABLE_TO_COMPRESS_IMAGES_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}{sep}unable_to_compress_images.npy"
124 | COMPRESSION_IMAGE_EXTENSIONS=["jpg","jpeg","png"]
125 | IMAGE_TEMP_FILE_EXTENSIONS=["compressimages-backup", "unoptimized", "tmp"]
126 | COMPRESSION_JPEG_QUALITY_PERCENTAGE=60
127 |
128 | [VIDEO_COMPRESSION]
129 | COMPRESSED_VIDEOS_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}{sep}compressed_videos.npy"
130 | UNABLE_TO_COMPRESS_VIDEOS_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}{sep}unable_to_compress_videos.npy"
131 | COMPRESSION_VIDEO_EXTENSIONS=["avi","flv","m4v","mkv","mp4","mpeg","mpg","wmv"]
132 | COMPRESSION_FFMPEG_VIDEO_PRESET="slow"
133 | FFMPEG_PROCESS_PRIORITY_CLASS="HIGH_PRIORITY_CLASS"
134 | FFMPEG_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}logs{sep}ffmpeg.log"
135 | FFMPEG_THREADS=4
136 |
137 | [REMOTE]
138 | REMOVED_REMOTE_FILES_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}{sep}removed_remote_files.npy"
139 |
140 | [MEGATOOLS]
141 | MEGATOOLS_PROCESS_PRIORITY_CLASS="HIGH_PRIORITY_CLASS"
142 | MEGATOOLS_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}{sep}logs{sep}mega_tools.log"
143 |
144 | [MEGA]
145 | MEGA_DOWNLOAD_SPEED=200
146 | MEGA_UPLOAD_SPEED=200
147 |
148 | [PROFILE_0]
149 | profile_name=Pictures - email@email.com
150 | username=email@email.com
151 | password=mypassword
152 | local_path_0=/mnt/sda1/pictures
153 | remote_path_0=/Root/pictures
154 |
155 | [PROFILE_1]
156 | profile_name=Videos & Games - email2@email.com
157 | username=email2@email.com
158 | password=mypassword2
159 | local_path_0=/mnt/sda1/videos
160 | remote_path_0=/Root/videos
161 | local_path_1=/mnt/sda1/games
162 | remote_path_1=/Root/games
163 | ```
164 |
165 | Paths are now operating system agnostic (eg: can process both `\\` and `/`).
166 | Example:
167 |
168 | ```
169 | [ACTIONS]
170 | COMPRESS_ALL=True
171 | COMPRESS_IMAGES=False
172 | COMPRESS_VIDEOS=False
173 | DOWNLOAD=False
174 | UPLOAD=False
175 | SYNC=True
176 | LOCAL_IS_TRUTH=True
177 |
178 | [PROPERTIES]
179 | LOG_LEVEL="DEBUG"
180 | LOG_RETENTION="midnight"
181 | LOG_RETNETION_BACKUP_COUNT=5
182 | MEGA_MANAGER_CONFIG_DIR_PATH="{HOME_DIRECTORY}/.mega_manager"
183 | MEGA_MANAGER_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/logs/mega_manager_log.log"
184 | MEGA_MANAGER_CONFIG_DIR_DATA_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/data"
185 | MEGA_MANAGER_STDOUT_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/logs/mega_stdout.log"
186 | MEGA_MANAGER_STDERR_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/logs/mega_stderr.log"
187 | MEGA_MANAGER_OUTPUT_PROFILE_DATA_PATH=""
188 | SLEEP_TIME_BETWEEN_RUNS_SECONDS=300
189 | REMOVE_OLDEST_FILE_VERSION=False
190 | PROCESS_SET_PRIORITY_TIMEOUT=60
191 |
192 | [IMAGE_COMPRESSION]
193 | COMPRESSED_IMAGES_FILE_PATH ="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}/compressed_images.npy"
194 | UNABLE_TO_COMPRESS_IMAGES_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}/unable_to_compress_images.npy"
195 | COMPRESSION_IMAGE_EXTENSIONS=["jpg","jpeg","png"]
196 | IMAGE_TEMP_FILE_EXTENSIONS=["compressimages-backup", "unoptimized", "tmp"]
197 | COMPRESSION_JPEG_QUALITY_PERCENTAGE=60
198 |
199 | [VIDEO_COMPRESSION]
200 | COMPRESSED_VIDEOS_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}/compressed_videos.npy"
201 | UNABLE_TO_COMPRESS_VIDEOS_FILE_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}/unable_to_compress_videos.npy"
202 | COMPRESSION_VIDEO_EXTENSIONS=["avi","flv","m4v","mkv","mp4","mpeg","mpg","wmv"]
203 | COMPRESSION_FFMPEG_VIDEO_PRESET="slow"
204 | FFMPEG_PROCESS_PRIORITY_CLASS="HIGH_PRIORITY_CLASS"
205 | FFMPEG_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/logs/ffmpeg.log"
206 | FFMPEG_THREADS=4
207 |
208 | [REMOTE]
209 | REMOVED_REMOTE_FILES_PATH="{MEGA_MANAGER_CONFIG_DIR_DATA_PATH}/removed_remote_files.npy"
210 |
211 | [MEGATOOLS]
212 | MEGATOOLS_PROCESS_PRIORITY_CLASS="HIGH_PRIORITY_CLASS"
213 | MEGATOOLS_LOG_PATH="{MEGA_MANAGER_CONFIG_DIR_PATH}/logs/mega_tools.log"
214 |
215 | [MEGA]
216 | MEGA_DOWNLOAD_SPEED=200
217 | MEGA_UPLOAD_SPEED=200
218 |
219 | [PROFILE_0]
220 | profile_name=Pictures - email@email.com
221 | username=email@email.com
222 | password=mypassword
223 | local_path_0=/mnt/sda1/pictures
224 | remote_path_0=/Root/pictures
225 |
226 | [PROFILE_1]
227 | profile_name=Videos & Games - email2@email.com
228 | username=email2@email.com
229 | password=mypassword2
230 | local_path_0=/mnt/sda1/videos
231 | remote_path_0=/Root/videos
232 | local_path_1=/mnt/sda1/games
233 | remote_path_1=/Root/games
234 | ```
235 |
236 |
237 |
238 |
--------------------------------------------------------------------------------
/megamanager/__init__.py:
--------------------------------------------------------------------------------
1 | from .account import Account
2 | from .mega_manager import MegaManager
3 | from .path_mapping import PathMapping
4 | from .syncprofile import SyncProfile
5 | from .__version__ import *
6 |
--------------------------------------------------------------------------------
/megamanager/__main__.py:
--------------------------------------------------------------------------------
1 | from argparse import ArgumentParser
2 | from mega_manager import MegaManager
3 | from os import path, sep
4 |
5 | HOME_DIRECTORY = path.expanduser("~")
6 | MEGA_MANAGER_CONFIG_DIR = path.join("{HOME_DIRECTORY}".format(HOME_DIRECTORY=HOME_DIRECTORY), ".mega_manager")
7 | MEGA_MANAGER_CONFIG_FILE_PATH = path.join("{MEGA_MANAGER_CONFIG_DIR}".format(
8 | MEGA_MANAGER_CONFIG_DIR=MEGA_MANAGER_CONFIG_DIR), "mega_manager.cfg")
9 |
10 |
11 | def get_args():
12 | """
13 | Get arguments from command line, and returns them as dictionary.
14 |
15 | Returns:
16 | Dictionary: Dictionary of arguments for MEGA Manager.
17 | """
18 |
19 | parser = ArgumentParser(description='MEGA Manager is a MEGA cloud storage management and optimization application.')
20 |
21 | parser.add_argument('--compress', dest='compress_all', action='store_true', default=False,
22 | help='If true, this will compress local image and video files.')
23 |
24 | parser.add_argument('--compress-images', dest='compress_images', action='store_true', default=False,
25 | help='If true, this will compress local image files.')
26 |
27 | parser.add_argument('--compress-videos', dest='compress_videos', action='store_true', default=False,
28 | help='If true, this will compress local video files.')
29 |
30 | parser.add_argument('--config', dest='mega_manager_config_path', default=MEGA_MANAGER_CONFIG_FILE_PATH,
31 | help='Set MEGA Manager config file location. Default: "{}"'.format(MEGA_MANAGER_CONFIG_FILE_PATH))
32 |
33 | parser.add_argument('--download', dest='download', action='store_true', default=False,
34 | help='If true, items will be downloaded from MEGA')
35 |
36 | parser.add_argument('--download-speed', dest='mega_download_speed', type=int, default=None,
37 | help='Total download speed limit in kilobytes.')
38 |
39 | parser.add_argument('--log', dest='log_level', help='Set logging level')
40 |
41 | parser.add_argument('--profile-output-data', dest='mega_manager_output_profile_data_path', action='store_true', default=False,
42 | help='If true, this will output all profile data to standard output.')
43 |
44 | parser.add_argument('--remove-oldest-file-version', dest='remove_oldest_file_version', action='store_true', default=False,
45 | help='If true, this will remove outdated files locally or remotely that are older than their '
46 | 'local/remote counterpart (syncing action).')
47 |
48 | parser.add_argument('--remove-remote', dest='remove_remote', action='store_true', default=False,
49 | help='If true, this will allow for remote files to be removed if no corresponding '
50 | 'local file exists.')
51 |
52 | parser.add_argument('--sync', dest='sync', action='store_true', default=False,
53 | help='If true, local and remote files for accoutns will be synced. Equivalent to '
54 | 'using arguments "--download", "--removeLocal", "--removeRemote" and "--upload" '
55 | 'all at once.')
56 |
57 | parser.add_argument('--upload', dest='upload', action='store_true', default=False,
58 | help='If true, items will be uploaded to MEGA')
59 |
60 | parser.add_argument('--upload-speed', dest='mega_upload_speed', type=int, default=None,
61 | help='Total upload speed limit in kilobytes.')
62 |
63 | args = parser.parse_args()
64 | return args.__dict__
65 |
66 |
67 | def main():
68 | kwargs = get_args()
69 | mega = MegaManager(**kwargs)
70 | mega.run()
71 |
72 |
73 | if __name__ == "__main__":
74 | main()
75 |
--------------------------------------------------------------------------------
/megamanager/__version__.py:
--------------------------------------------------------------------------------
1 | __version__ = "1.2.0"
2 |
--------------------------------------------------------------------------------
/megamanager/account.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 7/2/2017
4 | # Initial Creation.
5 | # MEGA account class. Used for MEGA account details.
6 | ###
7 |
8 | from logging import getLogger
9 | from libs.lib import Lib
10 |
11 | __author__ = 'szmania'
12 |
13 |
14 | class Account(object):
15 | def __init__(self, username, password, log_level='DEBUG'):
16 | """
17 | Library for ffmpeg converter and encoder interaction.
18 |
19 | Args:
20 | username (str): MEGA account user name
21 | password (str): MEGA account password
22 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
23 | """
24 | self.__username = username
25 | self.__password = password
26 | self.__log_level = log_level
27 |
28 | self.__freeSpace = None
29 | self.__totalSpace = None
30 | self.__usedSpace = None
31 |
32 | self.__lib = Lib(log_level=log_level)
33 |
34 | @property
35 | def freeSpace(self):
36 | """
37 | Getter for MEGA account free space.
38 |
39 | Returns:
40 | String: Returns MEGA account free space
41 | """
42 | logger = getLogger('Account.free_space')
43 | logger.setLevel(self.__log_level)
44 | return self.__freeSpace
45 |
46 | @freeSpace.setter
47 | def freeSpace(self, value):
48 | """
49 | Setter for MEGA account free space.
50 |
51 | Args:
52 | value (str): value to set account free space to.
53 | """
54 | logger = getLogger('Account.free_space')
55 | logger.setLevel(self.__log_level)
56 | self.__freeSpace = value
57 |
58 | @property
59 | def totalSpace(self):
60 | """
61 | Getter for MEGA account total space.
62 |
63 | Returns:
64 | String: Returns MEGA account total space
65 | """
66 | logger = getLogger('Account.total_space')
67 | logger.setLevel(self.__log_level)
68 | return self.__totalSpace
69 |
70 | @totalSpace.setter
71 | def totalSpace(self, value):
72 | """
73 | Setter for MEGA account free space.
74 |
75 | Args:
76 | value (str): value to set account total space to.
77 | """
78 | logger = getLogger('Account.total_space')
79 | logger.setLevel(self.__log_level)
80 | self.__totalSpace = value
81 |
82 |
83 | @property
84 | def usedSpace(self):
85 | """
86 | Getter for MEGA account used space.
87 |
88 | Returns:
89 | String: Returns MEGA account used space
90 | """
91 | logger = getLogger('Account.used_space')
92 | logger.setLevel(self.__log_level)
93 | return self.__usedSpace
94 |
95 | @usedSpace.setter
96 | def usedSpace(self, value):
97 | """
98 | Setter for MEGA account used space.
99 |
100 | Args:
101 | value (str): value to set account used space to.
102 | """
103 | logger = getLogger('Account.used_space')
104 | logger.setLevel(self.__log_level)
105 | self.__usedSpace = value
106 |
107 | @property
108 | def password(self):
109 | """
110 | Getter for MEGA account password.
111 |
112 | Returns:
113 | String: Returns MEGA account password
114 | """
115 | logger = getLogger('Account.password')
116 | logger.setLevel(self.__log_level)
117 | return self.__password
118 |
119 | @password.setter
120 | def password(self, value):
121 | """
122 | Setter for MEGA account password.
123 |
124 | Args:
125 | value (str): value to set password to.
126 | """
127 | logger = getLogger('Account.password')
128 | logger.setLevel(self.__log_level)
129 | self.__password = value
130 |
131 | @property
132 | def username(self):
133 | """
134 | Getter for MEGA account username.
135 |
136 | Returns:
137 | String: Returns MEGA account username
138 | """
139 | logger = getLogger('Account.username')
140 | logger.setLevel(self.__log_level)
141 | return self.__username
142 |
143 | @username.setter
144 | def username(self, value):
145 | """
146 | Setter for MEGA account username.
147 |
148 | Args:
149 | value (str): value to set username to.
150 | """
151 | logger = getLogger('Account.username')
152 | logger.setLevel(self.__log_level)
153 | self.__username = value
154 |
155 |
--------------------------------------------------------------------------------
/megamanager/libs/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/szmania/mega_manager/3bd7ec5f5b90e758002fe2213d07a007edad7ae4/megamanager/libs/__init__.py
--------------------------------------------------------------------------------
/megamanager/libs/compress_images_lib.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 6/5/2017
4 | # Initial Creation.
5 | ###
6 | from libs.lib import Lib
7 | from logging import getLogger
8 | from os import path
9 | from re import IGNORECASE, match
10 | from tools.compressImages import CompressImage, DeleteBackupImage
11 | # from tools import CompressImage, DeleteBackupImage
12 |
13 | __author__ = 'szmania'
14 |
15 |
16 | class CompressImages_Lib:
17 | def __init__(self, log_level='DEBUG'):
18 | """
19 | Library for compressImages.py interaction.
20 |
21 | Args:
22 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
23 | """
24 |
25 | self.__log_level = log_level
26 |
27 | self.__compress_images_obj = CompressImage()
28 | self.__deleteBackupImageObj = DeleteBackupImage()
29 | self.__lib = Lib(log_level=self.__log_level)
30 |
31 | def compress_image_file(self, file_path, jpeg_compression_quality_percentage, delete_backup=False,
32 | delete_corrupt_images=False):
33 | """
34 | Compress images file.
35 |
36 | Args:
37 | file_path (str): File path of image to compress.
38 | jpeg_compression_quality_percentage (int): Quality percentage compression for jpeg files.
39 | delete_backup (bool): Delete backup image file.
40 | delete_corrupt_images (bool): Delete backup image file.
41 |
42 | Returns:
43 | Boolean: whether compression operation was successful or not.
44 | """
45 |
46 | logger = getLogger('CompressImages_Lib.compress_image_file')
47 | logger.setLevel(self.__log_level)
48 |
49 | logger.debug(' Compressing image file: "%s"' % file_path)
50 | results = []
51 | file_ext = file_path.split('.')[-1]
52 | compressed_any_image = self.__compress_images_obj.processfile(filename=file_path)
53 | results.append(compressed_any_image)
54 |
55 | if match('jpe{0,1}g', file_ext, IGNORECASE):
56 | jpeg_compressed = self.compress_jpeg_image_file(file_path=file_path,
57 | quality_percentage=jpeg_compression_quality_percentage)
58 |
59 | if delete_corrupt_images and not jpeg_compressed:
60 | logger.debug(' Deleting CORRUPT JPEG or JPG image file: "{}"'.format(file_path))
61 | self.__lib.delete_local_file(file_path=file_path)
62 |
63 | results.append(jpeg_compressed)
64 |
65 | if delete_backup:
66 | compress_path_backup = file_path + '.compressimages-backup'
67 | if path.exists(compress_path_backup):
68 | logger.debug(' Removing backup image file "{}"!'.format(compress_path_backup))
69 | self.__lib.delete_local_file(file_path=compress_path_backup)
70 |
71 | if True in results:
72 | logger.debug(' Success, image file "%s" compressed successfully.' % file_path)
73 | return True
74 |
75 | logger.debug(' Error, image file "%s" NOT compressed successfully!' % file_path)
76 | return False
77 |
78 | def compress_jpeg_image_file(self, file_path, quality_percentage):
79 | """
80 | Compress images file.
81 |
82 | Args:
83 | file_path (str): File path of image to compress.
84 | quality_percentage (int): Percentage to set output jpeg file.
85 |
86 | Returns:
87 | Boolean: whether compression operation was successful or not.
88 | """
89 |
90 | logger = getLogger('CompressImages_Lib.compress_jpeg_image_file')
91 | logger.setLevel(self.__log_level)
92 |
93 | logger.debug(' Compressing JPEG or JPG image file "%s".' % file_path)
94 | compressed = False
95 | skipped = False
96 | out, err = None, None
97 | try:
98 | out, err = self.__lib.exec_cmd_and_return_output(command='jpegoptim --max={quality_percentage} "{file_path}"'.format(
99 | quality_percentage=quality_percentage, file_path=file_path))
100 | except Exception as e:
101 | logger.error(' Exception: {}'.format(e))
102 | if not out:
103 | logger.debug(' Error, JPEG or JPG image file "%s" NOT compressed successfully!' % file_path)
104 | return False
105 | else:
106 | logger.debug(f' Image compression output: {out}')
107 | if 'optimized' in str(out):
108 | logger.debug(' Success, JPEG or JPG image file "%s" compressed successfully.' % file_path)
109 | return True
110 | elif 'skipped' in str(out):
111 | logger.debug(' JPEG or JPG file already optimized! File was skipped: "{}"'.format(file_path))
112 | return True
113 | else:
114 | logger.debug(' Error, JPEG or JPG image file "%s" NOT compressed successfully!' % file_path)
115 | return False
116 |
117 |
118 | def delete_backups_in_dir(self, dirPath):
119 | """
120 | Delete backup files in directory
121 |
122 | Args:
123 | dirPath (str): Directory path of image backups to delete
124 |
125 | Returns:
126 | Boolean: whether compression operation was successful or not.
127 | """
128 |
129 | logger = getLogger('CompressImages_Lib.delete_backups_in_dir')
130 | logger.setLevel(self.__log_level)
131 |
132 | logger.debug(' Deleting compression file backups in dirPath "%s".' % dirPath)
133 |
134 | result = self.__deleteBackupImageObj.processdir(path=dirPath)
135 |
136 | if result:
137 | logger.debug(' Success, could remove backup image compression files in direcotry "%s"' % dirPath)
138 | return True
139 | else:
140 | logger.error(' Error, could NOT remove backup image compression files in direcotry "%s"' % dirPath)
141 | return False
142 |
--------------------------------------------------------------------------------
/megamanager/libs/ffmpeg_lib.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 6/5/2017
4 | # Initial Creation.
5 | ###
6 |
7 | from .lib import Lib
8 | from .mega_tools_lib import MEGA_MANAGER_CONFIG_DIR
9 | from logging import getLogger
10 | from os import path, remove, sep
11 | from platform import system
12 |
13 | __author__ = 'szmania'
14 |
15 | HOME_DIRECTORY = "~"
16 | FFMPEG_LOG_PATH = path.join("{MEGA_MANAGER_CONFIG_DIR}".format(
17 | MEGA_MANAGER_CONFIG_DIR=MEGA_MANAGER_CONFIG_DIR),"logs","ffmpeg.log")
18 |
19 |
20 |
21 | class FFMPEG_Lib(object):
22 | def __init__(self, log_level='DEBUG', log_file_path=FFMPEG_LOG_PATH):
23 | """
24 | Library for ffmpeg converter and encoder interaction.
25 |
26 | Args:
27 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
28 | log_file_path (str): Log file path for ffmpeg.
29 |
30 | """
31 |
32 | self.__log_level = log_level
33 | self.__ffmpeg_log = log_file_path
34 | self.__lib = Lib(log_level=log_level)
35 |
36 | def compress_video_file(self, source_path, target_path, compression_max_width=1280, compression_preset="medium",
37 | ffmpeg_threads=1, overwrite=False,
38 | process_priority_class="NORMAL_PRIORITY_CLASS", process_set_priority_timeout=60):
39 | """
40 | Compress video file.
41 |
42 | Args:
43 | source_path (str): File path of video to __compress_all.
44 | target_path (str): File path of video to be compressed into.
45 | compression_max_width (int): Compression for video, max width. eg. 1280 or 1920
46 | compression_preset (str): Compression preset. ie: "fast" or "slow"
47 | ffmpeg_threads (int): Number of threads to use when running ffmpeg.
48 | overwrite (bool): Overwrite target file if it exists.
49 | process_priority_class (str): Priority level to set for process. ie: "NORMAL_PRIORITY_CLASS"
50 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
51 |
52 | Returns:
53 | subprocess object:
54 | """
55 |
56 | logger = getLogger('FFMPEG_Lib.compress_video_file')
57 | logger.setLevel(self.__log_level)
58 | logger.debug(f' Compressing video file "{source_path}" to "{target_path}"')
59 |
60 | if overwrite:
61 | logger.debug(' Overwrite set to "True". Removing target file: "%s"' % target_path)
62 | if path.exists(target_path):
63 | try:
64 | remove(target_path)
65 | except Exception as e:
66 | logger.exception(' Exception: %s' % str(e))
67 | pass
68 |
69 | cmd = ('ffmpeg -i "{source_path}" -vf "scale=\'if(lte(iw,{compression_max_width}), {compression_max_width}, iw)\':-2" '
70 | '-preset {preset} -threads {ffmpeg_threads} -max_muxing_queue_size 9999 "{target_path}"').format(
71 | source_path=source_path, compression_max_width=compression_max_width, preset=compression_preset,
72 | ffmpeg_threads=ffmpeg_threads, target_path=target_path)
73 |
74 | process_name = 'ffmpeg.exe' if system() == 'Windows' else 'ffmpeg'
75 | result = self.__lib.exec_cmd(command=cmd, no_window=True, output_file=self.__ffmpeg_log, process_priority_class=process_priority_class,
76 | process_name=process_name, process_set_priority_timeout=process_set_priority_timeout)
77 |
78 | if result:
79 | logger.debug(' Success, could compress video file "%s" to "%s".' % (source_path, target_path))
80 | else:
81 | logger.error(' Error, could NOT compress video file "%s"!' % source_path)
82 | return result
83 |
84 |
--------------------------------------------------------------------------------
/megamanager/libs/lib.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 5/31/2017
4 | # Initial Creation.
5 | ###
6 | import psutil
7 | import shlex
8 | import shutil
9 | from hashlib import md5
10 | from logging import getLogger
11 | from numpy import array, load, save
12 | from os import chdir, kill, listdir, path, remove, rename, sep
13 | from platform import system
14 | from re import split, sub
15 | from signal import SIGTERM
16 | from subprocess import call, PIPE, Popen
17 | from threading import Thread
18 | from time import localtime, sleep, strftime, time
19 |
20 | __author__ = 'szmania'
21 |
22 |
23 | class Lib(object):
24 | def __init__(self, log_level='DEBUG'):
25 | """
26 | MegaManager library.
27 |
28 | Args:
29 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
30 | """
31 |
32 | self.__log_level = log_level
33 |
34 |
35 | def _find_procs_by_name(self, name, timeout=None):
36 | """
37 | Return a list of processes matching 'name'
38 | Args:
39 | name (str): Process name to find.
40 | timeout (int): timeout in seconds to wait for process to start.
41 |
42 | Returns:
43 | List: Processes matching name.
44 | """
45 | logger = getLogger('Lib._find_procs_by_name')
46 | logger.setLevel(self.__log_level)
47 |
48 | found_procs = []
49 | time_started = time()
50 | try:
51 | while True:
52 | for p in psutil.process_iter():
53 | if p.name() == name:
54 | found_procs.append(p)
55 |
56 | now = time()
57 | if len(found_procs) > 0:
58 | logger.debug(' Success, found process "{}"'.format(name))
59 | break
60 | elif now - time_started > timeout:
61 | raise ProcessNameNotFound
62 | return found_procs
63 |
64 | except ProcessNameNotFound as e:
65 | logger.warning(' ProcessNameNotFound Exception: Process name "{}" not found!'.format(name))
66 | pass
67 |
68 | except Exception as e:
69 | logger.error(' Exception: {}'.format(e))
70 | pass
71 |
72 | return found_procs
73 |
74 | def _set_priority_once_proc_starts(self, process_name, priority_class, process_set_priority_timeout):
75 | """
76 | Wait for process to start then set priority.
77 | Args:
78 | process_name (str): Process name to set priority.
79 | priority_class (str): priority level. ie: "NORMAL_PRIORITY_CLASS"
80 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
81 |
82 | Returns:
83 |
84 | """
85 | logger = getLogger('Lib._set_priority_once_proc_starts')
86 | logger.setLevel(self.__log_level)
87 |
88 | sleep(1)
89 | t = Thread(target=self._thread_set_priority_once_proc_starts, args=(process_name, priority_class,
90 | process_set_priority_timeout, ),
91 | name='thread_set_priority_{}'.format(process_name))
92 | t.start()
93 |
94 | def _set_proc_priority(self, proc, priority_class):
95 | """
96 | Set process priority.
97 | Args:
98 | proc: psutils process object.
99 | priority_class (str): Priority level. ie: "BELOW_NORMAL_PRIORITY_CLASS"
100 |
101 | Returns:
102 | Boolean: Whether successful or not.
103 | """
104 | logger = getLogger('Lib._set_proc_priority')
105 | logger.setLevel(self.__log_level)
106 | logger.debug(' Setting process "{}" to priority class "{}"'.format(proc.name(), priority_class))
107 |
108 | try:
109 | if priority_class == 'IDLE_PRIORITY_CLASS':
110 | if system() == 'Windows':
111 | proc.nice(psutil.IDLE_PRIORITY_CLASS)
112 | else:
113 | proc.nice(20)
114 | elif priority_class == 'BELOW_NORMAL_PRIORITY_CLASS':
115 | if system() == 'Windows':
116 | proc.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
117 | else:
118 | proc.nice(10)
119 | elif priority_class == 'NORMAL_PRIORITY_CLASS':
120 | if system() == 'Windows':
121 | proc.nice(psutil.NORMAL_PRIORITY_CLASS)
122 | else:
123 | proc.nice(0)
124 | elif priority_class == 'ABOVE_NORMAL_PRIORITY_CLASS':
125 | if system() == 'Windows':
126 | proc.nice(psutil.ABOVE_NORMAL_PRIORITY_CLASS)
127 | else:
128 | proc.nice(-5)
129 | elif priority_class == 'HIGH_PRIORITY_CLASS':
130 | if system() == 'Windows':
131 | proc.nice(psutil.HIGH_PRIORITY_CLASS)
132 | else:
133 | proc.nice(-10)
134 | elif priority_class == 'REALTIME_PRIORITY_CLASS':
135 | if system() == 'Windows':
136 | proc.nice(psutil.REALTIME_PRIORITY_CLASS)
137 | else:
138 | proc.nice(-20)
139 | else:
140 | logger.warning('Error, invalid priority class: {}'.format(priority_class))
141 | return False
142 |
143 | logger.debug(' Success, set process priority_class to "{}"'.format(priority_class))
144 | return True
145 |
146 | except Exception as e:
147 | logger.error(' Exception: {}'.format(e))
148 | return False
149 |
150 | def _thread_set_priority_once_proc_starts(self, process_name, priority_class, process_set_priority_timeout):
151 | """
152 | Wait for process to start then set priority.
153 | Args:
154 | process_name (str): Process name to set priority.
155 | priority_class (): priority level. ie: "NORMAL_PRIORITY_CLASS"
156 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
157 |
158 | """
159 | logger = getLogger('Lib._thread_set_priority_once_proc_starts')
160 | logger.setLevel(self.__log_level)
161 | sleep(10)
162 | procs = self._find_procs_by_name(name=process_name, timeout=process_set_priority_timeout)
163 | for proc in procs:
164 | self._set_proc_priority(proc=proc, priority_class=priority_class)
165 |
166 | def convert_epoch_to_mega_time(self, epoch_time):
167 | """
168 | Convert epoch time to time format used by MEGA.
169 | ie: 1247547145.65 to '2017-08-18 22:37:50'
170 |
171 | Args:
172 | epoch_time (str): Epoch time to convert.
173 |
174 | Returns:
175 | datetime: Converted datetime object. ie: '%Y-%m-%d %H:%M:%S'
176 | """
177 | return strftime('%Y-%m-%d %H:%M:%S', localtime(epoch_time))
178 |
179 | def delete_local_file(self, file_path):
180 | """
181 | Delete local file.
182 |
183 | Args:
184 | file_path (str): File path to delete.
185 |
186 | Returns:
187 | Boolean: boolean of whether successful or not
188 | """
189 | logger = getLogger('Lib.delete_local_file')
190 | logger.setLevel(self.__log_level)
191 | logger.debug(' Deleting local file: {}'.format(file_path))
192 |
193 | if path.exists(file_path):
194 | for retry in range(10):
195 | try:
196 | remove(file_path)
197 | logger.debug(' File deleted!')
198 | return True
199 |
200 | except Exception as e:
201 | logger.exception(' Exception: {}'.format(e))
202 | logger.debug(' Removing of file "{}" FAILED, retrying...'.format(file_path))
203 | else:
204 | logger.error(' Cannot delete! Path does not exist: "{}"'.format(file_path))
205 |
206 | return False
207 |
208 | def dump_set_into_numpy_file(self, item_set, file_path):
209 | """
210 | Dump set into file for each item on a new line.
211 |
212 | Args:
213 | item_set (set): Set to dump into file.
214 | file_path (str): File to dump to.
215 |
216 | Returns:
217 | Boolean: boolean of whether successful or not
218 | """
219 |
220 | logger = getLogger('MegaManager_lib.dump_set_into_numpy_file')
221 | logger.setLevel(self.__log_level)
222 |
223 | logger.debug(' Dumping set into "%s".' % file_path)
224 |
225 | try:
226 | np_list = array(list(item_set))
227 | # savez_compressed(file_path, list=npList)
228 | # savez(file_path, list=npList)
229 | save(file_path, np_list)
230 | return True
231 | except Exception as e:
232 | logger.debug(' Exception: %s' % str(e))
233 | return False
234 |
235 | def exec_cmd(self, command, working_dir=None, no_window=False, output_file=None, process_name=None,
236 | process_priority_class=None, process_set_priority_timeout=60):
237 | """
238 | Execute given command.
239 |
240 | Args:
241 | command (str): Command to execute.
242 | working_dir (str): Working directory.
243 | no_window (bool): No window will be created if true.
244 | output_file (str): file path to output program output to.
245 | process_name (str): Process name to set priority level.
246 | process_priority_class (str): Priority level to set process to. ie: "BELOW_NORMAL_PRIORITY_CLASS"
247 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
248 |
249 | Returns:
250 | subprocess object
251 | """
252 | logger = getLogger('Lib.exec_cmd')
253 | logger.setLevel(self.__log_level)
254 | logger.debug(' Executing command: "%s"' % command)
255 |
256 | try:
257 | if working_dir:
258 | chdir(working_dir)
259 |
260 | if process_priority_class:
261 | self._set_priority_once_proc_starts(process_name=process_name, priority_class=process_priority_class,
262 | process_set_priority_timeout=process_set_priority_timeout)
263 | with open(output_file, 'a') as out_file:
264 | if no_window and system() == 'Windows':
265 | CREATE_NO_WINDOW = 0x08000000
266 | exit_code = call(shlex.split(command), stdout=out_file, stderr=out_file, creationflags=CREATE_NO_WINDOW)
267 |
268 | else:
269 | exit_code = call(shlex.split(command), stdout=out_file, stderr=out_file)
270 |
271 | if exit_code == 0:
272 | logger.debug(' Successfully executed command "%s".' % command)
273 | return True
274 |
275 | except Exception as e:
276 | logger.exception(' Exception: {}'.format(e))
277 |
278 | logger.error(' Error when running command "%s".' % command)
279 | return False
280 |
281 | def exec_cmd_and_return_output(self, command, working_dir=None, output_file=None):
282 | """
283 | Execute given command and return stdout and stderr.
284 |
285 | Args:
286 | command (str): Command to execute.
287 | working_dir (str): Working directory.
288 | output_file (str): File to pipe process output to.
289 |
290 | Returns:
291 | Tuple: of stdout and stderr.
292 | """
293 | logger = getLogger('MegaTools_Lib.exec_cmd_and_return_output')
294 | logger.setLevel(self.__log_level)
295 | logger.debug(' Executing command: "%s"' % command)
296 |
297 | out = None
298 | err = None
299 | if working_dir:
300 | chdir(working_dir)
301 |
302 | try:
303 | # out = check_output(shlex.split(command), stderr=STDOUT)
304 | proc = Popen(shlex.split(command), stdout=PIPE, stderr=PIPE)
305 | (out, err) = proc.communicate()
306 | if output_file:
307 | with open(output_file, 'a') as out_file:
308 | out_file.write(out)
309 | except Exception as e:
310 | logger.warning(' Exception: %s' % str(e))
311 | return None, None
312 | return out, err
313 |
314 | def get_file_md5_hash(self, file_path):
315 | """
316 | Gets file md5 hash.
317 |
318 | ArgsL
319 | file_path (str): File path to get md5 has of.
320 |
321 | Returns:
322 | Str: md5 hash of file.
323 | """
324 | logger = getLogger('Lib.get_file_md5_hash')
325 | logger.setLevel(self.__log_level)
326 | logger.debug(' Getting md5 hash of file: "{}"'.format(file_path))
327 |
328 | try:
329 | hash_md5 = md5()
330 | with open(file_path, "rb") as f: # Open the file in binary mode
331 | for chunk in iter(lambda: f.read(4096), b""): # Read file in chunks of 4096 bytes
332 | hash_md5.update(chunk)
333 | file_md5_hash = hash_md5.hexdigest()
334 | # file_md5_hash = md5(file_path).hexdigest()
335 | logger.debug(' md5 hash of file is: "{}"'.format(file_md5_hash))
336 | return file_md5_hash
337 | except Exception as e:
338 | logger.debug(' Exception: %s' % str(e))
339 | return False
340 |
341 | def get_mb_size_from_bytes(self, bytes):
342 | """
343 | Convert bytes to size in MegaBytes.
344 |
345 | Args:
346 | bytes (int): Size in bytes.
347 |
348 | Returns:
349 | string: Size in MegaBytes converted from bytes.
350 | """
351 |
352 | logger = getLogger('Lib.get_mb_size_from_bytes')
353 | logger.setLevel(self.__log_level)
354 |
355 | logger.debug(' Converting kilobytes to megabytes.')
356 |
357 | result = ''
358 | if bytes < 1000:
359 | result = '%i' % bytes + 'B'
360 | elif 1000 <= bytes < 1000000:
361 | result = '%.1f' % float(bytes / 1000) + 'KB'
362 | elif 1000000 <= bytes < 1000000000:
363 | result = '%.1f' % float(bytes / 1000000) + 'MB'
364 | elif 1000000000 <= bytes < 1000000000000:
365 | result = '%.1f' % float(bytes / 1000000000) + 'GB'
366 | elif 1000000000000 <= bytes:
367 | result = '%.1f' % float(bytes / 1000000000000) + 'TB'
368 |
369 | return result
370 |
371 | def get_remote_path_from_local_path(self, localPath, localRoot="{sep}".format(sep=sep), remoteRoot="\\"):
372 | """
373 | Get remote path given local path.
374 |
375 | Args:
376 | localPath (str): Local path.
377 | localRoot (str): Local root
378 | remoteRoot (str): Remote root.
379 | Returns:
380 | string: Remote path.
381 | """
382 |
383 | logger = getLogger('Lib.get_remote_path_from_local_path')
384 | logger.setLevel(self.__log_level)
385 |
386 | logger.debug(' Getting remote path from local path')
387 |
388 | localRoot_adj = sub('\\\\', '/', localRoot)
389 | localFilePath_adj = sub('\\\\', '/', localPath)
390 | postfix = split(localRoot_adj, localFilePath_adj)
391 |
392 | if len(postfix) > 1:
393 | subPath = postfix[1]
394 |
395 | logger.debug(' Success, could get remote path from local path.')
396 | return remoteRoot + subPath
397 | else:
398 | logger.debug(' Error, could NOT get remote path from local path!')
399 | return None
400 |
401 | def get_sleep_time(self):
402 | """
403 | Get time in seconds to sleep. Function is used to pace program speed during iterations.
404 |
405 | :return: Time in seconds to sleep as integer.
406 | """
407 |
408 | logger = getLogger('megaManager_lib.get_sleep_time')
409 | logger.setLevel(self.__log_level)
410 |
411 | sleepTime = 0
412 |
413 | return sleepTime
414 |
415 | def get_items_in_list_with_sub_string(self, list, sub_string):
416 | """
417 | Return sub list of list that contain substring.
418 |
419 | :param list: List to find substrings in.
420 | :type list: List.
421 | :param sub_string: Substring to find.
422 | :type sub_string: String.
423 |
424 | :return: List of items that contain subString.
425 | """
426 |
427 | logger = getLogger('Lib.get_items_in_list_with_sub_string')
428 | logger.setLevel(self.__log_level)
429 |
430 | subList = []
431 | for item in list:
432 | if sub_string in item:
433 | subList.append(item)
434 | return subList
435 |
436 | def kill_running_processes_with_name(self, proc_name):
437 | """
438 | Kill processes with name.
439 |
440 | Args:
441 | proc_name (str): Process name.
442 |
443 | :return:
444 | """
445 |
446 | logger = getLogger('megaManager_lib.kill_running_processes_with_name')
447 | logger.setLevel(self.__log_level)
448 |
449 | logger.info(' Killing processes with name "%s"' % proc_name)
450 | try:
451 | # p = Popen(['ps', '-a'], stdout=PIPE)
452 | p = Popen(['tasklist', '/v'], stdout=PIPE)
453 | out, err = p.communicate()
454 |
455 | for line in out.splitlines():
456 | if line.startswith(proc_name):
457 | pid = int(line.split()[1])
458 | kill(pid, SIGTERM)
459 |
460 | logger.debug(' Success, all "%s" processes have been killed.' % proc_name)
461 | return True
462 |
463 | except Exception as e:
464 | logger.error(' Exception: {}'.format(e))
465 | return False
466 |
467 | def load_numpy_file_as_set(self, file_path):
468 | """
469 | Load file as set splitting each line into a new item.
470 |
471 | Args:
472 | file_path (str): File to lead.
473 |
474 | Returns:
475 | Lines in file as a set.
476 | """
477 | logger = getLogger('Lib.load_numpy_file_as_set')
478 | logger.setLevel(self.__log_level)
479 |
480 | items = set()
481 | if path.isfile(file_path):
482 | logger.debug(' Loading file as set: {}'.format(file_path))
483 |
484 | try:
485 | data = load(file=file_path, allow_pickle=False)
486 | items = set(data.tolist())
487 | except Exception as e:
488 | logger.debug(' Exception: %s' % str(e))
489 | finally:
490 | return items
491 |
492 | else:
493 | logger.debug(' Filepath "%s" does NOT exist. Using empty set.' % file_path)
494 | return items
495 |
496 | def rename_file(self, old_name, new_name):
497 | """
498 | Renames file.
499 |
500 | Args:
501 | old_name (str): Old file name.
502 | new_name (str): New file name.
503 |
504 | Returns:
505 | bool: Whether successful or not.
506 | """
507 | logger = getLogger('Lib.rename_file')
508 | logger.setLevel(self.__log_level)
509 |
510 | logger.debug(' Renaming file "{}" to "{}."'.format(old_name, new_name))
511 | for retry in range(10):
512 | try:
513 | shutil.move(old_name, new_name)
514 | logger.debug(' Rename succeeded!')
515 | return True
516 |
517 | except Exception as e:
518 | logger.error(' Exception: {}'.format(e))
519 | logger.debug(" Rename failed, retrying...")
520 | if 'Cannot create a file when that file already exists' in str(e):
521 | self.delete_local_file(file_path=new_name)
522 | return False
523 |
524 | def size_of_dir(self, dir_path):
525 | """
526 | Walks through the directory, getting the cumulative size of the directory
527 |
528 | :param dir_path: Directory to walk through to get size.
529 | :type dir_path: String.
530 |
531 | :return: Size in bytes as integer.
532 | """
533 |
534 | logger = getLogger('Lib.size_of_dir')
535 | logger.setLevel(self.__log_level)
536 |
537 | logger.debug(' Getting size of directory "%s"' % dir_path)
538 |
539 | sum = 0
540 | for file in listdir(dir_path):
541 | sum += path.getsize(path.join(dir_path,file))
542 | return sum
543 |
544 |
545 |
546 | class ProcessNameNotFound(Exception):
547 | pass
548 |
--------------------------------------------------------------------------------
/megamanager/libs/mega_tools_lib.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 5/31/2017
4 | # Initial Creation.
5 | ###
6 |
7 | from .lib import Lib
8 | from logging import getLogger
9 | from os import linesep, path, sep
10 | from platform import system
11 | from re import findall, split, sub
12 |
13 | __author__ = 'szmania'
14 |
15 | HOME_DIRECTORY = "~"
16 | MEGA_MANAGER_CONFIG_DIR = path.join("{HOME_DIRECTORY}".format(HOME_DIRECTORY=HOME_DIRECTORY),".mega_manager")
17 | MEGATOOLS_LOG_PATH = path.join("{MEGA_MANAGER_CONFIG_DIR}".format(
18 | MEGA_MANAGER_CONFIG_DIR=MEGA_MANAGER_CONFIG_DIR), "logs","mega_tools.log")
19 |
20 |
21 | class MegaTools_Lib(object):
22 | def __init__(self, down_speed_limit=None, up_speed_limit=None, log_level='DEBUG', log_file_path=MEGATOOLS_LOG_PATH):
23 | """
24 | Library for interaction with MegaTools. A tool suite for MEGA.
25 |
26 | Args:
27 | log_file_path (str): Log file path for MEGATools
28 | down_speed_limit (int): Max download speed limit.
29 | up_speed_limit (int): Max upload speed limit.
30 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
31 | """
32 | self.__mega_tools_log = log_file_path
33 | self.__downSpeedLimit = down_speed_limit
34 | self.__up_speed_limit = up_speed_limit
35 | self.__log_level = log_level
36 |
37 | self.__lib = Lib(log_level=log_level)
38 |
39 | def create_remote_dir(self, username, password, remote_path, process_priority_class, process_set_priority_timeout):
40 | """
41 | Create remote MEGA directory
42 |
43 | Args:
44 | username (str): username of account to __download file from
45 | password (str): password of account to __download file from
46 | remote_path (str): Path to remote directory.
47 | process_priority_class (str): Priority level to set process to. ie: "NORMAL_PRIORITY_CLASS"
48 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
49 |
50 | Returns:
51 | Bool: Whether successful or not.
52 | """
53 | logger = getLogger('MegaTools_Lib.create_remote_dir')
54 | logger.setLevel(self.__log_level)
55 |
56 | logger.debug(' Creating remote directory for account "{}" in MEGA: "{}"'.format(username, remote_path))
57 | try:
58 | cmd = 'megamkdir -u {} -p {} "{}"'.format(username, password, remote_path)
59 |
60 | process_name = 'megamkdir.exe' if system() == 'Windows' else 'megamkdir'
61 | result = self.__lib.exec_cmd(command=cmd, no_window=True, output_file=self.__mega_tools_log,
62 | process_name=process_name, process_priority_class=process_priority_class,
63 | process_set_priority_timeout=process_set_priority_timeout)
64 | # result = self.__lib.exec_cmd(command=cmd, working_dir=self.__mega_tools_dir, noWindow=True,
65 | # output_file=self.__mega_tools_log)
66 | if not result:
67 | logger.debug(' Error, could not make directory "{}" for account "{}"!'.format(remote_path, username))
68 | return False
69 |
70 | logger.debug(' Success, could make directory "{}" for account "{}"!'.format(remote_path, username))
71 | return True
72 |
73 | except Exception as e:
74 | logger.error(' Exception: {}'.format(e))
75 | return False
76 |
77 | def download_all_files_from_account(self, username, password, local_root, remote_root, process_set_priority_timeout):
78 | """
79 | Download all account files.
80 |
81 | Args:
82 | username (str): username of account to __download file from
83 | password (str): password of account to __download file from
84 | local_root (str): Local path to __download file to
85 | remote_root (str): Remote path of file to __download
86 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
87 |
88 | Returns:
89 | """
90 |
91 | logger = getLogger('MegaTools_Lib.download_all_files_from_account')
92 | logger.setLevel(self.__log_level)
93 |
94 | logger.debug(' MEGA downloading directory from account "%s" from "%s" to "%s"' % (username, local_root, remote_root))
95 |
96 | if self.__downSpeedLimit:
97 | cmd = 'megacopy --download -u %s -p %s --limit-speed %d --local "%s" --remote "%s"' % (
98 | username, password, self.__downSpeedLimit, local_root, remote_root)
99 |
100 | else:
101 | cmd = 'megacopy --download -u %s -p %s --local "%s" --remote "%s"' % (
102 | username, password, local_root, remote_root)
103 |
104 | process_name = 'megacopy.exe' if system() == 'Windows' else 'megacopy'
105 | result = self.__lib.exec_cmd(command=cmd, no_window=True, output_file=self.__mega_tools_log,
106 | process_priority_class='NORMAL_PRIORITY_CLASS', process_name=process_name,
107 | process_set_priority_timeout=process_set_priority_timeout)
108 |
109 | if result:
110 | logger.debug(' Success, downloadeded all files from account.')
111 | return True
112 | else:
113 | logger.debug(' Error, could not download all files from account!')
114 | return False
115 |
116 | def download_file(self, username, password, localFilePath, remoteFilePath):
117 | """
118 | Download a remote file from MEGA account.
119 |
120 | Args:
121 | username (str): username of account to __download file from
122 | password (str): password of account to __download file from
123 | localFilePath (str): Location to __download file to.
124 | remoteFilePath (str): Location to __download file from.
125 |
126 | Returns:
127 | bool: whether successful download or not
128 | """
129 |
130 | logger = getLogger('MegaManager._download_file')
131 | logger.setLevel(self.__log_level)
132 |
133 | logger.debug(' MEGA downloading file from account "%s" - "%s" to "%s"' % (username, password, localFilePath))
134 |
135 | cmd = 'megaget -u %s -p %s --path "%s" "%s"' % (username, password, localFilePath, remoteFilePath)
136 | process_name = 'megaget.exe' if system() == 'Windows' else 'megaget'
137 | result = self.__lib.exec_cmd(command=cmd, no_window=True, output_file=self.__mega_tools_log,
138 | process_priority_class='NORMAL_PRIORITY_CLASS', process_name=process_name,
139 | process_set_priority_timeout=self.__process_set_priority_timeout)
140 |
141 | if result:
142 | logger.debug(' Successfully downloaded file.')
143 | return True
144 | else:
145 | logger.debug(' Error when trying to download file.')
146 | return False
147 |
148 | def get_file_date_from_megals_line_data(self, line):
149 | """
150 | Extract file date from megals line data output.
151 | example input:
152 | udtDgR7I Xz2tWWB5Dmo 0 4405067776 2013-04-10 19:16:02 /Root/bigfile
153 |
154 | Args:
155 | line (str): line to extract file date from.
156 |
157 | Returns:
158 | string: File date as string. ie: "2013-04-10 19:16:02"
159 | """
160 |
161 | logger = getLogger('MegaTools_Lib.get_file_date_from_megals_line_data')
162 | logger.setLevel(self.__log_level)
163 |
164 | logger.debug(' Getting file date from "%s"' % line)
165 |
166 | line_split = line.split()
167 |
168 | if len(line_split) > 2:
169 | remoteFileModifiedDate = line_split[4]
170 | remoteFileModifiedTime = line_split[5]
171 |
172 | remoteFileModifiedDate_time = '%s %s' % (remoteFileModifiedDate, remoteFileModifiedTime)
173 |
174 | logger.debug(' Success, could find remote file modified date.')
175 | return remoteFileModifiedDate_time
176 |
177 | logger.warning(' Error, could NOT find remote file modified date!')
178 | return None
179 |
180 | def get_file_extension_from_megals_line_data(self, line):
181 | """
182 | Extract file extension from megals line data output.
183 | example input:
184 | udtDgR7I Xz2tWWB5Dmo 0 4405067776 2013-04-10 19:16:02 /Root/bigfile.jpg
185 |
186 | Args:
187 | line (str): line to extract file extension from.
188 |
189 | Returns:
190 | string: File extension. ie: "jpg"
191 | """
192 |
193 | logger = getLogger('MegaTools_Lib.get_file_extension_from_megals_line_data')
194 | logger.setLevel(self.__log_level)
195 |
196 | logger.debug(' Getting file extension from "%s"' % line)
197 | fileExt = None
198 | try:
199 | fileExt = path.splitext(split(r':\d{2} ', line)[1])[1]
200 | except Exception as e:
201 | logger.error('Exception: %s' % str(e))
202 | finally:
203 | return fileExt
204 |
205 | def get_file_path_from_megals_line_data(self, line):
206 | """
207 | Extract file path from megals line data output.
208 | example input:
209 | udtDgR7I Xz2tWWB5Dmo 0 4405067776 2013-04-10 19:16:02 /Root/bigfile
210 |
211 | Args:
212 | line (str): line to extract file path from.
213 |
214 | Returns:
215 | string: File path. ie: "/Root".
216 | """
217 |
218 | logger = getLogger('MegaTools_Lib.get_file_path_from_megals_line_data')
219 | logger.setLevel(self.__log_level)
220 |
221 | logger.debug(' Getting file path from "%s"' % line)
222 | remote_filePath = None
223 | try:
224 | remote_filePath = split(r':\d{2} ', line)[1]
225 | except Exception as e:
226 | logger.error('Exception: %s' % str(e))
227 | finally:
228 | return remote_filePath
229 |
230 | def get_file_size_from_megals_line_data(self, line):
231 | """
232 | Extract file size from megals line data output.
233 | example input:
234 | udtDgR7I Xz2tWWB5Dmo 0 4405067776 2013-04-10 19:16:02 /Root/bigfile
235 |
236 | Args:
237 | line (str): Line to extract file path from.
238 |
239 | Returns:
240 | string: File size. ie: "4405067776".
241 | """
242 |
243 | logger = getLogger('MegaTools_Lib.get_file_size_from_megals_line_data')
244 | logger.setLevel(self.__log_level)
245 |
246 | logger.debug(' Getting file size from "%s"' % line)
247 |
248 | line_split = line.split()
249 | if len(line_split) > 2:
250 | remoteFileSize = line_split[3]
251 | logger.debug(' Success, remote file size of line "%s" is "%s"' % (line, remoteFileSize))
252 | return remoteFileSize
253 | else:
254 | logger.error(' Error, could not get remote file size from line "%s"' % line)
255 | return None
256 |
257 | def get_file_type_from_megals_line_data(self, line):
258 | """
259 | Extract file type from megals line data output.
260 | example input:
261 | udtDgR7I Xz2tWWB5Dmo 0 4405067776 2013-04-10 19:16:02 /Root/bigfile
262 |
263 | Args:
264 | line (str): line to extract file type from.
265 |
266 | Returns:
267 | string: File type as integer. 0 = file, 1 = directory, 2 = MEGA account system file ie: "/Root".
268 | """
269 | logger = getLogger('MegaTools_Lib.get_file_type_from_megals_line_data')
270 | logger.setLevel(self.__log_level)
271 | logger.debug(' Getting file type from "%s"' % line)
272 |
273 | remote_type = None
274 | try:
275 | remote_type = line.split()[2]
276 | except Exception as e:
277 | logger.error('Exception: %s' % str(e))
278 | finally:
279 | return remote_type
280 |
281 | def get_account_free_space(self, username, password):
282 | """
283 | Get account free space in gigabytes
284 |
285 | Args:
286 | username (str): username for MEGA account
287 | password (str): password for MEGA account
288 |
289 | Returns:
290 | int: Free space of account in gigabytes.
291 | """
292 |
293 | logger = getLogger('MegaTools_Lib.get_account_used_space')
294 | logger.setLevel(self.__log_level)
295 |
296 | # chdir('%s' % self.__mega_tools_dir)
297 |
298 | cmd = 'megadf --free -h --gb -u %s -p %s' % (username, password)
299 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
300 |
301 | if err:
302 | logger.info(str(err))
303 |
304 | if not out == '':
305 | freeSpace = sub('\r', '', out).rstrip()
306 | logger.debug(' Success, could get account free space.')
307 | return freeSpace
308 | logger.debug(' Error, could NOT get account free space!')
309 | return 0
310 |
311 | def get_account_used_space(self, username, password):
312 | """
313 | Get account used space in gigabytes
314 |
315 | Args:
316 | username (str): username for MEGA account
317 | password (str): password for MEGA account
318 |
319 | Returns:
320 | int: Used space of account in gigabytes.
321 | """
322 |
323 | logger = getLogger('MegaTools_Lib.get_account_used_space')
324 | logger.setLevel(self.__log_level)
325 |
326 | cmd = 'megadf --used -h --gb -u %s -p %s' % (username, password)
327 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
328 |
329 | if err:
330 | logger.info(str(err))
331 |
332 | if not out == '':
333 | usedSpace = sub('\r', '', out).rstrip()
334 | logger.debug(' Success, could get account free space.')
335 | return usedSpace
336 | logger.debug(' Error, could NOT get account free space!')
337 | return None
338 |
339 | def get_account_total_space(self, username, password):
340 | """
341 | Get account total space in gigabytes
342 |
343 | Args:
344 | username (str): username for MEGA account
345 | password (str): password for MEGA account
346 |
347 | Returns:
348 | int: Total space of account in gigabytes.
349 | """
350 |
351 | logger = getLogger('MegaTools_Lib.get_account_total_space')
352 | logger.setLevel(self.__log_level)
353 |
354 | cmd = 'megadf --total -h --gb -u %s -p %s' % (username, password)
355 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
356 |
357 | if err:
358 | logger.info(str(err))
359 |
360 | if not out == '':
361 | totalSpace = sub('\r', '', out).rstrip()
362 | logger.debug(' Success, could get account total space.')
363 | return totalSpace
364 | logger.debug(' Error, could NOT get account total space!')
365 | return None
366 |
367 | def get_remote_dir_size(self, username, password, localDirPath, localRoot, remoteRoot):
368 | """
369 | Get remote directory sizes of equivalent local file path
370 |
371 | Args:
372 | username (str): username for MEGA account
373 | password (str): password for MEGA account
374 | localDirPath (str): Local directory path of remote file size to get
375 | localRoot (str): Local root path of local account files to map with remote root.
376 | remoteRoot (str): Remote root path of remote accounts to map with local root.
377 |
378 | Returns:
379 | tuple: Remote directory size and remote directory path
380 | """
381 |
382 | logger = getLogger('MegaTools_Lib.get_remote_dir_size')
383 | logger.setLevel(self.__log_level)
384 |
385 | remotePath = self.__lib.get_remote_path_from_local_path(localPath=localDirPath, localRoot=localRoot,
386 | remoteRoot=remoteRoot)
387 |
388 | if remotePath:
389 |
390 | cmd = 'megals -lR -u %s -p %s "%s"' % (username, password, remotePath)
391 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
392 |
393 | lines = out.split(linesep)
394 | totalRemoteDirSize = 0
395 | for line in lines:
396 | line_split = line.split()
397 | if len(line_split) > 2:
398 | remoteFileSize = line_split[3]
399 | if remoteFileSize.isdigit():
400 | totalRemoteDirSize = totalRemoteDirSize + int(remoteFileSize)
401 |
402 | logger.debug(' Success, could get remote directory size.')
403 | return totalRemoteDirSize
404 |
405 | logger.debug(' Error, could NOT get remote directory size!')
406 | return None
407 |
408 | def get_remote_dirs(self, username, password, remote_path):
409 | """
410 | Get remote directories
411 |
412 | Args:
413 | username (str): username of account to get remote directories from
414 | password (str): password of account to get remote directories from
415 | remote_path (str): Remote root path of remote accounts to map with local root.
416 |
417 | Returns:
418 | list: returns list of directories
419 | """
420 |
421 | logger = getLogger('MegaTools_Lib.get_remote_dirs')
422 | logger.setLevel(self.__log_level)
423 |
424 | logger.debug(' Get remote directories.')
425 |
426 | cmd = ['start', '/B', 'megals', '-u', '%s' % username, '-p', '%s' % password, '"%s"' % remote_path]
427 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
428 |
429 | dirs = out.split(linesep)
430 | dirList = []
431 |
432 | for dir in dirs:
433 | dirName = sub('%s' % remote_path, '', dir)
434 | if not dirName == '':
435 | dirList.append(sub('/', '', dirName))
436 |
437 | return dirList
438 |
439 | def get_remote_file_data_recursively(self, username, password, remote_path='/', remove_blank_lines=False):
440 | """
441 | Get all remote file data as list. This includes file path, modified date/time, file size, file type (file or dir),
442 |
443 | Args:
444 | username (str): username of MEGA account.
445 | password (str): password of MEGA account.
446 | remote_path (str): root path to get remote files from.
447 | remove_blank_lines (bool): If set to true output list will not contain empty strings.
448 | Returns:
449 | List: list of remote file data in given remote_path.
450 | """
451 |
452 | logger = getLogger('MegaTools_Lib.get_remote_file_data_recursively')
453 | logger.setLevel(self.__log_level)
454 |
455 | cmd = 'megals -lR -u %s -p %s "%s"' % (username, password, remote_path)
456 |
457 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd, output_file=self.__mega_tools_log)
458 |
459 | if not err:
460 | if not out == '':
461 | lines = out.split(linesep)
462 | if remove_blank_lines:
463 | lines = list(filter(None, lines)) # fastest
464 | logger.debug(' Success, could get remote file data recursievly.')
465 | return lines
466 |
467 | logger.warning(str(err))
468 | return None
469 |
470 | def get_remote_file_modified_date(self, username, password, remotePath):
471 | """
472 | Get remote file modified date of equivalent local file path
473 |
474 | Args:
475 | username (str): username for MEGA account
476 | password (str): password for MEGA account
477 | remotePath (str): Remote file path of remote file size to get
478 |
479 | Returns:
480 | Tuple: Remote file modified data and remote file path
481 | """
482 |
483 | logger = getLogger('MegaTools_Lib.get_remote_file_modified_date')
484 | logger.setLevel(self.__log_level)
485 |
486 | cmd = 'megals -ln -u %s -p %s "%s"' % (username, password, remotePath)
487 | out, err = self.__lib.exec_cmd_and_return_output(cmd)
488 | line_split = out.split()
489 |
490 | if len(line_split) > 2:
491 | remoteFileModifiedDate = line_split[4]
492 | remoteFileModifiedTime = line_split[5]
493 |
494 | remoteFileModifiedDate_time = '%s %s' % (remoteFileModifiedDate, remoteFileModifiedTime)
495 |
496 | logger.debug(' Success, could find remote file modified date.')
497 | return remoteFileModifiedDate_time
498 |
499 | logger.warning(' Error, could NOT find remote file modified date!')
500 | return None
501 |
502 | def get_remote_file_size(self, username, password, remotePath='/'):
503 | """
504 | Get remote file size in bytes of given remote path.
505 |
506 | Args:
507 | username (str): username for MEGA account
508 | password (str): password for MEGA account
509 | remotePath (str): remote path of file to get size for
510 |
511 | Returns:
512 | int: remote file size
513 | """
514 |
515 | logger = getLogger('MegaTools_Lib.get_remote_file_size')
516 | logger.setLevel(self.__log_level)
517 |
518 | cmd = 'megals -ln -u %s -p %s "%s"' % (username, password, remotePath)
519 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
520 |
521 | line_split = out.split()
522 | if len(line_split) > 2:
523 | remoteFileSize = line_split[3]
524 | logger.debug(' Success, remote file size for path "%s" is "%s"' % (remotePath, remoteFileSize))
525 | return int(remoteFileSize)
526 | else:
527 | logger.error(' Error, could not get remote file size of path "%s"' % remotePath)
528 | return None
529 |
530 |
531 | def get_remote_file_size_from_local_path(self, username, password, localFilePath, localRoot, remoteRoot):
532 | """
533 | Get remote file sizes of equivalent local file path
534 |
535 | Args:
536 | username (str): username for MEGA account
537 | password (str): password for MEGA account
538 | localFilePath (str): Local file path of remote file size to get
539 | localRoot (str): Local root path of local account files to map with remote root.
540 | remoteRoot (str): Remote root path of remote accounts to map with local root.s
541 |
542 | Returns:
543 | int: remote file size
544 | """
545 |
546 | logger = getLogger('MegaTools_Lib.get_remote_file_size_from_local_path')
547 | logger.setLevel(self.__log_level)
548 |
549 | remotePath = self.__lib.get_remote_path_from_local_path(localPath=localFilePath, localRoot=localRoot,
550 | remoteRoot=remoteRoot)
551 | if remotePath:
552 | remoteFileSize = self.get_remote_file_size(username=username, password=password, remotePath=remotePath)
553 | return remoteFileSize
554 |
555 | return None
556 |
557 | def get_remote_file_paths_recursively(self, username, password, remote_path='/', process_priority_class=None,
558 | process_set_priority_timeout=60):
559 | """
560 | Get remote files list.
561 |
562 | Args:
563 | username (str): username of MEGA account.
564 | password (str): password of MEGA account.
565 | remote_path (str): root path to get remote files from.
566 | process_priority_class (str): Priority level to set process to. ie: "NORMAL_PRIORITY_CLASS"
567 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
568 |
569 | Returns:
570 | List: list of remote files in given remote_path.
571 | """
572 | logger = getLogger('MegaTools_Lib.get_remote_file_paths_recursively')
573 | logger.setLevel(self.__log_level)
574 |
575 | cmd = 'megals -R -u %s -p %s "%s"' % (username, password, remote_path)
576 |
577 | # out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
578 |
579 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd, output_file=self.__mega_tools_log)
580 |
581 | if not err:
582 | if not out == '':
583 | lines = out.split(linesep)
584 | remote_files = []
585 | for line in lines:
586 | if not line == '' and len(findall(r"\?", line)) == 0:
587 | remote_files.append(line)
588 | logger.debug(' Success, could get remote file paths.')
589 | return remote_files
590 |
591 | self.create_remote_dir(username=username, password=password, remote_path=remote_path,
592 | process_priority_class=process_priority_class, process_set_priority_timeout=process_set_priority_timeout)
593 | logger.warning(' Warning: {}'.format(err))
594 | logger.warning(' Error in megals output. Returning "None".')
595 | return None
596 |
597 | def get_remote_subdir_names_only(self, username, password, remote_path):
598 | """
599 | Get remote sub directory names only.
600 | Only the subdirectories immediately under remote_path are gotten.
601 |
602 | Args:
603 | username (str): username of account to get remote directories from
604 | password (str): password of account to get remote directories from
605 | remote_path (str): Remote root path of remote accounts to map with local root.
606 |
607 | Returns:
608 | list: sub directory names.
609 | """
610 |
611 | logger = getLogger('MegaTools_Lib.get_remote_subdir_names_only')
612 | logger.setLevel(self.__log_level)
613 |
614 | remote_root = remote_path + '/'
615 | cmd = 'start /B megals -n -u %s -p %s "%s"' % (username, password, remote_root)
616 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd)
617 |
618 | if not err:
619 | if not out == '':
620 | lines = out.split(linesep)
621 | logger.debug(' Success, could get remote sub directory names.')
622 | return lines
623 |
624 | logger.warning(str(err))
625 | return None
626 |
627 | def is_remote_dir(self, username, password, remote_dir_path):
628 | """
629 | Determines if remote path is an existing remote direcotry.
630 |
631 | Args:
632 | username (str): MEGA profile username.
633 | password (str): MEGA profile password.
634 | remote_dir_path (str): MEGA profile remote directory path to check for existance.
635 |
636 | Returns:
637 | Bool: Whether true or not.
638 | """
639 | logger = getLogger('MegaTools_Lib.is_remote_dir')
640 | logger.setLevel(self.__log_level)
641 | logger.info(' Determining if remote directory path exists: "{}"'.format(remote_dir_path))
642 | remote_dirs = self.get_remote_dirs(username=username, password=password, remote_path=remote_dir_path)
643 | if len(remote_dirs) < 1:
644 | logger.info(' Remote directory path does NOT exist! "{}"'.format(remote_dir_path))
645 | return False
646 | logger.info(' Remote directory path does exist: "{}"'.format(remote_dir_path))
647 | return True
648 |
649 | def is_temp_mega_file(self, file_path):
650 | """
651 | Determines if file is temp MEGA file.
652 |
653 | Args:
654 | file_path (str): File path.
655 |
656 | Returns:
657 | Bool: Whether successful or not.
658 | """
659 | logger = getLogger('MegaTools_Lib.remove_temp_mega_files')
660 | logger.setLevel(self.__log_level)
661 | logger.info(' Determining if file is temp mega file: "{}"'.format(file_path))
662 | try:
663 | if len(findall(r'^.*\.megatmp\..*$', file_path)) > 0:
664 | logger.info(' File is temp mega file: "{}"'.format(file_path))
665 | return True
666 |
667 | logger.info(' File is NOT temp mega file: "{}"'.format(file_path))
668 | return False
669 |
670 | except Exception as e:
671 | logger.error(' Exception: {}'.format(e))
672 | return False
673 |
674 | def remove_remote_path(self, username, password, remote_file_path, process_priority_class="NORMAL_PRIORITY_CLASS", process_set_priority_timeout=60):
675 | """
676 | Remove remote file or directory.
677 |
678 | Args:
679 | username (str): username of account to upload to
680 | password (str): password of account to upload to
681 | remote_file_path (str): remote file path to remove.
682 | process_priority_class (str): Priority level to set for process. ie: "NORMAL_PRIORITY_CLASS".
683 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
684 |
685 | Returns:
686 | boolean: whether successful or not.
687 | """
688 |
689 | logger = getLogger('MegaTools_Lib.remove_remote_path')
690 | logger.setLevel(self.__log_level)
691 |
692 | logger.debug(' %s - %s: Removing remote file "%s".' % (username, password, remote_file_path))
693 |
694 | cmd = 'megarm -u %s -p %s "%s"' % (username, password, remote_file_path)
695 |
696 | process_name = 'megarm.exe' if system() == 'Windows' else 'megarm'
697 | result = self.__lib.exec_cmd(command=cmd, process_name=process_name, process_priority_class=process_priority_class,
698 | process_set_priority_timeout=process_set_priority_timeout)
699 |
700 | if result:
701 | logger.debug(' Success, could remove remote file: "{}"'.format(remote_file_path))
702 | return True
703 | else:
704 | logger.debug(' Error, could NOT remove remote file! "{}"'.format(remote_file_path))
705 | return False
706 |
707 | def upload_local_dir(self, username, password, local_dir, remote_path, process_priority_class=None,
708 | process_set_priority_timeout=60):
709 | """
710 | Upload directory.
711 |
712 | username (str): username of account to upload to
713 | password (str): password of account to upload to
714 | local_dir (str): Local directory to upload
715 | remote_path (str): Remote directory to upload to
716 | process_priority_class (str): Priority level to set process to. ie: "NORMAL_PRIORITY_CLASS"
717 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
718 |
719 | Returns:
720 | boolean: whether successful or not.
721 | """
722 |
723 | logger = getLogger('MegaTools_Lib.upload_local_dir')
724 | logger.setLevel(self.__log_level)
725 |
726 | logger.debug('%s - %s: Uploading files in directory "%s"' % (username, password, local_dir))
727 |
728 | if not self.is_remote_dir(username=username, password=password, remote_dir_path=remote_path):
729 | self.create_remote_dir(username=username, password=password, remote_path=remote_path,
730 | process_priority_class=process_priority_class,
731 | process_set_priority_timeout=process_set_priority_timeout)
732 |
733 | if self.__up_speed_limit:
734 | cmd = 'megacopy -u %s -p %s --limit-speed %d --local "%s" --remote "%s"' % (username, password,
735 | self.__up_speed_limit, local_dir, remote_path)
736 | else:
737 | cmd = 'megacopy -u %s -p %s --local "%s" --remote "%s"' % (username, password, local_dir, remote_path)
738 |
739 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd, output_file=self.__mega_tools_log)
740 |
741 | if out and not err:
742 | logger.debug(' Success, uploaded local directory: {}'.format(local_dir))
743 | return True
744 |
745 | logger.warning(' Warning: {}'.format(err))
746 | return False
747 |
748 | def upload_to_account(self, username, password, local_root, remote_root, process_priority_class=None,
749 | process_set_priority_timeout=60):
750 | """
751 | Upload all files to account.
752 |
753 | Args:
754 | username (str): username of account to __upload to
755 | password (str): password of account to __upload to
756 | local_root (str): Local root path of local account files to map with remote root.
757 | remote_root (str): Remote root path of remote accounts to map with local root.
758 | process_priority_class (str): Priority level to set process to. ie: "NORMAL_PRIORITY_CLASS"
759 | process_set_priority_timeout (int): Timeout in seconds to wait for process to start after setting priority.
760 |
761 | Returns:
762 | boolean: whether successful or not.
763 | """
764 |
765 | logger = getLogger('MegaTools_Lib.upload_to_account')
766 | logger.setLevel(self.__log_level)
767 |
768 | logger.debug(' Starting uploading for %s - %s' % (username, password))
769 |
770 | self.upload_local_dir(username=username, password=password, local_dir=local_root, remote_path=remote_root,
771 | process_priority_class=process_priority_class,
772 | process_set_priority_timeout=process_set_priority_timeout)
773 |
774 | localRoot_adj = sub('\\\\', '/', local_root)
775 |
776 | cmd = 'megals -ln -u %s -p %s "%s"' % (username, password, remote_root)
777 | out, err = self.__lib.exec_cmd_and_return_output(command=cmd, output_file=self.__mega_tools_log)
778 |
779 | if not err:
780 | lines = out.split(linesep)
781 | for line in lines:
782 | if not line == '':
783 | if len(split(r':\d{2} ', line)) > 1:
784 | remote_filePath = split(r':\d{2} ', line)[1]
785 | dir_subPath = sub(remote_root, '', remote_filePath)
786 | local_dir = localRoot_adj + '/' + dir_subPath
787 | remote_dir = remote_root + '/' + dir_subPath
788 | if path.exists(local_dir):
789 | self.upload_local_dir(username, password, local_dir, remote_dir)
790 |
791 | logger.debug('Success, could upload files to account.')
792 | return True
793 |
794 | logger.warning(str(err))
795 | logger.debug(' Error, could NOT upload files to account!')
796 | return False
797 |
798 |
799 | class MegaToolsFile(object):
800 | def __init__(self, file_details, log_level='DEBUG', log_file_path=MEGATOOLS_LOG_PATH):
801 | """
802 | Class for Mega Tools files and extracting data about file given mega tools file output using "megals --long".
803 | For more info: https://megatools.megous.com/man/megals.html
804 |
805 | Args:
806 | file_details(str): File details line as shown using "megals --long".
807 | ie: 2FFSiaKZ Xz2tWWB5Dmo 0 2686 2013-04-15 08:33:47 /Root/directory/file.txt
808 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
809 | log_file_path (str): MEGATools log file path.
810 |
811 | """
812 | self.__log_level = log_level
813 | self.__mega_tools_file_log = log_file_path
814 |
815 |
816 |
817 |
--------------------------------------------------------------------------------
/megamanager/mega_manager.py:
--------------------------------------------------------------------------------
1 | ###
2 | # Created by: Curtis Szmania
3 | # Date: 10/1/2015
4 | # Initial Creation
5 | ###
6 |
7 | import tempfile
8 | import shutil
9 | import sys
10 | from ast import literal_eval
11 | from datetime import datetime
12 | from configparser import ConfigParser
13 | from importlib import reload # Import reload from importlib in Python 3
14 | from logging import DEBUG, getLogger, Formatter, StreamHandler, handlers
15 | from libs.lib import Lib
16 | from libs.compress_images_lib import CompressImages_Lib
17 | from libs.ffmpeg_lib import FFMPEG_Lib
18 | from libs.mega_tools_lib import MegaTools_Lib
19 | from os import path, remove, sep, walk
20 | from path_mapping import PathMapping
21 | from platform import system
22 | from random import shuffle
23 | from re import findall, IGNORECASE, match, search, sub
24 | from string import Formatter as string_formatter
25 | from syncprofile import SyncProfile
26 | from sys import stdout
27 | from threading import Thread
28 | from time import sleep, time
29 |
30 |
31 | reload(sys)
32 |
33 | __author__ = 'szmania'
34 |
35 | HOME_DIRECTORY = path.expanduser("~")
36 |
37 |
38 | # SLEEP_TIME_BETWEEN_RUNS_SECONDS = 300 # 5 minutes
39 | WORKING_DIR = path.dirname(path.realpath(__file__))
40 | MAX_PATH_MAPPING_EXIST_ATTEMPTS = 100
41 | PATH_MAPPING_TIMEOUT_SECONDS = 5
42 |
43 |
44 | class ConfigError(Exception):
45 | pass
46 |
47 |
48 | class MegaManager(object):
49 | def __init__(self, **kwargs):
50 | self.__threads = []
51 | self.__download = None
52 | self.__sync = None
53 | self.__upload = None
54 | self.__local_is_truth = None
55 | self.__remove_outdated = None
56 | self.__compress_all = None
57 | self.__compress_images = None
58 | self.__compress_videos = None
59 | self.__log_level = None
60 |
61 | self.__compressed_image_files = set()
62 | self.__compressed_video_files = set()
63 | self.__unable_to_compress_image_files = set()
64 | self.__unable_to_compress_video_files = set()
65 | self.__removed_remote_files = set()
66 |
67 | self.__sleep_time_between_runs_seconds = None
68 |
69 | self.__mega_manager_config_dir_path = None
70 | self.__mega_manager_config_dir_data_path = None
71 | self.__mega_manager_log_path = None
72 | self.__mega_manager_output_profile_data_path = None
73 | self.__compressed_images_file_path = None
74 | self.__compressed_videos_file_path = None
75 | self.__compression_image_extensions = []
76 | self.__compression_video_extensions = []
77 | self.__image_temp_file_extensions = []
78 | self.__compression_ffmpeg_video_max_width = None
79 | self.__compression_ffmpeg_video_preset = None
80 | self.__removed_remote_files_path = None
81 | self.__unable_to_compress_images_file_path = None
82 | self.__unable_to_compress_videos_file_path = None
83 | self.__process_set_priority_timeout = None
84 | self.__megatools_process_priority_class = None
85 | self.__megatools_log_path = None
86 | self.__mega_download_speed = None
87 | self.__mega_upload_speed = None
88 | self.__ffmpeg_process_priority_class = None
89 | self.__ffmpeg_log_path = None
90 | self.__ffmpeg_threads = None
91 | self.__max_video_compression_threads = None
92 |
93 | self.__sync_profiles = []
94 |
95 | self._setup(kwargs=kwargs)
96 |
97 | def _assign_attributes(self, **kwargs):
98 | """
99 | Assign argumetns to class attributes.
100 |
101 | Args:
102 | kwargs (dict): Dictionary of arguments.
103 | """
104 | for key, value in kwargs.items():
105 | if value:
106 | setattr(self, '_MegaManager__%s' % key, value)
107 |
108 | def _compress_image_file(self, file_path):
109 | """
110 | Compress given image file path.
111 |
112 | Args:
113 | file_path: Path to image file to compress.
114 |
115 | Returns:
116 | Boolean: Whether compression operation was successful or not.
117 | """
118 | logger = getLogger('MegaManager._compress_image_file')
119 | logger.setLevel(self.__log_level)
120 | logger.debug(' Compressing image file: "{}"'.format(file_path))
121 | compressed = self.__compress_images_lib.compress_image_file(file_path=file_path,
122 | jpeg_compression_quality_percentage=
123 | self.__compression_jpeg_quality_percentage,
124 | delete_backup=True,
125 | delete_corrupt_images=True)
126 | if compressed:
127 | logger.debug(' Image file compressed successfully "%s"!' % file_path)
128 | file_md5_hash = self.__lib.get_file_md5_hash(file_path)
129 | self.__compressed_image_files.add(file_md5_hash)
130 | self.__lib.dump_set_into_numpy_file(item_set=self.__compressed_image_files,
131 | file_path=self.__compressed_images_file_path)
132 | return True
133 |
134 | else:
135 | logger.debug(' Error, image file could not be compressed "%s"!' % file_path)
136 | file_md5_hash = self.__lib.get_file_md5_hash(file_path)
137 | self.__unable_to_compress_image_files.add(file_md5_hash)
138 | self.__lib.dump_set_into_numpy_file(item_set=self.__unable_to_compress_image_files,
139 | file_path=self.__unable_to_compress_images_file_path)
140 | return False
141 |
142 | def _compress_image_files(self, file_list):
143 | """
144 | Find image files to compress.
145 |
146 | Args:
147 | file_list (list[str]): List of all files to compress.
148 |
149 | Returns:
150 | Boolean: Returns whether operation was successful or not.
151 | """
152 | logger = getLogger('MegaManager._compress_image_files')
153 | logger.setLevel(self.__log_level)
154 | logger.debug(' Compressing image files.')
155 |
156 | try:
157 | for local_file_path in file_list:
158 | local_file_ext = local_file_path.split('.')[-1]
159 | if local_file_ext.lower() in self.__compression_image_extensions:
160 | while not path.exists(local_file_path):
161 | sleep(1)
162 | if local_file_ext in self.__image_temp_file_extensions or search(r'^.*\.megatmp\..*$', local_file_path):
163 | logger.warning(' File "{}" is temporary file. Deleting.'.format(local_file_path))
164 | self.__lib.delete_local_file(local_file_path)
165 | continue
166 |
167 | file_md5_hash = self.__lib.get_file_md5_hash(local_file_path)
168 | if (file_md5_hash not in self.__compressed_image_files) \
169 | and (file_md5_hash not in self.__unable_to_compress_image_files):
170 | self._compress_image_file(file_path=local_file_path)
171 | else:
172 | logger.debug(' Image file already compressed or previously unable to compress: "{}"'.format(local_file_path))
173 |
174 | logger.debug(' Success, finished compressing image files.')
175 | return True
176 |
177 | except Exception as e:
178 | logger.warning(' Exception: {}'.format(e))
179 | return False
180 |
181 | def _compress_video_file(self, orig_file_path):
182 | """
183 | Compress given video file path.
184 |
185 | Args:
186 | orig_file_path: Path to video file to compress.
187 |
188 | Returns:
189 | Boolean: Whether compression operation was successful or not.
190 | """
191 | logger = getLogger('MegaManager._compress_video_file')
192 | logger.setLevel(self.__log_level)
193 |
194 | logger.debug(' Compressing video file: "{}"'.format(orig_file_path))
195 | temp_dir = tempfile.gettempdir()
196 | temp_file_path = path.join(temp_dir, path.basename(orig_file_path))
197 | if path.exists(temp_file_path):
198 | self.__lib.delete_local_file(file_path=temp_file_path)
199 | logger.debug(f' Copying video file from "{orig_file_path}" to "{temp_file_path}".')
200 | shutil.copy(orig_file_path, temp_file_path)
201 | logger.debug(f' Finished copying video file from "{orig_file_path}" to "{temp_file_path}".')
202 | new_file_path = temp_file_path.rsplit(".", 1)[0] + '_NEW.mp4'
203 | result = self.__ffmpeg_lib.compress_video_file(source_path=temp_file_path, target_path=new_file_path,
204 | compression_max_width=self.__compression_ffmpeg_video_max_width,
205 | compression_preset=self.__compression_ffmpeg_video_preset,
206 | ffmpeg_threads=self.__ffmpeg_threads, overwrite=True,
207 | process_priority_class=self.__ffmpeg_process_priority_class,
208 | process_set_priority_timeout=self.__process_set_priority_timeout)
209 | self._compress_video_file_teardown(result, orig_file_path, temp_file_path, new_file_path)
210 | return result
211 |
212 | def _compress_video_file_setup(self, file_path, temp_file_path):
213 | """
214 | Setup for compress video file.
215 |
216 | Args:
217 | file_path (str): File path to compress.
218 | temp_file_path (str): Temporary file path.
219 | """
220 | logger = getLogger('MegaManager._compress_video_file_setup')
221 | logger.setLevel(self.__log_level)
222 | logger.debug(' Video file compression setup.')
223 | if path.exists(temp_file_path):
224 | self.__lib.delete_local_file(file_path=temp_file_path)
225 |
226 | for ext in self.__compression_video_extensions:
227 | possible_prev_file_path = file_path.rsplit(".", 1)[0] + ext
228 | if path.exists(possible_prev_file_path):
229 | self.__lib.delete_local_file(file_path=possible_prev_file_path)
230 |
231 | def _compress_video_file_teardown(self, result, orig_file_path, temp_file_path, new_file_path):
232 | """
233 | Teardown for compress video file.
234 |
235 | Args:
236 | result (bool): Result of video file compression.
237 | orig_file_path (str): Original file path to compress.
238 | temp_file_path (str): Temporary file path that is the source file to compress.
239 | new_file_path (str): Temporary compress target file path with "_NEW" appended.
240 | """
241 | logger = getLogger('MegaManager._compress_video_file_teardown')
242 | logger.setLevel(self.__log_level)
243 |
244 | logger.debug(' Video file compression teardown.')
245 |
246 | if result and path.exists(new_file_path):
247 | logger.debug(' Video file could be compressed "%s"!' % temp_file_path)
248 | dir_path = path.dirname(orig_file_path)
249 | final_file_path = path.join(dir_path, sub('_NEW', '', path.basename(new_file_path)))
250 | if path.exists(temp_file_path):
251 | self.__lib.delete_local_file(file_path=temp_file_path)
252 | if path.exists(new_file_path):
253 | orig_file_size = path.getsize(orig_file_path)
254 | new_file_size = path.getsize(new_file_path)
255 | if orig_file_size > new_file_size:
256 | logger.debug(f' Original file size ({orig_file_size} ) is larger than the new file size ({new_file_size}). Replacing old file with new: "{orig_file_path}"')
257 | if path.exists(final_file_path):
258 | self.__lib.delete_local_file(file_path=final_file_path)
259 | self.__lib.rename_file(old_name=new_file_path, new_name=final_file_path)
260 | if path.exists(orig_file_path):
261 | self.__lib.delete_local_file(file_path=orig_file_path)
262 | if path.exists(new_file_path):
263 | self.__lib.delete_local_file(file_path=new_file_path)
264 | else:
265 | logger.debug(f' Original file size ({orig_file_size}) is smaller or equal to the new file size ({new_file_size}). Not going to replace the old file: "{orig_file_path}"')
266 | logger.debug(' Video file compressed successfully "%s" into "%s"!' % (
267 | orig_file_path, final_file_path))
268 | file_md5_hash = self.__lib.get_file_md5_hash(final_file_path)
269 | self.__compressed_video_files.add(file_md5_hash)
270 | self.__lib.dump_set_into_numpy_file(item_set=self.__compressed_video_files, file_path=self.__compressed_videos_file_path)
271 | else:
272 | logger.debug(' Error, video file could not be compressed "%s"!' % temp_file_path)
273 | logger.debug(' Error, video file could not be compressed "%s"!' % temp_file_path)
274 | if path.exists(new_file_path):
275 | logger.debug(' Deleting temporary NEW file "%s"!' % new_file_path)
276 | self.__lib.delete_local_file(file_path=new_file_path)
277 | if path.exists(temp_file_path):
278 | file_md5_hash = self.__lib.get_file_md5_hash(temp_file_path)
279 | self.__unable_to_compress_video_files.add(file_md5_hash)
280 | self.__lib.dump_set_into_numpy_file(item_set=self.__unable_to_compress_video_files,
281 | file_path=self.__unable_to_compress_videos_file_path)
282 | logger.debug(' Deleting temporary file "%s"!' % temp_file_path)
283 | self.__lib.delete_local_file(file_path=temp_file_path)
284 |
285 | logger.debug(' Successfully completed video file compression teardown.')
286 |
287 | def _compress_video_files(self, file_list):
288 | """
289 | Find video files to compress.
290 |
291 | Args:
292 | file_list (list[str]): List of all files to compress.
293 |
294 | Returns:
295 | Boolean: Whether operation is successful or not.
296 | """
297 | logger = getLogger('MegaManager._compress_video_files')
298 | logger.setLevel(self.__log_level)
299 | logger.debug(' Finding video files to compress.')
300 | try:
301 | for local_file_path in file_list:
302 | local_file_ext = local_file_path.split('.')[-1]
303 | if local_file_ext.lower() in self.__compression_video_extensions:
304 | while not path.exists(local_file_path):
305 | sleep(1)
306 | if local_file_path.endswith('_NEW.mp4') or search(r'^.*\.megatmp\..*$', local_file_path):
307 | logger.warning(' File "{}" is a temporary file. Deleting.'.format(local_file_path))
308 | self.__lib.delete_local_file(local_file_path)
309 | continue
310 | file_md5_hash = self.__lib.get_file_md5_hash(local_file_path)
311 | if (file_md5_hash not in self.__compressed_video_files) \
312 | and (file_md5_hash not in self.__unable_to_compress_video_files):
313 | self._compress_video_file(orig_file_path=local_file_path)
314 | else:
315 | logger.debug(' Video file already compressed or previously unable to compress: "{}"'.format(local_file_path))
316 | logger.debug(' Success, finished compressing video files.')
317 | return True
318 | except Exception as e:
319 | logger.warning(' Exception: {}'.format(e))
320 | return False
321 |
322 | def _create_thread_get_profile_data(self, profile):
323 | """
324 | Create thread to create profiles data file.
325 |
326 | Args:
327 | profile (Profile): Profile object
328 |
329 | Returns:
330 | Boolean: Whether operation was successful or not.
331 | """
332 |
333 | logger = getLogger('MegaManager._create_thread_get_profile_data')
334 | logger.setLevel(self.__log_level)
335 |
336 | try:
337 | t = Thread(target=self._get_profile_data, args=(profile,),
338 | name='thread_get_profile_data_{}'.format(profile.profile_name))
339 | self.__threads.append(t)
340 | t.start()
341 | return True
342 | except Exception as e:
343 | logger.warning('Exception: {}'.format(e))
344 | return False
345 |
346 | def _create_thread_download(self, profile):
347 | """
348 | Create thread to download files.
349 |
350 | Args:
351 | profile (Profile): Profile object
352 |
353 | Returns:
354 | Boolean: Whether operation was successful or not.
355 | """
356 |
357 | logger = getLogger('MegaManager._create_thread_download')
358 | logger.setLevel(self.__log_level)
359 |
360 | try:
361 | logger.debug(' Creating thread to download files from MEGA account profile {}.'.format(profile.profile_name))
362 |
363 | t = Thread(target=self._thread_download_profile_files, args=(profile,),
364 | name='thread_download_{}'.format(profile.profile_name))
365 | self.__threads.append(t)
366 | t.start()
367 | return True
368 | except Exception as e:
369 | logger.warning(' Exception: {}'.format(e))
370 | return False
371 |
372 | def _create_thread_upload(self, profile):
373 | """
374 | Create thread to upload files.
375 |
376 | Args:
377 | profile (Profile): Profile object
378 |
379 | Returns:
380 | Boolean: Whether operation was successful or not.
381 | """
382 |
383 | logger = getLogger('MegaManager._create_thread_upload')
384 | logger.setLevel(self.__log_level)
385 |
386 | logger.debug(' Creating thread to upload files to MEGA accounts.')
387 |
388 | try:
389 | t = Thread(target=self._thread_upload_profile_files, args=(profile, ),
390 | name='thread_upload_{}'.format(profile.profile_name))
391 | self.__threads.append(t)
392 | t.start()
393 | return True
394 | except Exception as e:
395 | logger.warning(' Exception: {}'.format(e))
396 | return False
397 |
398 | def _create_thread_remove_remote_files_that_dont_exist_locally(self, profile):
399 | """
400 | Create threads to remove remote files that don't exist locally.
401 |
402 | Args:
403 | profile (Profile): Profile object
404 |
405 | Returns:
406 | Boolean: Whether successful or not.
407 | """
408 | logger = getLogger('MegaManager._create_thread_remove_remote_files_that_dont_exist_locally')
409 | logger.setLevel(self.__log_level)
410 | try:
411 | logger.debug(' Creating thread to remove remote files that dont exist locally for profile {}.'.format(
412 | profile.profile_name))
413 | t_remove_remote_files_dont_exist_locally = Thread(target=self._thread_remove_remote_files_that_dont_exist_locally,
414 | args=(profile,),
415 | name='thread_remove_remote_files_dont_exist_locally_{}'.format(
416 | profile.profile_name))
417 | self.__threads.append(t_remove_remote_files_dont_exist_locally)
418 | t_remove_remote_files_dont_exist_locally.start()
419 | return True
420 |
421 | except Exception as e:
422 | logger.error(' Exception: {}'.format(e))
423 | return False
424 |
425 | def _create_thread_compress_image_files(self, file_list):
426 | """
427 | Create thread to compress image files.
428 |
429 | Args:
430 | file_list (list[str]): List of files to compress.
431 |
432 | Returns:
433 | Boolean: Whether successful or not.
434 | """
435 |
436 | logger = getLogger('MegaManager._create_thread_compress_image_files')
437 | logger.setLevel(self.__log_level)
438 |
439 | logger.debug(' Creating thread to compress local image files')
440 |
441 | try:
442 | t_compress = Thread(target=self._compress_image_files, args=(file_list,), name='thread_compress_images')
443 | self.__threads.append(t_compress)
444 | t_compress.start()
445 | return True
446 | except Exception as e:
447 | logger.error(' Exception: {}'.format(e))
448 | return False
449 |
450 | def _create_thread_compress_video_files(self, file_list):
451 | """
452 | Create thread to compress video files.
453 |
454 | Args:
455 | file_list (list[str]): List of files to compress.
456 |
457 | Returns:
458 | Boolean: Whether successful or not.
459 | """
460 |
461 | logger = getLogger('MegaManager._create_thread_compress_video_files')
462 | logger.setLevel(self.__log_level)
463 |
464 | logger.debug(' Creating thread to compress local video files.')
465 | try:
466 | t_compress = Thread(target=self._compress_video_files, args=(file_list,), name='thread_compress_videos')
467 | self.__threads.append(t_compress)
468 | t_compress.start()
469 | return True
470 | except Exception as e:
471 | logger.error(' Exception: {}'.format(e))
472 | return False
473 |
474 | def _create_thread_output_profile_data(self, profile):
475 | """
476 | Create thread to output profile data
477 |
478 | Args:
479 | profile (Profile): Profile object
480 |
481 | Returns:
482 | Boolean: Whether successful or not.
483 | """
484 |
485 | logger = getLogger('MegaManager._create_thread_output_profile_data')
486 | logger.setLevel(self.__log_level)
487 |
488 | logger.debug(' Creating thread to output profile data for {}'.format(profile.profile_name))
489 |
490 | try:
491 | t_output = Thread(target=self._thread_output_profile_data, args=(profile,), name='thread_output_profile_data_{}'.format(profile.profile_name))
492 | self.__threads.append(t_output)
493 | t_output.start()
494 | return True
495 | except Exception as e:
496 | logger.error(' Exception: {}'.format(e))
497 | return False
498 |
499 | def _export_config_file_data(self, config_parser):
500 | """
501 | Export config file data.
502 |
503 | Args:
504 | config_parser (ConfigParser): Config parser object of config file.
505 |
506 | """
507 | logger = getLogger('MegaManager._export_config_file_data')
508 | logger.setLevel(self.__log_level)
509 | logger.debug(' Exporting config data to MEGA Manager config file.')
510 | try:
511 | with open(self.__mega_manager_config_path, 'w') as config_file:
512 | config_parser.write(config_file)
513 | return True
514 | except Exception as e:
515 | logger.error(' Exception: %s' % str(e))
516 | return False
517 |
518 | def _get_accounts_user_pass(self, file):
519 | """
520 | Get username and password from file with lines of " - "
521 |
522 | Args:
523 | file (str): file in format with lines:
524 |
525 | " - "
526 | " - "
527 |
528 | Where is account username and is account password.
529 |
530 | Returns:
531 | Returns list of dictionaries holding user and pass.
532 | """
533 | logger = getLogger('MegaManager._get_accounts_user_pass')
534 | logger.setLevel(self.__log_level)
535 |
536 | logger.debug(' Getting usernames and passwords.')
537 |
538 | foundUserPass = []
539 |
540 | try:
541 | with open(file, "r", encoding='utf-8') as ins:
542 | for line in ins:
543 | dict = {}
544 | if len(findall('-', line)) > 0 and len(findall('@', line)) > 0:
545 | username = sub('\\n', '', sub(' - .*', '', line))
546 | password = sub('\\n', '', sub('.* - ', '', line))
547 |
548 | dict['user'] = username
549 | dict['pass'] = password
550 | foundUserPass.append(dict)
551 | ins.close()
552 | except Exception as e:
553 | logger.warning(' Exception: %s' % str(e))
554 |
555 | return foundUserPass
556 |
557 | def _get_all_files(self, root_path):
558 | logger = getLogger('MegaManager._get_all_files')
559 | logger.setLevel(self.__log_level)
560 | logger.debug(' Getting all files in root path: {root_path}'.format(root_path=root_path))
561 | all_files = []
562 | for root, dirs, files in walk(root_path):
563 | for file in files:
564 | full_path = path.join(root, file)
565 | all_files.append(full_path)
566 | logger.debug(' Retrieved all files in root path: "{root_path}". Total number of files: {files}'.format(root_path=root_path, files=len(all_files)))
567 | return all_files
568 |
569 | def _get_profile_data(self, profile):
570 | """
571 | Create self.__mega_accounts_output_path file. File that has all fetched data of accounts and local and remote spaces of each account.
572 |
573 | Args:
574 | profile (Profile): Profile object
575 |
576 | Returns:
577 | Boolean: Whether successful or not.
578 | """
579 |
580 | logger = getLogger('MegaManager._get_profile_data')
581 | logger.setLevel(self.__log_level)
582 |
583 | try:
584 | logger.debug(' Creating thread to gather details for profile "%s".' % profile.profile_name)
585 |
586 | self._get_profile_details(profile=profile)
587 | return True
588 | except (Exception, KeyboardInterrupt)as e:
589 | logger.warning(' Exception: {}'.format(e))
590 | # if path.exists(self.__mega_accounts_output_path + '.old'):
591 | # copyfile(self.__mega_accounts_output_path + '.old', self.__mega_accounts_output_path)
592 | return False
593 |
594 | def _get_profile_details(self, profile):
595 | """
596 | Creates dictionary of account data (remote size, local size, etc...) for self.__mega_accounts_output_path file.
597 |
598 | Args:
599 | profile (SyncProfile): Profile to get data for.
600 | """
601 | self._update_account_remote_details(account=profile.account)
602 |
603 | def _get_remote_file_modified_date(self, username, password, localFilePath, localRoot, remoteRoot):
604 | """
605 | Get remote file modified date of equivalent local file path
606 |
607 | Args:
608 | username (str): username for MEGA account
609 | password (str): password for MEGA account
610 | localFilePath (str): Local file path of remote file size to get
611 | localRoot (str): Local root path of local account files to map with remote root.
612 | remoteRoot (str): Remote root path of remote accounts to map with local root.
613 |
614 | Returns:
615 | Tuple: Remote file modified data and remote file path
616 | """
617 |
618 | logger = getLogger('MegaManager.-get_remote_file_modified_date')
619 | logger.setLevel(self.__log_level)
620 |
621 | remotePath = self.__lib.get_remote_path_from_local_path(localPath=localFilePath, localRoot=localRoot,
622 | remoteRoot=remoteRoot)
623 | if remotePath:
624 | return self.__mega_tools_lib.get_remote_file_modified_date(username=username, password=password,
625 | remotePath=remotePath)
626 | logger.warning(' Remote file path could not be gotten.')
627 | return None
628 |
629 | def _get_remote_files_that_dont_exist_locally(self, username, password, local_root, remote_root):
630 | """
631 | Get remote files that don't exist locally.
632 |
633 | Args:
634 | username (str): username of account to upload to
635 | password (str): Password of account to upload to
636 | local_root (str): Local path to download file to
637 | remote_root (str): Remote path of file to download
638 |
639 | Returns:
640 | list of remote files that don't exist locally
641 | """
642 |
643 | logger = getLogger('MegaManager._get_remote_files_that_dont_exist_locally')
644 | logger.setLevel(self.__log_level)
645 |
646 | logger.debug(' Getting remote files that do not exist locally on %s - %s.' % (username, password))
647 |
648 | remote_files = self.__mega_tools_lib.get_remote_file_paths_recursively(username=username, password=password,
649 | remote_path=remote_root,
650 | process_priority_class=self.__megatools_process_priority_class,
651 | process_set_priority_timeout=self.__process_set_priority_timeout)
652 | dont_exist_locally = []
653 | try:
654 | if remote_files:
655 | for remote_file_path in remote_files:
656 | file_sub_path = sub(remote_root, '', remote_file_path)
657 | local_file_path = path.join(local_root, path.abspath(file_sub_path))
658 |
659 | if not path.exists(local_file_path):
660 | found = False
661 | for not_exist_local_file_path in dont_exist_locally:
662 | if local_file_path in not_exist_local_file_path:
663 | found = True
664 | break
665 | if not found:
666 | dont_exist_locally.append(remote_file_path)
667 |
668 | except Exception as e:
669 | logger.warning(' Exception: {}'.format(e))
670 | finally:
671 | return dont_exist_locally
672 |
673 | def _import_config_file_data(self, ignore_config_actions):
674 | """
675 | Load config file data.
676 |
677 | Args:
678 | ignore_config_actions (bool): Whether to ignore config file OPTIONS section
679 |
680 | Returns:
681 | ConfigParser: Config parser object.
682 | """
683 | print(' Loading MEGA Manager config file.')
684 |
685 | config_parser = ConfigParser()
686 | try:
687 | print(' Loading config file: {}'.format(self.__mega_manager_config_path))
688 | if path.exists(self.__mega_manager_config_path):
689 | config_parser.read(self.__mega_manager_config_path)
690 | config_parser.file_path = self.__mega_manager_config_path
691 | else:
692 | raise Exception('Config file not found!')
693 | except Exception as e:
694 | print(' Exception: {}'.format(e))
695 | raise e
696 |
697 | self._import_config_file_properties(config_parser=config_parser, ignore_config_actions=ignore_config_actions)
698 | self._import_config_mega_profile_data(config_parser=config_parser)
699 | print(' Successfully imported sections from config file: {}'.format(config_parser.sections()))
700 | return config_parser
701 |
702 | def _import_config_mega_profile_data(self, config_parser):
703 | """
704 | Load config profile data.
705 |
706 | Args:
707 | config_parser (object): ConfigParser object.
708 | """
709 | print(' Loading MEGA profile data.')
710 | try:
711 | sync_profile_obj = None
712 | for section in config_parser:
713 | profile_name = None
714 | username = None
715 | password = None
716 | sync_profiles = []
717 | if section.startswith('PROFILE_'):
718 | profile_name = config_parser[section]['profile_name']
719 | username = config_parser[section]['username']
720 | password = config_parser[section]['password']
721 | path_mappings = []
722 |
723 | for entry in config_parser[section]:
724 | local_path = None
725 | remote_path = None
726 | path_mapping_entry = None
727 | if entry.startswith('local_path_'):
728 | local_path = config_parser[section][entry]
729 | remote_path = config_parser[section][entry.replace('local_path_','remote_path_')]
730 | path_mapping_entry = PathMapping(local_path=local_path, remote_path=remote_path, log_level=self.__log_level)
731 | path_mappings.append(path_mapping_entry)
732 | sync_profile_obj = SyncProfile(profile_name=profile_name, username=username, password=password,
733 | path_mappings=path_mappings)
734 | self.__sync_profiles.append(sync_profile_obj)
735 | except Exception as e:
736 | print(' Exception: {}'.format(e))
737 | raise e
738 |
739 | print(' Successfully loaded MEGA profile data from config.')
740 | return sync_profile_obj
741 |
742 | def _import_config_file_properties(self, config_parser, ignore_config_actions):
743 | """
744 | Import config file data properties sections.
745 |
746 | Args:
747 | config_parser (ConfigParser): Config parser object.
748 | ignore_config_actions (bool): Whether to ignore config file OPTIONS section
749 |
750 | """
751 | print(' Loading MEGA Manager config file properties data.')
752 | config_key_values = {}
753 | sections = config_parser.sections()
754 | if not sections:
755 | raise ConfigError('No sections found in config file "{}"!'.format(config_parser.file_path))
756 |
757 | for section in config_parser.sections():
758 | if not section.startswith('PROFILE_') and (not ignore_config_actions or (ignore_config_actions and not section == 'ACTIONS')):
759 | for key, value in (config_parser[section].items()):
760 | try:
761 | placeholders_in_str = {}
762 | format_placeholders = [fn for _, fn, _, _ in string_formatter().parse(value) if fn is not None]
763 | found = False
764 | for format_placeholder in format_placeholders:
765 | if format_placeholder.lower() in config_key_values:
766 | found = True
767 | placeholders_in_str[format_placeholder] = config_key_values[format_placeholder.lower()].replace('"','')
768 | if 'sep' in format_placeholders:
769 | found = True
770 | placeholders_in_str['sep'] = sep
771 | if 'HOME_DIRECTORY' in format_placeholders:
772 | found = True
773 | placeholders_in_str['HOME_DIRECTORY'] = HOME_DIRECTORY.replace('"','')
774 | if found:
775 | value = value.format(**placeholders_in_str)
776 | setattr(self, '_MegaManager__{}'.format(key.lower()), literal_eval(value))
777 | config_key_values[key] = value
778 | except Exception as e:
779 | print(' Exception: {}'.format(e))
780 | raise e
781 |
782 | print(' Successfully loaded MEGA Manager config file properties data.')
783 |
784 | def _remove_outdated_files(self, username, password, local_root, remote_root):
785 | """
786 | Remove old versions of files.
787 |
788 | Args:
789 | username (str): username for MEGA account
790 | password (str): password for MEGA account
791 | local_root (str): Local path to download file to
792 | remote_root (str): Remote path of file to download
793 |
794 | Returns:
795 | Boolean: Whether operation was successful or not.
796 | """
797 | logger = getLogger('MegaManager.remove_outdated_files')
798 | logger.setLevel(self.__log_level)
799 | logger.debug(' Removing outdated files for username "{}"'.format(username))
800 |
801 | try:
802 | lines = self.__mega_tools_lib.get_remote_file_data_recursively(username=username, password=password,
803 | remote_path=remote_root, remove_blank_lines=True)
804 | if lines:
805 | for line in lines:
806 | logger.debug(' Processing line "%s"' % line)
807 | remote_file_type = self.__mega_tools_lib.get_file_type_from_megals_line_data(line=line)
808 |
809 | if remote_file_type == '0':
810 | remote_file_path = self.__mega_tools_lib.get_file_path_from_megals_line_data(line=line)
811 | remote_file_size = self.__mega_tools_lib.get_file_size_from_megals_line_data(line=line)
812 | remote_file_modified_date = self.__mega_tools_lib.get_file_date_from_megals_line_data(line=line)
813 | remote_file_modified_date_dt = datetime.strptime(remote_file_modified_date, '%Y-%m-%d %H:%M:%S')
814 | remote_root = path.abspath(remote_root)
815 | local_root = local_root
816 | conv_remote_file_path = path.abspath(remote_file_path)
817 | local_file_path = conv_remote_file_path.replace(remote_root, local_root)
818 |
819 | if path.exists(local_file_path):
820 | logger.debug(' Local file exists. Determining if local file outdated compared to remote counterpart: "%s"' % local_file_path)
821 | local_file_size = path.getsize(local_file_path)
822 |
823 | if search(r'^.*\.megatmp\..*$', local_file_path):
824 | logger.warning(' File "{}" is temporary file. Deleting.'.format(local_file_path))
825 | self.__lib.delete_local_file(local_file_path)
826 | continue
827 |
828 | local_file_modified_date = self.__lib.convert_epoch_to_mega_time(path.getmtime(local_file_path))
829 | local_file_modified_date_dt = datetime.strptime(local_file_modified_date, '%Y-%m-%d %H:%M:%S')
830 |
831 | if local_file_size != int(remote_file_size):
832 | if local_file_modified_date_dt > remote_file_modified_date_dt:
833 | # local file is newer.
834 | logger.debug(' Local file is newer. Deleting remote file "%s"' % remote_file_path)
835 |
836 | self.__mega_tools_lib.remove_remote_path(username=username, password=password,
837 | remote_file_path=remote_file_path,
838 | process_priority_class=self.__megatools_process_priority_class,
839 | process_set_priority_timeout=self.__process_set_priority_timeout)
840 | logger.debug(' Success, removed local incomplete file "{}"'.format(local_file_path))
841 |
842 | else:
843 | # remote file is newer
844 | logger.debug(' Remote file is newer. Deleting local file "%s"' % local_file_path)
845 |
846 | for retry in range(100):
847 | try:
848 | remove(local_file_path)
849 | logger.debug(' Success, removed local incomplete file "{}"'.format(local_file_path))
850 | break
851 | except Exception as e:
852 | logger.exception(' Remove failed for remote file, "{}" retrying...'.format(e))
853 | else:
854 | logger.debug(' Local file does NOT exist: "%s"' % local_file_path)
855 | else:
856 | logger.debug(' Remote location is not a file: "{}"'.format(line))
857 | else:
858 | logger.debug(' No file list retrieved from MEGA for account "{}"'.format(username))
859 |
860 | logger.debug(' Finished removing outdated files for username "{}"'.format(username))
861 | return True
862 | except Exception as e:
863 | logger.warning(' Exception: {}'.format(e))
864 | return False
865 |
866 | def _remove_outdated_local_remote_files(self, profile):
867 | """
868 | Remove outdated local and remote files
869 |
870 | Args:
871 | profile (Profile): Profile object
872 | """
873 | logger = getLogger('MegaManager._remove_outdated_local_remote_files')
874 | logger.setLevel(self.__log_level)
875 |
876 | try:
877 | profile_name = profile.profile_name
878 | logger.debug(' Removing outdated files for {}'.format(profile_name))
879 | username = profile.account.username
880 | password = profile.account.password
881 |
882 | for pathMapping in profile.path_mappings:
883 | local_path = pathMapping.local_path
884 | remote_path = pathMapping.remote_path
885 | self._remove_outdated_files(username=username, password=password, local_root=local_path,
886 | remote_root=remote_path)
887 | except Exception as e:
888 | logger.error('Exception: {}'.format(e))
889 |
890 | def _remove_remote_files_that_dont_exist_locally(self, username, password, local_root, remote_root):
891 | """
892 | Remove remote files that don't exist locally.
893 |
894 | Args:
895 | username (str): username of account to upload to
896 | password (str): Password of account to upload to
897 | local_root (str): Local path to download file to
898 | remote_root (str): Remote path of file to download
899 |
900 | Returns:
901 | Boolean: Whether operation is successful or not.
902 | """
903 | logger = getLogger('MegaManager._remove_remote_files_that_dont_exist_locally')
904 | logger.setLevel(self.__log_level)
905 | logger.debug(' Removing remote files that do not exist locally for account: "{}"'.format(username))
906 | dont_exist_locally = self._get_remote_files_that_dont_exist_locally(username=username, password=password,
907 | local_root=local_root, remote_root=remote_root)
908 | try:
909 | for file_path in dont_exist_locally:
910 | file_md5_hash = self.__lib.get_file_md5_hash(file_path)
911 | self.__removed_remote_files.add(file_md5_hash)
912 | self.__mega_tools_lib.remove_remote_path(username=username, password=password,
913 | remote_file_path=file_path,
914 | process_priority_class=self.__megatools_process_priority_class,
915 | process_set_priority_timeout=self.__process_set_priority_timeout)
916 | self.__lib.dump_set_into_numpy_file(item_set=self.__removed_remote_files,
917 | file_path=self.__removed_remote_files_path)
918 | return True
919 |
920 | except Exception as e:
921 | logger.error('Exception: {}'.format(e))
922 | return False
923 |
924 | def _setup(self, kwargs):
925 | """
926 | Setup MegaManager applicaiton.
927 |
928 | Args:
929 | kwargs (dict): Mega_Manager keyword arguments.
930 | """
931 |
932 | try:
933 | self.__mega_manager_config_path = kwargs['mega_manager_config_path']
934 | ignore_config_actions = False
935 | for key, value in kwargs.items():
936 | if value == True:
937 | ignore_config_actions = True
938 | self.__config_parser = self._import_config_file_data(ignore_config_actions=ignore_config_actions)
939 | self._assign_attributes(**kwargs)
940 | self.__lib = Lib(log_level=self.__log_level)
941 | self._setup_logger(log_file_path=self.__mega_manager_log_path)
942 |
943 | self.__compress_images_lib = CompressImages_Lib(log_level=self.__log_level)
944 | self.__ffmpeg_lib = FFMPEG_Lib(log_file_path=self.__ffmpeg_log_path, log_level=self.__log_level)
945 | self.__mega_tools_lib = MegaTools_Lib(log_file_path=self.__megatools_log_path, down_speed_limit=self.__mega_download_speed, up_speed_limit=self.__mega_upload_speed, log_level=self.__log_level)
946 |
947 | self.__removed_remote_files = self.__lib.load_numpy_file_as_set(file_path=self.__removed_remote_files_path)
948 | self.__compressed_video_files = self.__lib.load_numpy_file_as_set(file_path=self.__compressed_videos_file_path)
949 | self.__unable_to_compress_video_files = self.__lib.load_numpy_file_as_set(
950 | file_path=self.__unable_to_compress_videos_file_path)
951 | self.__compressed_image_files = self.__lib.load_numpy_file_as_set(file_path=self.__compressed_images_file_path)
952 | self.__unable_to_compress_image_files = self.__lib.load_numpy_file_as_set(
953 | file_path=self.__unable_to_compress_images_file_path)
954 | self.__compression_video_extensions = [ext.lower() for ext in self.__compression_video_extensions]
955 | self.__compression_image_extensions = [ext.lower() for ext in self.__compression_image_extensions]
956 |
957 | except Exception as e:
958 | print(' Exception: ' + str(e))
959 | self._teardown()
960 |
961 | def _setup_logger(self, log_file_path):
962 | """
963 | Logger setup.
964 |
965 | Args:
966 | log_file_path (str): Log file path.
967 | """
968 | print(' Setting up logger using log file: {}'.format(log_file_path))
969 | root = getLogger()
970 | root.setLevel(self.__log_level)
971 | file_handler = handlers.TimedRotatingFileHandler(log_file_path, when=self.__log_retention,
972 | backupCount=self.__log_retention_backup_count)
973 | formatter = Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
974 | file_handler.setLevel(DEBUG)
975 | file_handler.setFormatter(formatter)
976 |
977 | ch = StreamHandler(stdout)
978 | ch.setLevel(self.__log_level)
979 | ch.setFormatter(formatter)
980 |
981 | root.addHandler(file_handler)
982 | root.addHandler(ch)
983 |
984 | logger = getLogger('MegaManager._setup_logger')
985 | logger.setLevel(self.__log_level)
986 | logger.info(' Logging to %s' % self.__mega_manager_log_path)
987 |
988 | def _teardown(self):
989 | """
990 | Tearing down of MEGA Manager.
991 | """
992 |
993 | logger = getLogger('MegaManager._teardown')
994 | logger.setLevel(self.__log_level)
995 |
996 | logger.info(' Tearing down megaManager!')
997 | try:
998 | if self.__sync:
999 | self.__lib.dump_set_into_numpy_file(item_set=self.__removed_remote_files,
1000 | file_path=self.__removed_remote_files_path, )
1001 | if self.__compress_images:
1002 | self.__lib.dump_set_into_numpy_file(item_set=self.__compressed_image_files,
1003 | file_path=self.__compressed_images_file_path, )
1004 | self.__lib.dump_set_into_numpy_file(item_set=self.__unable_to_compress_image_files,
1005 | file_path=self.__unable_to_compress_images_file_path)
1006 | if self.__compress_videos:
1007 | self.__lib.dump_set_into_numpy_file(item_set=self.__compressed_video_files,
1008 | file_path=self.__compressed_videos_file_path, )
1009 | self.__lib.dump_set_into_numpy_file(item_set=self.__unable_to_compress_video_files,
1010 | file_path=self.__unable_to_compress_videos_file_path)
1011 |
1012 | # self._remove_temp_files()
1013 |
1014 | megacopy_process_name = 'megacopy.exe' if system() == 'Windows' else 'megacopy'
1015 | self.__lib.kill_running_processes_with_name(megacopy_process_name)
1016 | megals_process_name = 'megals.exe' if system() == 'Windows' else 'megals'
1017 | self.__lib.kill_running_processes_with_name(megals_process_name)
1018 | megadf_process_name = 'megadf.exe' if system() == 'Windows' else 'megadf'
1019 | self.__lib.kill_running_processes_with_name(megadf_process_name)
1020 | megarm_process_name = 'megarm.exe' if system() == 'Windows' else 'megarm'
1021 | self.__lib.kill_running_processes_with_name(megarm_process_name)
1022 | megamkdir_process_name = 'megamkdir.exe' if system() == 'Windows' else 'megamkdir'
1023 | self.__lib.kill_running_processes_with_name(megamkdir_process_name)
1024 | ffmpeg_process_name = 'ffmpeg.exe' if system() == 'Windows' else 'ffmpeg'
1025 | self.__lib.kill_running_processes_with_name(ffmpeg_process_name)
1026 |
1027 | logger.info(' Tear down successful!')
1028 | except Exception as e:
1029 | logger.warning(' Exception: %s' % str(e))
1030 | pass
1031 |
1032 | def _thread_download_profile_files(self, profile):
1033 | """
1034 | Download files from profile.
1035 |
1036 | Args:
1037 | profile (Profile): Profile object
1038 |
1039 | Returns:
1040 | Boolean: Whether operation was successful or not.
1041 | """
1042 |
1043 | logger = getLogger('MegaManager._thread_download_profile_files')
1044 | logger.setLevel(self.__log_level)
1045 |
1046 | for pathMapping in profile.path_mappings:
1047 | self.__mega_tools_lib.download_all_files_from_account(username=profile.account.username,
1048 | password=profile.account.password,
1049 | local_root=pathMapping.local_path,
1050 | remote_root=pathMapping.remote_path,
1051 | process_set_priority_timeout=self.__process_set_priority_timeout)
1052 |
1053 | def _thread_output_profile_data(self, profile):
1054 | """
1055 | Output profile data to standard output.
1056 |
1057 | Args:
1058 | profile (Profile): Profile object
1059 |
1060 | Returns:
1061 | Boolean: Whether successful or not.
1062 | """
1063 | logger = getLogger('MegaManager._thead_output_profile_data')
1064 | logger.setLevel(self.__log_level)
1065 | try:
1066 | print('')
1067 | print('')
1068 | print('Profile Data Output:')
1069 | print(('\tProfile Name: {}').format(profile.profile_name))
1070 | print(('\tLocal Used Space: {}').format(profile.local_used_space))
1071 | print(('\tRemote Free Space: {}').format(profile.remote_free_space))
1072 | print(('\tRemote Total Space: {}').format(profile.remote_total_space))
1073 | print(('\tRemote Used Space: {}').format(profile.remote_used_space))
1074 | print(('\tAccount Username: {}').format(profile.account.username))
1075 | print(('\tAccount Password: {}').format(profile.account.password))
1076 | print('')
1077 |
1078 | except Exception as e:
1079 | logger.warning('Exception: {}'.format(e))
1080 |
1081 | def _thread_remove_remote_files_that_dont_exist_locally(self, profile):
1082 | """
1083 | Get remote files that don't exist locally.
1084 |
1085 | Args:
1086 | profile (SyncProfile): Profile to remove remote files that dont exist locally.
1087 |
1088 | Returns:
1089 | Boolean: whether operation is successful or not
1090 | """
1091 |
1092 | logger = getLogger('MegaManager._thread_remove_remote_files_that_dont_exist_locally')
1093 | logger.setLevel(self.__log_level)
1094 | logger.debug(' Removing files that do not exist locally for profile: "{}"'.format(profile.profile_name))
1095 | for path_mapping in profile.path_mappings:
1096 | local_root = path_mapping.local_path
1097 | remote_root = path_mapping.remote_path
1098 | username = profile.account.username
1099 | password = profile.account.password
1100 | self._remove_outdated_files(username=username, password=password, local_root=local_root, remote_root=remote_root)
1101 | self._remove_remote_files_that_dont_exist_locally(username=username, password=password, local_root=local_root,
1102 | remote_root=remote_root)
1103 | def _thread_upload_profile_files(self, profile):
1104 | """
1105 | Upload to all MEGA profile accounts.
1106 |
1107 | Args:
1108 | profile (Profile): Profile object
1109 |
1110 | Returns:
1111 | Boolean: Whether successful or not.
1112 | """
1113 |
1114 | logger = getLogger('MegaManager._thead_download_profile_files')
1115 | logger.setLevel(self.__log_level)
1116 |
1117 | try:
1118 | for pathMapping in profile.path_mappings:
1119 | self.__mega_tools_lib.upload_to_account(username=profile.account.username, password=profile.account.password,
1120 | local_root=pathMapping.local_path,
1121 | remote_root=pathMapping.remote_path,
1122 | process_priority_class=self.__megatools_process_priority_class,
1123 | process_set_priority_timeout=self.__process_set_priority_timeout)
1124 | except Exception as e:
1125 | logger.warning(' Exception: {}'.format(e))
1126 |
1127 | def _update_account_remote_details(self, account):
1128 | """
1129 | Gather data for account (remote used space, remote free space, remote total space, etc...).
1130 |
1131 | Args:
1132 | account (Account): Account object to gather data for.
1133 |
1134 | Returns:
1135 | Account: account object with remote data updated
1136 | """
1137 |
1138 | logger = getLogger('MegaManager._update_account_remote_details')
1139 | logger.setLevel(self.__log_level)
1140 | username = account.username
1141 | password = account.password
1142 | account.total_space = self.__mega_tools_lib.get_account_total_space(username=username, password=password)
1143 | account.free_space = self.__mega_tools_lib.get_account_free_space(username=username, password=password)
1144 | account.used_space = self.__mega_tools_lib.get_account_used_space(username=username, password=password)
1145 | return account
1146 |
1147 | def _update_profile_remote_details(self, profile):
1148 | """
1149 | Gather data for profile (remote size, local size, etc...) for self.__mega_accounts_output_path file.
1150 |
1151 | Args:
1152 | profile (SyncProfile): profile object to gather data for.
1153 |
1154 | Returns:
1155 | SyncProfile: profile object with remote data updated
1156 | """
1157 |
1158 | logger = getLogger('MegaManager._update_profile_remote_details')
1159 | logger.setLevel(self.__log_level)
1160 |
1161 | username = profile.account.username
1162 | password = profile.account.password
1163 | totalRemoteSize = 0
1164 |
1165 | for pathMapping in profile.path_mappings:
1166 | remotePath = pathMapping.remotepath
1167 | remotePaths = self.__mega_tools_lib.get_remote_file_paths_recursively(username=username, password=password,
1168 | remote_path=remotePath,
1169 | process_priority_class=self.__megatools_process_priority_class,
1170 | process_set_priority_timeout=self.__process_set_priority_timeout)
1171 |
1172 | pathMappingRemoteSize = 0
1173 | for path in remotePaths:
1174 | pathMappingRemoteSize += self.__mega_tools_lib.get_remote_file_size(username=username, password=password, remotePath=path)
1175 |
1176 | pathMapping.remote_path_used_space = pathMappingRemoteSize
1177 | totalRemoteSize += pathMappingRemoteSize
1178 | return profile
1179 |
1180 | def _wait_for_threads_to_finish(self, threads=None, timeout=None, max_video_compression_threads=None):
1181 | """
1182 | Wait for threads to finish.
1183 |
1184 | Args:
1185 | threads (List[threading.Thread]): List of threads to wait for.
1186 | timeout (int): Maximum time in seconds to wait for threads.
1187 | max_video_compression_threads (int): Maximum video compression threads to wait for.
1188 | """
1189 | logger = getLogger('MegaManager._wait_for_threads_to_finish')
1190 | logger.setLevel(self.__log_level)
1191 |
1192 | threads = threads if threads else self.__threads
1193 | logger.debug(' Waiting for threads to finish: {}'.format(threads))
1194 | start_time = time()
1195 | while len(threads) > 0:
1196 | video_compression_threads = 0
1197 | if (timeout and not time() - start_time > timeout) or not timeout:
1198 | for thread in threads:
1199 | if not thread.is_alive():
1200 | threads.remove(thread)
1201 | logger.info(' Thread "%s" finished!' % thread.name)
1202 | logger.debug(' Threads left: %d' % len(threads))
1203 | else:
1204 | if thread.name.startswith('thread_compress_videos'):
1205 | video_compression_threads+=1
1206 | if max_video_compression_threads and video_compression_threads < max_video_compression_threads:
1207 | return
1208 | elif max_video_compression_threads:
1209 | logger.debug(f' Video compression threads left: {video_compression_threads}')
1210 | logger.debug(f' Max video compression threads allowed: {max_video_compression_threads}')
1211 | else:
1212 | logger.debug(' TIMED OUT waiting for threads to complete! Timeout %d (seconds)' % timeout)
1213 | return
1214 | sleep(10)
1215 |
1216 | def get_mega_manager_log_file(self):
1217 | """
1218 | Returns Mega Manager logging file path.
1219 |
1220 | Returns:
1221 | Mega Manager logging file path.
1222 | """
1223 |
1224 | return self.__mega_manager_log_path
1225 |
1226 | def run(self):
1227 | """
1228 | Run MegaManager tasks.
1229 |
1230 | Returns:
1231 | Integer: Returns 0 for successful or 1 for unsuccessful.
1232 | """
1233 |
1234 | logger = getLogger('MegaManager.run')
1235 | logger.setLevel(self.__log_level)
1236 |
1237 | logger.debug(' Running MEGA Manager.')
1238 |
1239 | result = 0
1240 | try:
1241 | while True:
1242 | sync_profiles_randomized = self.__sync_profiles[:]
1243 | shuffle(sync_profiles_randomized)
1244 | compression_threads = []
1245 | for idx, profile in enumerate(sync_profiles_randomized):
1246 | for pathMapping in profile.path_mappings:
1247 | attempt = 0
1248 | while not path.exists(pathMapping.local_path):
1249 | attempt+=1
1250 | if attempt >= MAX_PATH_MAPPING_EXIST_ATTEMPTS:
1251 | logger.error(' No files found in path: "{}"'.format(pathMapping.local_path))
1252 | raise PathMappingDoesNotExist(' Path mapping does not exist: "{}"'.format(pathMapping.local_path))
1253 | logger.warning(f' Waiting for path mapping to exist. Attempt {attempt}: {pathMapping.local_path}')
1254 | sleep(PATH_MAPPING_TIMEOUT_SECONDS)
1255 | logger.warning(f' Path mapping exists: {pathMapping.local_path}')
1256 | if self.__compress_all:
1257 | file_list = self._get_all_files(root_path=pathMapping.local_path)
1258 | shuffle(file_list)
1259 | self._create_thread_compress_image_files(file_list=file_list)
1260 | self._create_thread_compress_video_files(file_list=file_list)
1261 | elif self.__compress_images:
1262 | file_list = self._get_all_files(root_path=pathMapping.local_path)
1263 | shuffle(file_list)
1264 | self._create_thread_compress_image_files(file_list=file_list)
1265 | elif self.__compress_videos:
1266 | file_list = self._get_all_files(root_path=pathMapping.local_path)
1267 | shuffle(file_list)
1268 | self._create_thread_compress_video_files(file_list=file_list)
1269 | self._wait_for_threads_to_finish(threads=self.__threads,
1270 | max_video_compression_threads=self.__max_video_compression_threads)
1271 | compression_threads = list(self.__threads)
1272 | for profile in sync_profiles_randomized:
1273 | if self.__mega_manager_output_profile_data_path:
1274 | self._create_thread_output_profile_data(profile=profile)
1275 |
1276 | if self.__sync:
1277 | if self.__local_is_truth:
1278 | self._thread_remove_remote_files_that_dont_exist_locally(profile=profile)
1279 | else:
1280 | self._create_thread_remove_remote_files_that_dont_exist_locally(profile=profile)
1281 | self._create_thread_download(profile=profile)
1282 | self._create_thread_upload(profile=profile)
1283 |
1284 | profile_threads = [item for item in self.__threads if item not in compression_threads]
1285 | self._wait_for_threads_to_finish(threads=profile_threads)
1286 |
1287 | self._wait_for_threads_to_finish()
1288 | logger.debug(' Sleeping {} seconds before next run.'.format(self.__sleep_time_between_runs_seconds))
1289 | sleep(self.__sleep_time_between_runs_seconds)
1290 |
1291 | except Exception as e:
1292 | logger.exception(' Exception: ' + str(e))
1293 | result = 1
1294 | finally:
1295 | self._teardown()
1296 | return result
1297 |
1298 |
1299 | class PathMappingDoesNotExist(Exception):
1300 | pass
1301 |
1302 |
1303 |
1304 |
--------------------------------------------------------------------------------
/megamanager/path_mapping.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 7/2/2017
4 | # Initial Creation.
5 | # Path Mapping class. Used for path mappings.
6 | ###
7 | import os
8 | from account import Account
9 | from logging import getLogger
10 |
11 | __author__ = 'szmania'
12 |
13 |
14 | class PathMapping(Account):
15 | def __init__(self, local_path, remote_path, log_level='DEBUG'):
16 | """
17 | Class used to keep track of local and remote path mappings. This correlates local locations with remote locations,
18 | for syncing purposes.
19 |
20 | Args:
21 | local_path (str): Local path of path mapping.
22 | remote_path (str): Remote path of path mapping.
23 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
24 | """
25 | self.__local_path = os.path.abspath(local_path)
26 | self.__remote_path = os.path.abspath(remote_path)
27 | self.__log_level = log_level
28 | self.__local_path_free_space = None
29 | self.__local_path_used_space = None
30 | self.__remote_path_used_space = None
31 |
32 | @property
33 | def local_path(self):
34 | """
35 | Getter for MEGA local path.
36 |
37 | Returns:
38 | String: Returns local path
39 | """
40 |
41 | logger = getLogger('SyncProfile.local_path')
42 | logger.setLevel(self.__log_level)
43 |
44 | return self.__local_path
45 |
46 | @local_path.setter
47 | def local_path(self, value):
48 | """
49 | Setter for local path.
50 |
51 | Args:
52 | value (str): value to set local path to.
53 | """
54 | logger = getLogger('SyncProfile.local_path')
55 | logger.setLevel(self.__log_level)
56 | self.__local_path = os.path.abspath(value)
57 |
58 | @property
59 | def local_path_free_space(self):
60 | """
61 | Getter for MEGA local path free space.
62 |
63 | Returns:
64 | String: Returns local path free space
65 | """
66 | logger = getLogger('SyncProfile.local_path_free_space')
67 | logger.setLevel(self.__log_level)
68 | return self.__local_path_free_space
69 |
70 | @local_path_free_space.setter
71 | def local_path_free_space(self, value):
72 | """
73 | Setter for local path free space.
74 |
75 | Args:
76 | value (str): value to set local path free space to.
77 | """
78 | logger = getLogger('SyncProfile.local_path_free_space')
79 | logger.setLevel(self.__log_level)
80 | self.__local_path_free_space = value
81 |
82 | @property
83 | def local_path_used_space(self):
84 | """
85 | Getter for local path used space.
86 |
87 | Returns:
88 | String: Returns local path used space
89 | """
90 | logger = getLogger('SyncProfile.localRoot_usedSpace')
91 | logger.setLevel(self.__log_level)
92 | return self.__local_path_used_space
93 |
94 | @local_path_used_space.setter
95 | def local_path_used_space(self, value):
96 | """
97 | Setter for local path used space.
98 |
99 | Args:
100 | value (str): value to set local path used space to.
101 | """
102 | logger = getLogger('SyncProfile.local_path_used_space')
103 | logger.setLevel(self.__log_level)
104 | self.__local_path_used_space = value
105 |
106 | @property
107 | def remote_path(self):
108 | """
109 | Getter for remote path.
110 |
111 | Returns:
112 | String: Returns remote path
113 | """
114 | logger = getLogger('SyncProfile.remote_path')
115 | logger.setLevel(self.__log_level)
116 | return self.__remote_path
117 |
118 | @remote_path.setter
119 | def remote_path(self, value):
120 | """
121 | Setter for remote path.
122 |
123 | Args:
124 | value (str): value to set remote path to.
125 | """
126 | logger = getLogger('SyncProfile.remote_path')
127 | logger.setLevel(self.__log_level)
128 | self.__remote_path = os.path.abspath(value)
129 |
130 | @property
131 | def remote_path_used_space(self):
132 | """
133 | Getter for remote path used space.
134 |
135 | Returns:
136 | String: Returns remote path used space
137 | """
138 | logger = getLogger('SyncProfile.remote_path_used_space')
139 | logger.setLevel(self.__log_level)
140 | return self.__remote_path_used_space
141 |
142 | @remote_path_used_space.setter
143 | def remote_path_used_space(self, value):
144 | """
145 | Setter for remote path used space.
146 |
147 | Args:
148 | value (str): value to set remote path used space to.
149 | """
150 | logger = getLogger('SyncProfile.remote_path_used_space')
151 | logger.setLevel(self.__log_level)
152 | self.__remote_path_used_space = value
153 |
154 |
--------------------------------------------------------------------------------
/megamanager/syncprofile.py:
--------------------------------------------------------------------------------
1 | ##
2 | # Created by: Curtis Szmania
3 | # Date: 7/2/2017
4 | # Initial Creation.
5 | # Sync Profile class. Used for sync profile data.
6 | ###
7 |
8 | from account import Account
9 | from logging import getLogger
10 | from path_mapping import PathMapping
11 |
12 | __author__ = 'szmania'
13 |
14 |
15 | class SyncProfile(object):
16 | def __init__(self, username, password, path_mappings, profile_name=None, log_level='DEBUG'):
17 | """
18 | Library for ffmpeg converter and encoder interaction.
19 |
20 | Args:
21 | username (str): MEGA account user name
22 | password (str): MEGA account password
23 | path_mappings (list): dictionary of local and remote path mappings.
24 | profile_name (str): Unique profile name
25 | log_level (str): Logging level setting ie: "DEBUG" or "WARN"
26 | """
27 |
28 |
29 | self.__profile_name = profile_name if profile_name is not None else username
30 | self.__path_mappings = path_mappings
31 | self.__log_level = log_level
32 | self.__local_used_space = None
33 | self.__account = Account(username=username, password=password, log_level=log_level)
34 |
35 | @property
36 | def account(self):
37 | """
38 | Getter for MEGA profile account.
39 |
40 | Returns:
41 | Account: Returns MEGA profile account
42 | """
43 | logger = getLogger('SyncProfile.account')
44 | logger.setLevel(self.__log_level)
45 | return self.__account
46 |
47 | @account.setter
48 | def account(self, value):
49 | """
50 | Setter for MEGA profile account.
51 |
52 | Args:
53 | value (str): value to set profile account to.
54 | """
55 | logger = getLogger('SyncProfile.account')
56 | logger.setLevel(self.__log_level)
57 | self.__account = value
58 |
59 | @property
60 | def local_used_space(self):
61 | """
62 | Getter for MEGA profile local used space.
63 |
64 | Returns:
65 | String: Returns MEGA profile local used space
66 | """
67 | logger = getLogger('SyncProfile.local_used_space')
68 | logger.setLevel(self.__log_level)
69 | return self.__local_used_space
70 |
71 | @local_used_space.setter
72 | def local_used_space(self, value):
73 | """
74 | Setter for MEGA profile local used space.
75 |
76 | Args:
77 | value (str): value to set profile local used space to.
78 | """
79 | logger = getLogger('SyncProfile.local_used_space')
80 | logger.setLevel(self.__log_level)
81 | self.__local_used_space = value
82 |
83 | @property
84 | def path_mappings(self):
85 | """
86 | Getter for MEGA profile path mappings.
87 |
88 | Returns:
89 | Account: Returns MEGA profile path mappings list of PathMapping objects
90 | """
91 | logger = getLogger('SyncProfile.path_mappings')
92 | logger.setLevel(self.__log_level)
93 | return self.__path_mappings
94 |
95 | @path_mappings.setter
96 | def path_mappings(self, value):
97 | """
98 | Setter for MEGA profile path_mappings.
99 |
100 | Args:
101 | value (list): list of PathMapping objects to set to.
102 | """
103 | logger = getLogger('SyncProfile.path_mappings')
104 | logger.setLevel(self.__log_level)
105 | self.__path_mappings = value
106 |
107 | @property
108 | def profile_name(self):
109 | """
110 | Getter for profile name.
111 |
112 | Returns:
113 | String: Returns profile name
114 | """
115 | logger = getLogger('SyncProfile.profile_name')
116 | logger.setLevel(self.__log_level)
117 | return self.__profile_name
118 |
119 | @profile_name.setter
120 | def profile_name(self, value):
121 | """
122 | Setter for profile name.
123 |
124 | Args:
125 | value (str): value to set profile name to.
126 | """
127 | logger = getLogger('SyncProfile.profile_name')
128 | logger.setLevel(self.__log_level)
129 | self.__profile_name = value
130 |
131 | @property
132 | def remote_free_space(self):
133 | """
134 | Getter for MEGA profile remote free space.
135 |
136 | Returns:
137 | String: Returns MEGA profile remote free space
138 | """
139 | logger = getLogger('SyncProfile.remote_free_space')
140 | logger.setLevel(self.__log_level)
141 | return self.__account.freeSpace
142 |
143 | @property
144 | def remote_total_space(self):
145 | """
146 | Getter for MEGA profile remote total space.
147 |
148 | Returns:
149 | String: Returns MEGA profile remote total space
150 | """
151 | logger = getLogger('SyncProfile.remote_total_space')
152 | logger.setLevel(self.__log_level)
153 | return self.__account.totalSpace
154 |
155 | @property
156 | def remote_used_space(self):
157 | """
158 | Getter for MEGA profile remote used space.
159 |
160 | Returns:
161 | String: Returns MEGA profile remote used space
162 | """
163 | logger = getLogger('SyncProfile.remote_used_space')
164 | logger.setLevel(self.__log_level)
165 | return self.__account.usedSpace
166 |
167 | def add_path_mapping(self, localRoot, remoteRoot):
168 | """
169 | Add path mapping to profile path mappings list.
170 |
171 | Args:
172 | localRoot (str): Local root of pair.
173 | remoteRoot (str): Remote root of pair.
174 | """
175 | logger = getLogger('SyncProfile.add_path_mapping')
176 | logger.setLevel(self.__log_level)
177 | logger.debug(' Adding path mapping to profile.')
178 | try:
179 | pathMapObj = PathMapping(local_path=localRoot, remote_path=remoteRoot)
180 | self.__path_mappings[self.get_path_mappings_count()] = pathMapObj
181 | return True
182 |
183 | except Exception as e:
184 | logger.error(' Exception: %s' % str(e))
185 | return False
186 |
187 | def get_path_mapping(self, index=None):
188 | """
189 | Getter for profile path mapping at given index.
190 |
191 | Args:
192 | index (int): index of path mapping to get.
193 |
194 | Returns:
195 | PathMapping: Returns PathMapping object.
196 | """
197 | logger = getLogger('SyncProfile.get_path_mapping')
198 | logger.setLevel(self.__log_level)
199 | logger.debug(' Returning path mapping at index %d.' % index)
200 | if index:
201 | if not index + 1 > len(self.__path_mappings):
202 | return self.__path_mappings[index]
203 | else:
204 | logger.error(' Error, given index of "%d" is larger than path mappings list for profile.' % index)
205 | return None
206 | else:
207 | if len(self.__path_mappings) > 0:
208 | logger.debug(' No index given. Returning first item.')
209 | return self.__path_mappings[0]
210 | else:
211 | logger.error(' Error, no path mapping exist for this profile!')
212 | return None
213 |
214 | def get_path_mapping_local_path(self, index=None):
215 | """
216 | Getter for profile local root path.
217 |
218 | Args:
219 | index (int): index of path mapping to get local root of.
220 |
221 | Returns:
222 | String: Returns local root path.
223 | """
224 | logger = getLogger('SyncProfile.get_path_mapping_local_path')
225 | logger.setLevel(self.__log_level)
226 | logger.debug(' Returning path mapping local root path.')
227 | if index:
228 | if not index + 1 > len(self.__path_mappings):
229 | return self.__path_mappings[index].local_path
230 | else:
231 | logger.error(' Error, given index of "%d" is larger than path mappings list for profile.' % index)
232 | return None
233 | else:
234 | if len(self.__path_mappings) > 0:
235 | logger.debug(' No index given. Returning first item.')
236 | return self.__path_mappings[0].local_path
237 | else:
238 | logger.error(' Error, no path mappings exist for this profile!')
239 | return None
240 |
241 | def get_path_mapping_remote_path(self, index=None):
242 | """
243 | Getter for profile remote root path.
244 |
245 | Args:
246 | index (int): index of path mapping to get remote root of.
247 |
248 | Returns:
249 | String: Returns remote root path.
250 | """
251 | logger = getLogger('SyncProfile.get_path_mapping_remote_path')
252 | logger.setLevel(self.__log_level)
253 | logger.debug(' Returning path mapping remote root path.')
254 | if index:
255 | if not index + 1 > len(self.__path_mappings):
256 | return self.__path_mappings[index].remote_path
257 | else:
258 | logger.error(' Error, given index of "%d" is larger than path mappings list for profile.' % index)
259 | return None
260 | else:
261 | if len(self.__path_mappings) > 0:
262 | logger.debug(' No index given. Returning first item.')
263 | return self.__path_mappings[0].remote_path
264 | else:
265 | logger.error(' Error, no path mappings exist for this profile!')
266 | return None
267 |
268 | def get_path_mappings_count(self):
269 | """
270 | Return path mappings list size.
271 |
272 | Returns:
273 | integer: size of path mappings list.
274 | """
275 | logger = getLogger('SyncProfile.get_path_mappings_count')
276 | logger.setLevel(self.__log_level)
277 | return len(self.__path_mappings)
278 |
279 | def set_path_mapping_local_path(self, value, index=None):
280 | """
281 | Setter for profile local root path.
282 |
283 | Args:
284 | index (int): index of path mapping to set local root of.
285 | value (str): value to set local root to.
286 | """
287 | logger = getLogger('SyncProfile.set_path_mapping_local_path')
288 | logger.setLevel(self.__log_level)
289 | logger.debug(' Setting path mapping local root path.')
290 | if not index + 1 > len(self.__path_mappings):
291 | self.__path_mappings[index].local_path = value
292 | return True
293 | else:
294 | logger.error(' Error, given index of "%d" is larger than path mappings list for profile.' % index)
295 | return False
296 |
297 | def set_path_mapping_remote_root(self, index, value):
298 | """
299 | Setter for profile remote root path.
300 |
301 | Args:
302 | index (int): index of path mapping to set remote root of.
303 | value (str): value to set remote root to.
304 | """
305 | logger = getLogger('SyncProfile.set_path_mapping_remote_root')
306 | logger.setLevel(self.__log_level)
307 | logger.debug(' Setting path mapping remote root path.')
308 | if not index + 1 > len(self.__path_mappings):
309 | self.__path_mappings[index].remote_path = value
310 | return True
311 | else:
312 | logger.error(' Error, given index of "%d" is larger than path mappings list for profile.' % index)
313 | return False
314 |
315 |
--------------------------------------------------------------------------------
/megamanager/tools/__init__.py:
--------------------------------------------------------------------------------
1 | # from .compressImages import CompressImage, DeleteBackupImage
2 |
--------------------------------------------------------------------------------
/megamanager/tools/compressImages/README.md:
--------------------------------------------------------------------------------
1 | CompressImages
2 | ==============
3 |
4 | Python script for automatically reducing size of all JPEG and PNG images in a directory.
5 |
6 | Usage: `compressimages.py [-h] [--mode {compress,restorebackup,deletebackup}] path`
7 |
8 | Positional arguments:
9 | `path` File or directory name
10 |
11 | Optional arguments:
12 | `-h, --help` show this help message and exit
13 | `--mode {compress,restorebackup,deletebackup}` Mode to solve with (default: compress)
14 | * `compress`: Compress the image(s).
15 | * `restorebackup`: Restore the backup images (valid for directory path only).
16 | * `deletebackup`: Delete the backup images (valid for directory path only).
17 |
18 | Python 2.7 or later is required.
19 |
20 | See [Using Python to Reduce JPEG and PNG Image File Sizes Without Loss of Quality (Softwariness.com)](https://www.softwariness.com/articles/reduce-image-file-sizes-using-python/) for more information.
21 |
--------------------------------------------------------------------------------
/megamanager/tools/compressImages/__init__.py:
--------------------------------------------------------------------------------
1 | from .compressImages import CompressImage, DeleteBackupImage
2 |
--------------------------------------------------------------------------------
/megamanager/tools/compressImages/compressImages.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # Copyright (c) 2015, Softwariness.com
3 | # All rights reserved.
4 | #
5 | # Redistribution and use in source and binary forms, with or without
6 | # modification, are permitted provided that the following conditions are met:
7 | #
8 | # * Redistributions of source code must retain the above copyright notice, this
9 | # list of conditions and the following disclaimer.
10 | #
11 | # * Redistributions in binary form must reproduce the above copyright notice,
12 | # this list of conditions and the following disclaimer in the documentation
13 | # and/or other materials provided with the distribution.
14 | #
15 | # * Neither the name of Softwariness.com nor the names of its
16 | # contributors may be used to endorse or promote products derived from
17 | # this software without specific prior written permission.
18 | #
19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 | from PIL import Image, ImageFile
31 | from sys import exit, stderr
32 | from os.path import getsize, isfile, isdir, join
33 | from os import remove, rename, walk, stat
34 | from stat import S_IWRITE
35 | from shutil import move
36 | from argparse import ArgumentParser
37 | from abc import ABCMeta, abstractmethod
38 |
39 | class ProcessBase:
40 | """Abstract base class for file processors."""
41 | __metaclass__ = ABCMeta
42 |
43 | def __init__(self):
44 | self.extensions = []
45 | self.backupextension = 'compressimages-backup'
46 |
47 | @abstractmethod
48 | def processfile(self, filename):
49 | """Abstract method which carries out the process on the specified file.
50 | Returns True if successful, False otherwise."""
51 | pass
52 |
53 | def processdir(self, path):
54 | """Recursively processes files in the specified directory matching
55 | the self.extensions list (case-insensitively)."""
56 |
57 | filecount = 0 # Number of files successfully updated
58 |
59 | for root, dirs, files in walk(path):
60 | for file in files:
61 | # Check file extensions against allowed list
62 | lowercasefile = file.lower()
63 | matches = False
64 | for ext in self.extensions:
65 | if lowercasefile.endswith('.' + ext):
66 | matches = True
67 | break
68 | if matches:
69 | # File has eligible extension, so process
70 | fullpath = join(root, file)
71 | if self.processfile(fullpath):
72 | filecount = filecount + 1
73 | return filecount
74 |
75 | class CompressImage(ProcessBase):
76 | """Processor which attempts to reduce image file size."""
77 | def __init__(self):
78 | ProcessBase.__init__(self)
79 | self.extensions = ['jpg', 'jpeg', 'png']
80 |
81 | def processfile(self, filename):
82 | """Renames the specified image to a backup path,
83 | and writes out the image again with optimal settings."""
84 | try:
85 | # Skip read-only files
86 | if (not stat(filename)[0] & S_IWRITE):
87 | print('Ignoring read-only file "' + filename + '".')
88 | return False
89 |
90 | # Create a backup
91 | backupname = filename + '.' + self.backupextension
92 |
93 | if isfile(backupname):
94 | print('Ignoring file "' + filename + '" for which existing backup file is present.')
95 | return False
96 |
97 | rename(filename, backupname)
98 | except Exception as e:
99 | stderr.write('Skipping file "' + filename + '" for which backup cannot be made: ' + str(e) + '\n')
100 | return False
101 |
102 | ok = False
103 |
104 | try:
105 | # Open the image
106 | with open(backupname, 'rb') as file:
107 | img = Image.open(file)
108 |
109 | # Check that it's a supported format
110 | format = str(img.format)
111 | if format != 'PNG' and format != 'JPEG':
112 | print('Ignoring file "' + filename + '" with unsupported format ' + format)
113 | return False
114 |
115 | # This line avoids problems that can arise saving larger JPEG files with PIL
116 | ImageFile.MAXBLOCK = img.size[0] * img.size[1]
117 |
118 | # The 'quality' option is ignored for PNG files
119 | img.save(filename, quality=90, optimize=True)
120 |
121 | # Check that we've actually made it smaller
122 | origsize = getsize(backupname)
123 | newsize = getsize(filename)
124 |
125 | if newsize >= origsize:
126 | print('Cannot further compress "' + filename + '".')
127 | return False
128 |
129 | # Successful compression
130 | ok = True
131 | except Exception as e:
132 | stderr.write('Failure whilst processing "' + filename + '": ' + str(e) + '\n')
133 | finally:
134 | if not ok:
135 | try:
136 | move(backupname, filename)
137 | except Exception as e:
138 | stderr.write('ERROR: could not restore backup file for "' + filename + '": ' + str(e) + '\n')
139 |
140 | return ok
141 |
142 | class RestoreBackupImage(ProcessBase):
143 | """Processor which restores image from backup."""
144 |
145 | def __init__(self):
146 | ProcessBase.__init__(self)
147 | self.extensions = [self.backupextension]
148 |
149 | def processfile(self, filename):
150 | """Moves the backup file back to its original name."""
151 | try:
152 | move(filename, filename[: -(len(self.backupextension) + 1)])
153 | return True
154 | except Exception as e:
155 | stderr.write('Failed to restore backup file "' + filename + '": ' + str(e) + '\n')
156 | return False
157 |
158 | class DeleteBackupImage(ProcessBase):
159 | """Processor which deletes backup image."""
160 |
161 | def __init__(self):
162 | ProcessBase.__init__(self)
163 | self.extensions = [self.backupextension]
164 |
165 | def processfile(self, filename):
166 | """Deletes the specified file."""
167 | try:
168 | remove(filename)
169 | return True
170 | except Exception as e:
171 | stderr.write('Failed to delete backup file "' + filename + '": ' + str(e) + '\n')
172 | return False
173 |
174 | if __name__ == "__main__":
175 | # Argument parsing
176 | modecompress = '__compress_all'
177 | moderestorebackup = 'restorebackup'
178 | modedeletebackup = 'deletebackup'
179 | parser = ArgumentParser(description='Reduce file size of PNG and JPEG images.')
180 | parser.add_argument(
181 | 'path',
182 | help='File or directory name')
183 | parser.add_argument(
184 | '--mode', dest='mode', default=modecompress,
185 | choices=[modecompress, moderestorebackup, modedeletebackup],
186 | help='Mode to run with (default: ' + modecompress + '). '
187 | + modecompress + ': Compress the image(s). '
188 | + moderestorebackup + ': Restore the backup images (valid for directory path only). '
189 | + modedeletebackup + ': Delete the backup images (valid for directory path only).')
190 |
191 | args = parser.parse_args()
192 |
193 | # Construct processor requested mode
194 | if args.mode == modecompress:
195 | processor = CompressImage()
196 | elif args.mode == moderestorebackup:
197 | processor = RestoreBackupImage()
198 | elif args.mode == modedeletebackup:
199 | processor = DeleteBackupImage()
200 |
201 | # Run according to whether path is a file or a directory
202 | if isfile(args.path):
203 | if args.mode != modecompress:
204 | stderr.write('Mode "' + args.mode + '" supported on directories only.\n')
205 | exit(1)
206 | processor.processfile(args.path)
207 | elif isdir(args.path):
208 | filecount = processor.processdir(args.path)
209 | print('\nSuccessfully updated file count: ' + str(filecount))
210 | else:
211 | stderr.write('Invalid path "' + args.path + '"\n')
212 | exit(1)
213 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | auto-changelog>=0.6.0
2 | configparser>=7.1.0
3 | numpy>=2.2.1
4 | pillow>=11.1.0
5 | psutil>=6.1.1
6 | python-dateutil>=2.9.0.post0
7 | six>=1.17.0
8 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from setuptools import setup
2 |
3 | with open('megamanager/__version__.py') as f: exec(f.read())
4 |
5 | setup(
6 | name='megamanager',
7 | version=__version__,
8 | description='Multiple MEGA.co.nz account manager that has synchronization and compression capabilities. ',
9 | url='https://github.com/szmania/mega_manager',
10 | author='Curtis Szmania',
11 | author_email='szmania@yahoo.com',
12 | license='GNU General Public License v3.0',
13 | classifiers=[
14 | 'Development Status :: 3 - Alpha',
15 | 'Intended Audience :: Developers',
16 | 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
17 | 'Programming Language :: Python :: 2',
18 | ],
19 | keywords=['megamanager', 'mega', 'mega.co.nz', 'mega.nz', 'cloud', 'compression'],
20 | python_requires='>=3.10',
21 | packages=["megamanager"],
22 | install_requires=['numpy', 'psutil'],
23 | entry_points={
24 | 'console_scripts': [
25 | 'megamanager = megamanager.__main__:main',
26 | ],
27 | },
28 | )
29 |
--------------------------------------------------------------------------------