Reading Contents of RAR file for subtitle packages
#1
I am working on a subtitle addon and there are some subtitles packed by season in rar format.

like subs.rar
1) s1e1.srt
2) s1e2.srt
3) s1e3.srt
...

And the files in those packages do not have the same file as the playing video file.

What i wan to do for those packages, is to read the contents of rar file, pop up a dialog.select() and let the user chose which subtitle he wants to use. This is OK for zipfiles but rar files dont have pure pythonic extraction.

1) If i directly use this rar file xbmc expects a file in the package which has the same name as the playing video file. So this approach is not working
2) I can read the contents of rarfile with xbmcvfs.listdir(path.to.rar) but i can not access the content inside the package like xbmcvfs.File("path.to.rar" + "/" + "subtitle.srt"). Is there anyway for this approach
3) Since i can not access the file is possible to hack player to change playing listitems path with same name as the selected subtitle. This is hacky and ugly i know but sounds like an option.
4) What does rar:// scheme stand for.

Thanks.
Reply
#2
if i am not mistaken rar filesytem is called with

Xbmcvfs.open("rar://strcachepath,strrarpath,strfilepathinrar,strpwd")

May be, returned listitem path can ecactly accept rar:// url scheme as well, i will try asap, comment if im wrong ?
Reply
#3
previous post was the implementaion in 2004, here is the method for current implementation

looking at the code

/xbmc/filesystem/RarFile.cpp
Code:
void CRarFile::InitFromUrl(const CURL& url)
{
  m_strCacheDir = g_advancedSettings.m_cachePath;//url.GetDomain();
  URIUtils::AddSlashAtEnd(m_strCacheDir);
  m_strRarPath = url.GetHostName();
  m_strPassword = url.GetUserName();
  m_strPathInRar = url.GetFileName();

test.py
Code:
rarfile = "my/path/to/archive.rar"
uri = "rar://[%s]/path/inside/rar/file.srt" % rarfile
content = xbmcvfs.File(uri).read()

This works, and i can read contents of RAR file. If anyone needs in future
Reply
#4
Here is another problem:

when i xbmcvfs.listdir("rar://[path.to.file]/path.inside.rar"), on windows it opens and lists,contents however in android, it is trying to urlencode first [ char with %5B, and can not open.
normally i use unicode urls, but tried conver to str with utf-8 still result is the same. What can be wrong here? I am stuck.

tested in 16.0 in windows, 16.1 in android.

Code:
23:11:29 T:18446744073042508080 WARNING: virtual bool XFILE::CRarDirectory::GetDirectory(const CURL&, CFileItemList&): rar lib returned no files in archive [, likely corrupt
23:11:29 T:18446744073042508080   ERROR: static bool XFILE::CDirectory::GetDirectory(const CURL&, CFileItemList&, const XFILE::CDirectory::CHints&, bool) - Error getting rar://%5b/storage/emulated/0/Android/data/org.xbmc.kodi/files/.kodi/userdata/addon_data/service.subtitles.planetdp/temp/house-md-turkce-23976-fps-subrip-9015-planetdp.rar]

Code:
uri = "%s://[%s]%s" % (ar, fname, path)
    # uri = uri.encode("utf-8")
    print type(uri)
    # prints unicode
    print uri
    #prints rar://[/storage/emulated/0/Android/data/org.xbmc.kodi/files/.kodi/userdata/addon_data/service.subtitles.planetdp/temp/house-md-turkce-23976-fps-subrip-9015-planetdp.rar]
    ds, fs = xbmcvfs.listdir(uri)
    # can not open rar file under android, can open under windows
    # converting uir to string does not help
Reply
#5
remove the [] dude, that was just placeholder meaning 'url encode this bugger'.
Reply
#6
ok, ill try when i get back. but there is something i dont quite get. How does URL distguish below url?

Quote:rar://dir1/dir2/archive.rar/archive/dir4/file.txt

Quote:rarfile = /dir1/dir2/archive.rar
content in archive: /archive/dir4/file.txt

or

Quote:rarfile = /dir1/dir2/archive.rar/archive (assume that folder has . (archive.rar is a directory) and archive file does not have extension)
content in archive: /dir4/file.txt

and it is also weird that [path.to.rar] context works under windows but not under posix and encoded

in file /xbmc/URL.cpp
Code:
void CURL::Parse(const std::string& strURL1)
{
  Reset();
  // start by validating the path
  std::string strURL = CUtil::ValidatePath(strURL1);

  // strURL can be one of the following:
  // format 1: protocol://[username:password]@hostname[:port]/directoryandfile
  // format 2: protocol://file
  // format 3: drive:directoryandfile
  //
  // first need 2 check if this is a protocol or just a normal drive & path
  if (!strURL.size()) return ;
  if (strURL == "?") return;
Reply
#7
that's the whole point of the url encoding:

rar://[url encoded path to archive]/[path in archive]

this way, the archive is the *hostname* of your bog standard url interpretation.
Reply
#8
ah i udnerstand now, since path to archive is urlencoded there won't be "/"'s in it, thanks, this should, work, and there would be no need [, ] seperators. To be precise it should be processsed with urllib.quote_plus("my.path.to.arcvive")

UPDATE: YES it works like rar://[url encoded path to archive]/[path in archive] without brackets ofc.
Reply
#9
yes, the , thingie was an old url format used before it was beat into shape.
Reply
#10
(2017-03-09, 15:19)boogiepop Wrote: ah i udnerstand now, since path to archive is urlencoded there won't be "/"'s in it, thanks, this should, work, and there would be no need [, ] seperators. To be precise it should be processsed with urllib.quote_plus("my.path.to.arcvive")

UPDATE: YES it works like rar://[url encoded path to archive]/[path in archive] without brackets ofc.

quote_plus might raise errors, I found out that plain urllib.quote(uri) works better.
Reply
#11
(2017-09-21, 19:25)twilight0 Wrote:
(2017-03-09, 15:19)boogiepop Wrote: ah i udnerstand now, since path to archive is urlencoded there won't be "/"'s in it, thanks, this should, work, and there would be no need [, ] seperators. To be precise it should be processsed with urllib.quote_plus("my.path.to.arcvive")

UPDATE: YES it works like rar://[url encoded path to archive]/[path in archive] without brackets ofc.

quote_plus might raise errors, I found out that plain urllib.quote(uri) works better.

Correcting myself, after some tests, it appears that windows os requires quote while unix-like platforms require quote_plus.
Reply

Logout Mark Read Team Forum Stats Members Help
Reading Contents of RAR file for subtitle packages0