├── .gitignore ├── LICENSE ├── Plug-ins └── Xattr.bundle │ └── Contents │ ├── Code │ ├── __init__.py │ ├── filebot.py │ └── xattr.py │ ├── Info.plist │ └── Resources │ └── icon-default.png ├── README.md └── Scanners ├── Common ├── filebot.py └── xattr.py ├── Movies └── FileBot Xattr Movie Scanner.py └── Series └── FileBot Xattr Series Scanner.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 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Plug-ins/Xattr.bundle/Contents/Code/__init__.py: -------------------------------------------------------------------------------- 1 | from filebot import * 2 | 3 | 4 | def Start(): 5 | Log("FileBot Xattr Metadata Agent - CPU: %s, OS: %s" % (Platform.CPU, Platform.OS)) 6 | 7 | 8 | ##################################################################################################################### 9 | 10 | 11 | class XattrMovieAgent(Agent.Movies): 12 | name = 'FileBot Xattr Metadata (Movies)' 13 | languages = [Locale.Language.NoLanguage] 14 | primary_provider = False 15 | contributes_to = ['com.plexapp.agents.imdb', 'com.plexapp.agents.themoviedb', 'com.plexapp.agents.localmedia', 'com.plexapp.agents.none'] 16 | 17 | 18 | def search(self, results, media, lang): 19 | Log("[SEARCH]") 20 | 21 | if media.items is None: 22 | return 23 | 24 | file = media.items[0].parts[0].file 25 | Log("[FILE] %s" % file) 26 | 27 | attr = xattr_metadata(file) 28 | Log("[XATTR] %s" % attr) 29 | 30 | if attr is None: 31 | return 32 | 33 | mid = movie_id(attr) 34 | Log("[MOVIE] %s" % mid) 35 | 36 | if mid is None: 37 | return 38 | 39 | results.Append(MetadataSearchResult(id=mid, name=movie_name(attr), year=movie_year(attr), lang=lang, score=100)) 40 | 41 | 42 | def update(self, metadata, media, lang): 43 | Log("[UPDATE]") 44 | 45 | if media.items is None: 46 | return 47 | 48 | file = media.items[0].parts[0].file 49 | Log("[FILE] %s" % file) 50 | 51 | attr = xattr_metadata(file) 52 | Log("[XATTR] %s" % attr) 53 | 54 | if attr is None: 55 | return 56 | 57 | mid = movie_id(attr) 58 | Log("[MOVIE] %s" % mid) 59 | 60 | if mid is None: 61 | return 62 | 63 | metadata.id = mid 64 | metadata.title = movie_name(attr) 65 | metadata.year = movie_year(attr) 66 | 67 | 68 | ##################################################################################################################### 69 | 70 | 71 | class XattrSeriesAgent(Agent.TV_Shows): 72 | name = 'FileBot Xattr Metadata (TV)' 73 | languages = [Locale.Language.NoLanguage] 74 | primary_provider = False 75 | contributes_to = ['com.plexapp.agents.thetvdb', 'com.plexapp.agents.themoviedb', 'com.plexapp.agents.localmedia', 'com.plexapp.agents.none'] 76 | 77 | 78 | def search(self, results, media, lang): 79 | for s in media.seasons: 80 | for e in media.seasons[s].episodes: 81 | for i in media.seasons[s].episodes[e].items: 82 | for part in i.parts: 83 | Log("[SEARCH] %s | %s" % (s, e)) 84 | 85 | file = part.file 86 | Log("[FILE] %s" % file) 87 | 88 | attr = xattr_metadata(file) 89 | Log("[XATTR] %s" % attr) 90 | 91 | if attr is None: 92 | continue 93 | 94 | sid = series_id(attr) 95 | Log("[SERIES] %s" % sid) 96 | 97 | if sid is None: 98 | continue 99 | 100 | results.Append(MetadataSearchResult(id=sid, name=series_name(attr), year=series_year(attr), lang=lang, score=100)) 101 | 102 | 103 | def update(self, metadata, media, lang): 104 | for s in media.seasons: 105 | for e in media.seasons[s].episodes: 106 | for i in media.seasons[s].episodes[e].items: 107 | for part in i.parts: 108 | Log("[UPDATE] %s | %s" % (s, e)) 109 | 110 | file = part.file 111 | Log("[FILE] %s" % file) 112 | 113 | attr = xattr_metadata(file) 114 | Log("[XATTR] %s" % attr) 115 | 116 | if attr is None: 117 | return 118 | 119 | sid = series_id(attr) 120 | Log("[SERIES] %s" % sid) 121 | 122 | if sid is None: 123 | continue 124 | 125 | metadata.id = sid 126 | metadata.title = series_name(attr) 127 | metadata.originally_available_at = series_date(attr) 128 | metadata.content_rating = series_certification(attr) 129 | metadata.studio = series_network(attr) 130 | metadata.duration = series_runtime(attr) 131 | metadata.rating = series_rating(attr) 132 | metadata.genres = series_genres(attr) 133 | 134 | episode = metadata.seasons[s].episodes[e] 135 | 136 | episode.title = episode_title(attr) 137 | episode.absolute_index = episode_absolute_number(attr) 138 | episode.originally_available_at = episode_date(attr) 139 | -------------------------------------------------------------------------------- /Plug-ins/Xattr.bundle/Contents/Code/filebot.py: -------------------------------------------------------------------------------- 1 | import json, xattr 2 | 3 | from os.path import * 4 | from datetime import * 5 | 6 | 7 | ##################################################################################################################### 8 | 9 | 10 | def getxattr(file, key): 11 | # try xattr 12 | value = xattr.getxattr(file, key) 13 | if value: 14 | return value 15 | 16 | # try plain file xattr store 17 | return getxattr_plain_file('.xattr', file, key) 18 | 19 | 20 | def getxattr_plain_file(store, file, name): 21 | if not isabs(store): 22 | store = join(dirname(file), store) 23 | 24 | xattr_file = join(store, basename(file), name) 25 | 26 | if not isfile(xattr_file): 27 | return None 28 | 29 | fd = open(xattr_file, "rb") 30 | buffer = fd.read() 31 | fd.close() 32 | return buffer.decode('utf-8') 33 | 34 | 35 | def xattr_metadata(file): 36 | value = getxattr(file, 'net.filebot.metadata') 37 | return json.loads(value) if value else None 38 | 39 | 40 | def xattr_filename(file): 41 | return getxattr(file, 'net.filebot.filename') 42 | 43 | 44 | ##################################################################################################################### 45 | 46 | 47 | def movie_id(attr): 48 | imdb_id = attr.get('imdbId') 49 | if imdb_id > 0: 50 | return u"tt%07d" % imdb_id 51 | 52 | tmdb_id = attr.get('tmdbId') 53 | if tmdb_id > 0: 54 | return u"%01d" % tmdb_id 55 | 56 | return None 57 | 58 | 59 | def movie_guid(attr): 60 | imdb_id = attr.get('imdbId') 61 | if imdb_id > 0: 62 | return u"tt%07d" % imdb_id 63 | 64 | tmdb_id = attr.get('tmdbId') 65 | if tmdb_id > 0: 66 | return u"com.plexapp.agents.themoviedb://%s?lang=%s" % (tmdb_id, movie_language(attr)) 67 | 68 | return None 69 | 70 | 71 | def movie_name(attr): return attr.get('name') 72 | def movie_year(attr): return attr.get('year') 73 | def movie_language(attr): return attr.get('language') 74 | 75 | def movie_part_index(attr): return attr.get('partIndex') 76 | def movie_part_count(attr): return attr.get('partCount') 77 | 78 | 79 | ##################################################################################################################### 80 | 81 | 82 | def series_id(attr): 83 | series_id = attr_get(attr, 'seriesInfo', 'id') 84 | if series_id > 0: 85 | db = attr_get(attr, 'seriesInfo', 'database') 86 | return u"%s_%s" % (db, series_id) 87 | 88 | return None 89 | 90 | 91 | def series_guid(attr): 92 | series_id = attr_get(attr, 'seriesInfo', 'id') 93 | if series_id > 0: 94 | db = attr_get(attr, 'seriesInfo', 'database') 95 | lang = series_language(attr) 96 | if db == 'TheTVDB': 97 | return u"com.plexapp.agents.thetvdb://%s?lang=%s" % (series_id, lang) 98 | elif db == 'TheMovieDB::TV': 99 | return u"com.plexapp.agents.themoviedb://%s?lang=%s" % (series_id, lang) 100 | elif db == 'AniDB': 101 | return u"com.plexapp.agents.hama://%s?lang=%s" % (series_id, lang) 102 | else: 103 | return u"%s://%s" % (db.lower(), series_id) 104 | 105 | return None 106 | 107 | 108 | def list_episodes(attr): 109 | # check for single episode 110 | if series_name(attr): 111 | return [attr] 112 | 113 | # check for multi episode 114 | episodes = attr_get(attr, 'episodes') 115 | if episodes: 116 | return episodes 117 | 118 | return [] 119 | 120 | 121 | def series_name(attr): return attr_get(attr, 'seriesName') 122 | def series_year(attr): return attr_get(attr, 'seriesInfo', 'startDate', 'year') 123 | 124 | 125 | def series_language(attr): return attr_get(attr, 'seriesInfo', 'language') 126 | def series_date(attr): return attr_date(attr_get(attr, 'seriesInfo', 'startDate')) 127 | def series_certification(attr): return attr_get(attr, 'seriesInfo', 'certification') 128 | def series_network(attr): return attr_get(attr, 'seriesInfo', 'network') 129 | def series_runtime(attr): return attr_get(attr, 'seriesInfo', 'runtime') 130 | def series_rating(attr): return attr_get(attr, 'seriesInfo', 'rating') 131 | def series_genres(attr): return attr_get(attr, 'seriesInfo', 'genres') 132 | 133 | 134 | def episode_number(attr): return attr.get('episode') 135 | def episode_season_number(attr): return attr.get('season') 136 | def episode_special_number(attr): return attr.get('special') 137 | def episode_title(attr): return attr.get('title') 138 | def episode_absolute_number(attr): return attr.get('absolute') 139 | def episode_date(attr): return attr_date(attr_get(attr, 'airdate')) 140 | 141 | 142 | def attr_get(attr, *keys): 143 | for k in keys: 144 | attr = attr.get(k) 145 | if attr is None: 146 | return None 147 | return attr 148 | 149 | 150 | def attr_date(attr): 151 | if attr is not None: 152 | return datetime(year=attr['year'], month=attr['month'], day=attr['day']) 153 | return None 154 | -------------------------------------------------------------------------------- /Plug-ins/Xattr.bundle/Contents/Code/xattr.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | 4 | # Windows / NTFS Alternative Streams 5 | if os.name == 'nt': 6 | def getxattr(file, name): 7 | try: 8 | fd = open("%s:%s" % (file, name), 'rb') 9 | buffer = fd.read() 10 | fd.close() 11 | return buffer.decode('utf-8') 12 | except: 13 | return None 14 | 15 | 16 | # Unix / xattr 17 | elif os.name == 'posix': 18 | from ctypes import * 19 | 20 | # use current process to load library 21 | libc = CDLL(None) 22 | 23 | if sys.platform.find('linux') >= 0: 24 | libc.getxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t) 25 | libc.getxattr.restype = c_ssize_t 26 | def getxattr_impl(file, name, buffer): 27 | return libc.getxattr(fsencode(file), fsencode('user.'+name), buffer, sizeof(buffer)) 28 | 29 | elif sys.platform.find('darwin') >= 0: 30 | libc.getxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_uint32, c_int) 31 | libc.getxattr.restype = c_ssize_t 32 | def getxattr_impl(file, name, buffer): 33 | return libc.getxattr(fsencode(file), fsencode(name), buffer, sizeof(buffer), 0, 0) 34 | 35 | elif sys.platform.find('bsd') >= 0: 36 | libc.extattr_get_file.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t) 37 | libc.extattr_get_file.restype = c_ssize_t 38 | def getxattr_impl(file, name, buffer): 39 | return libc.extattr_get_file(fsencode(file), 0x1, fsencode(name), buffer, sizeof(buffer)) #define EXTATTR_NAMESPACE_USER 0x00000001 40 | 41 | 42 | def fsencode(file): 43 | return file.encode('utf-8') # do not use sys.getfilesystemencoding() because it defaults to ascii on badly configured Linux machines 44 | 45 | def getxattr(file, name): 46 | buffer = create_string_buffer(8 * 1024) # xattr size may be limited to 4 KB or less (e.g. ext4) 47 | n = getxattr_impl(file, name, buffer) 48 | if n > 0: 49 | return buffer.raw[0:n].decode('utf-8') 50 | else: 51 | return None 52 | 53 | 54 | # python xattr.py /path/to/file net.filebot.metadata 55 | if __name__ == "__main__": 56 | file = sys.argv[1] 57 | name = sys.argv[2] 58 | print(getxattr(file, name)) 59 | -------------------------------------------------------------------------------- /Plug-ins/Xattr.bundle/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleIdentifier 6 | com.plexapp.agents.xattr 7 | PlexFrameworkVersion 8 | 2 9 | PlexPluginClass 10 | Agent 11 | PlexPluginCodePolicy 12 | Elevated 13 | 14 | 15 | -------------------------------------------------------------------------------- /Plug-ins/Xattr.bundle/Contents/Resources/icon-default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/filebot/plex-agents/ab6ebb811ca9d94dea0f72eebf17b45465877c7e/Plug-ins/Xattr.bundle/Contents/Resources/icon-default.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FileBot Xattr Metadata Scanners & Plug-ins 2 | 3 | Enhance Plex with support for [FileBot Xattr Metadata](https://www.filebot.net/forums/viewtopic.php?f=3&t=324). 4 | 5 | 6 | ## Install 7 | 8 | 1. Download [plex-agents.zip](https://github.com/filebot/plex-agents/archive/master.zip) 9 | 2. Copy the `Scanners` and `Plug-ins` folders into the [`Plex Media Server`](https://support.plex.tv/articles/202915258-where-is-the-plex-media-server-data-directory-located/) data directory 10 | 3. Restart Plex 11 | 4. Configure `Advanced ➔ Scanner` for each library and select `FileBot Xattr Scanner` 12 | 5. Configure `Settings ➔ Agents` and enable `FileBot Xattr Metadata` and move it to the top for each primary agent 13 | ```sh 14 | #!/bin/sh -xu 15 | curl -L -O https://github.com/filebot/plex-agents/archive/master.zip 16 | unzip -o master.zip 17 | cp -vru 'plex-agents-master/Plug-ins' '/path/to/PlexMediaServer/Library/Plex Media Server' 18 | cp -vru 'plex-agents-master/Scanners' '/path/to/PlexMediaServer/Library/Plex Media Server' 19 | ``` 20 | 21 | ## FileBot Xattr Metadata Scanner 22 | 23 | The `FileBot Xattr Scanner` will read `name / year / season / episode / etc` from xattr metadata instead of guessing and parsing information from the file path. This scanner will greatly enhance primary agents such as `Plex Movie` or `TheTVDB`, regardless of whether files are named according to [`{plex}`](https://www.filebot.net/forums/viewtopic.php?f=5&t=4116) standards or not. Files without xattr metadata will be ignored. 24 | 25 | 26 | ## FileBot Xattr Metadata Agent 27 | 28 | The `FileBot Xattr Metadata` secondary agent can contribute `title / airdate / etc` from xattr metadata instantly, to improve your browsing experience while the primary agent is still busy in the background retrieving complete movie and episode information and artwork. 29 | -------------------------------------------------------------------------------- /Scanners/Common/filebot.py: -------------------------------------------------------------------------------- 1 | import json, xattr 2 | 3 | from os.path import * 4 | from datetime import * 5 | 6 | 7 | ##################################################################################################################### 8 | 9 | 10 | def getxattr(file, key): 11 | # try xattr 12 | value = xattr.getxattr(file, key) 13 | if value: 14 | return value 15 | 16 | # try plain file xattr store 17 | return getxattr_plain_file('.xattr', file, key) 18 | 19 | 20 | def getxattr_plain_file(store, file, name): 21 | if not isabs(store): 22 | store = join(dirname(file), store) 23 | 24 | xattr_file = join(store, basename(file), name) 25 | 26 | if not isfile(xattr_file): 27 | return None 28 | 29 | fd = open(xattr_file, "rb") 30 | buffer = fd.read() 31 | fd.close() 32 | return buffer.decode('utf-8') 33 | 34 | 35 | def xattr_metadata(file): 36 | value = getxattr(file, 'net.filebot.metadata') 37 | return json.loads(value) if value else None 38 | 39 | 40 | def xattr_filename(file): 41 | return getxattr(file, 'net.filebot.filename') 42 | 43 | 44 | ##################################################################################################################### 45 | 46 | 47 | def movie_id(attr): 48 | imdb_id = attr.get('imdbId') 49 | if imdb_id > 0: 50 | return u"tt%07d" % imdb_id 51 | 52 | tmdb_id = attr.get('tmdbId') 53 | if tmdb_id > 0: 54 | return u"%01d" % tmdb_id 55 | 56 | return None 57 | 58 | 59 | def movie_guid(attr): 60 | imdb_id = attr.get('imdbId') 61 | if imdb_id > 0: 62 | return u"tt%07d" % imdb_id 63 | 64 | tmdb_id = attr.get('tmdbId') 65 | if tmdb_id > 0: 66 | return u"com.plexapp.agents.themoviedb://%s?lang=%s" % (tmdb_id, movie_language(attr)) 67 | 68 | return None 69 | 70 | 71 | def movie_name(attr): return attr.get('name') 72 | def movie_year(attr): return attr.get('year') 73 | def movie_language(attr): return attr.get('language') 74 | 75 | def movie_part_index(attr): return attr.get('partIndex') 76 | def movie_part_count(attr): return attr.get('partCount') 77 | 78 | 79 | ##################################################################################################################### 80 | 81 | 82 | def series_id(attr): 83 | series_id = attr_get(attr, 'seriesInfo', 'id') 84 | if series_id > 0: 85 | db = attr_get(attr, 'seriesInfo', 'database') 86 | return u"%s_%s" % (db, series_id) 87 | 88 | return None 89 | 90 | 91 | def series_guid(attr): 92 | series_id = attr_get(attr, 'seriesInfo', 'id') 93 | if series_id > 0: 94 | db = attr_get(attr, 'seriesInfo', 'database') 95 | lang = series_language(attr) 96 | if db == 'TheTVDB': 97 | return u"com.plexapp.agents.thetvdb://%s?lang=%s" % (series_id, lang) 98 | elif db == 'TheMovieDB::TV': 99 | return u"com.plexapp.agents.themoviedb://%s?lang=%s" % (series_id, lang) 100 | elif db == 'AniDB': 101 | return u"com.plexapp.agents.hama://%s?lang=%s" % (series_id, lang) 102 | else: 103 | return u"%s://%s" % (db.lower(), series_id) 104 | 105 | return None 106 | 107 | 108 | def list_episodes(attr): 109 | # check for single episode 110 | if series_name(attr): 111 | return [attr] 112 | 113 | # check for multi episode 114 | episodes = attr_get(attr, 'episodes') 115 | if episodes: 116 | return episodes 117 | 118 | return [] 119 | 120 | 121 | def series_name(attr): return attr_get(attr, 'seriesName') 122 | def series_year(attr): return attr_get(attr, 'seriesInfo', 'startDate', 'year') 123 | 124 | 125 | def series_language(attr): return attr_get(attr, 'seriesInfo', 'language') 126 | def series_date(attr): return attr_date(attr_get(attr, 'seriesInfo', 'startDate')) 127 | def series_certification(attr): return attr_get(attr, 'seriesInfo', 'certification') 128 | def series_network(attr): return attr_get(attr, 'seriesInfo', 'network') 129 | def series_runtime(attr): return attr_get(attr, 'seriesInfo', 'runtime') 130 | def series_rating(attr): return attr_get(attr, 'seriesInfo', 'rating') 131 | def series_genres(attr): return attr_get(attr, 'seriesInfo', 'genres') 132 | 133 | 134 | def episode_number(attr): return attr.get('episode') 135 | def episode_season_number(attr): return attr.get('season') 136 | def episode_special_number(attr): return attr.get('special') 137 | def episode_title(attr): return attr.get('title') 138 | def episode_absolute_number(attr): return attr.get('absolute') 139 | def episode_date(attr): return attr_date(attr_get(attr, 'airdate')) 140 | 141 | 142 | def attr_get(attr, *keys): 143 | for k in keys: 144 | attr = attr.get(k) 145 | if attr is None: 146 | return None 147 | return attr 148 | 149 | 150 | def attr_date(attr): 151 | if attr is not None: 152 | return datetime(year=attr['year'], month=attr['month'], day=attr['day']) 153 | return None 154 | -------------------------------------------------------------------------------- /Scanners/Common/xattr.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | 4 | # Windows / NTFS Alternative Streams 5 | if os.name == 'nt': 6 | def getxattr(file, name): 7 | try: 8 | fd = open("%s:%s" % (file, name), 'rb') 9 | buffer = fd.read() 10 | fd.close() 11 | return buffer.decode('utf-8') 12 | except: 13 | return None 14 | 15 | 16 | # Unix / xattr 17 | elif os.name == 'posix': 18 | from ctypes import * 19 | 20 | # use current process to load library 21 | libc = CDLL(None) 22 | 23 | if sys.platform.find('linux') >= 0: 24 | libc.getxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t) 25 | libc.getxattr.restype = c_ssize_t 26 | def getxattr_impl(file, name, buffer): 27 | return libc.getxattr(fsencode(file), fsencode('user.'+name), buffer, sizeof(buffer)) 28 | 29 | elif sys.platform.find('darwin') >= 0: 30 | libc.getxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_uint32, c_int) 31 | libc.getxattr.restype = c_ssize_t 32 | def getxattr_impl(file, name, buffer): 33 | return libc.getxattr(fsencode(file), fsencode(name), buffer, sizeof(buffer), 0, 0) 34 | 35 | elif sys.platform.find('bsd') >= 0: 36 | libc.extattr_get_file.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t) 37 | libc.extattr_get_file.restype = c_ssize_t 38 | def getxattr_impl(file, name, buffer): 39 | return libc.extattr_get_file(fsencode(file), 0x1, fsencode(name), buffer, sizeof(buffer)) #define EXTATTR_NAMESPACE_USER 0x00000001 40 | 41 | 42 | def fsencode(file): 43 | return file.encode('utf-8') # do not use sys.getfilesystemencoding() because it defaults to ascii on badly configured Linux machines 44 | 45 | def getxattr(file, name): 46 | buffer = create_string_buffer(8 * 1024) # xattr size may be limited to 4 KB or less (e.g. ext4) 47 | n = getxattr_impl(file, name, buffer) 48 | if n > 0: 49 | return buffer.raw[0:n].decode('utf-8') 50 | else: 51 | return None 52 | 53 | 54 | # python xattr.py /path/to/file net.filebot.metadata 55 | if __name__ == "__main__": 56 | file = sys.argv[1] 57 | name = sys.argv[2] 58 | print(getxattr(file, name)) 59 | -------------------------------------------------------------------------------- /Scanners/Movies/FileBot Xattr Movie Scanner.py: -------------------------------------------------------------------------------- 1 | import Media, VideoFiles 2 | 3 | from filebot import * 4 | 5 | VideoFiles.ignore_dirs.remove('bdmv') 6 | excl_dirs = ['backup', 'clipinf', 'playlist'] 7 | VideoFiles.ignore_dirs.extend(excl_dirs) 8 | 9 | def Scan(path, files, mediaList, subdirs, language=None, root=None): 10 | VideoFiles.Scan(path, files, mediaList, subdirs, root) 11 | 12 | # require alphabetic order 13 | files.sort() 14 | 15 | # group movie parts as they come in sequence 16 | prev_media = None 17 | prev_part_index = None 18 | 19 | for file in files: 20 | attr = xattr_metadata(file) 21 | if attr is None: 22 | continue 23 | 24 | guid = movie_guid(attr) 25 | if guid is None: 26 | continue 27 | 28 | print("[XATTR] %s | %s" % (guid, attr)) 29 | 30 | part_index = movie_part_index(attr) 31 | if prev_part_index and part_index and prev_part_index + 1 == part_index and prev_media.guid == guid: 32 | prev_part_index = part_index 33 | prev_media.parts.append(file) 34 | print("[MEDIA] %s | Part %s" % (prev_media, part_index)) 35 | continue 36 | 37 | media = Media.Movie( 38 | movie_name(attr).encode('utf-8'), # use str since Plex doesn't like unicode strings 39 | movie_year(attr) 40 | ) 41 | media.guid = guid.encode('utf-8') 42 | 43 | original_filename = xattr_filename(file) 44 | if original_filename: 45 | media.source = VideoFiles.RetrieveSource(original_filename.encode('utf-8')) 46 | 47 | media.parts.append(file) 48 | mediaList.append(media) 49 | 50 | if part_index == 1: 51 | prev_media = media 52 | prev_part_index = 1 53 | else: 54 | prev_media = None 55 | prev_part_index = None 56 | 57 | print("[MEDIA] %s | %s | %s | %s" % (media, media.year, media.released_at, media.source)) 58 | -------------------------------------------------------------------------------- /Scanners/Series/FileBot Xattr Series Scanner.py: -------------------------------------------------------------------------------- 1 | import re, Media, VideoFiles 2 | 3 | from filebot import * 4 | 5 | VideoFiles.ignore_dirs.remove('bdmv') 6 | excl_dirs = ['backup', 'clipinf', 'playlist'] 7 | VideoFiles.ignore_dirs.extend(excl_dirs) 8 | 9 | def Scan(path, files, mediaList, subdirs, language=None, root=None): 10 | VideoFiles.Scan(path, files, mediaList, subdirs, root) 11 | 12 | for file in files: 13 | attr = xattr_metadata(file) 14 | if attr is None: 15 | continue 16 | 17 | print("[XATTR] %s" % attr) 18 | 19 | # single episode | multi episode 20 | episodes = list_episodes(attr) 21 | multi_episode_count = len(episodes) 22 | 23 | for i, attr in enumerate(episodes): 24 | guid = series_guid(attr) 25 | name = series_name(attr) 26 | special = episode_special_number(attr) 27 | 28 | media = Media.Episode( 29 | name.encode('utf-8'), # use str since Plex doesn't like unicode strings 30 | 0 if special else episode_season_number(attr), 31 | special if special else episode_number(attr), 32 | episode_title(attr).encode('utf-8'), # use str since Plex doesn't like unicode strings 33 | series_year(attr) 34 | ) 35 | 36 | date = episode_date(attr) 37 | if date: 38 | media.released_at = date.strftime('%Y-%m-%d') 39 | 40 | if (multi_episode_count > 1): 41 | media.display_offset = (i * 100) / multi_episode_count 42 | 43 | original_filename = xattr_filename(file) 44 | if original_filename: 45 | media.source = VideoFiles.RetrieveSource(original_filename.encode('utf-8')) 46 | 47 | media.parts.append(file) 48 | mediaList.append(media) 49 | 50 | print("[MEDIA] %s | %s | %s | %s" % (media, media.year, media.released_at, media.source)) 51 | --------------------------------------------------------------------------------