def more_kind(children_data): # title = '' if children_data['depth'] == 0 else '>' * children_data['depth'] + ' ' + control.lang(30117) title = control.lang(30144) name, id = (children_data['name'], children_data['id']) if len(name) < 10: name = children_data['parent_id'] if len(id) < 7: id = children_data['parent_id'][3:] parsed = urlparse(link) permalink = urlunparse(parsed._replace(path=parsed.path + id)) if children_data['children']: replies_urls = json.dumps([ urlunparse(parsed._replace(path=parsed.path + u)) for u in children_data['children'] ]) else: replies_urls = None image = control.addonInfo('icon') pairs = { 'title': title, 'name': name, 'id': id, 'image': image, 'kind': 'more', 'permalink': permalink, 'replies_urls': replies_urls } return pairs
def quote_paths(url): """ This function will quote paths **only** in a given url :param url: string or unicode :return: joined url string """ try: url = py2_enc(url) if url.startswith('http'): parsed = urlparse(url) processed_path = '/'.join( [quote(i) for i in parsed.path.split('/')]) url = urlunparse(parsed._replace(path=processed_path)) return url else: path = '/'.join([quote(i) for i in url.split('/')]) return path except Exception: return url
def next_appender(json_data): try: next_id = json_data['after'] if not next_id: raise KeyError elif '&after=' in parsed.query: _next_url = urlunparse( parsed._replace( query=re.sub(r'&after=\w{8,9}', r'&after=' + next_id, parsed.query))) else: _next_url = urlunparse( parsed._replace(query=parsed.query + '&after=' + next_id)) except KeyError: _next_url = '' return _next_url
def items_list(self, link): if not link.startswith('http'): link = base_link() + link link = client.quote_paths(link) link = link.replace('old.', 'oauth.' if access_boolean() else 'www.') link = link.replace('www.', 'oauth.' if access_boolean() else 'www.') #### Start of nested helper functions #### # Pulls images and thumbnails def image_generator(children_data): print(children_data) image = control.addonInfo('icon') fanart = control.fanart() try: try: m_thumb = children_data.get('media').get('oembed').get( 'thumbnail_url') except AttributeError: m_thumb = None try: s_thumb = children_data.get('secure_media').get( 'oembed').get('thumbnail_url') except AttributeError: s_thumb = None try: p_thumb = children_data.get('preview').get('oembed').get( 'thumbnail_url') except AttributeError: p_thumb = None try: u_thumb = children_data.get('preview').get( 'images')[0].get('source').get('url') except AttributeError: u_thumb = None images = [ children_data.get('community_icon'), children_data.get('icon_img'), children_data.get('header_img'), children_data.get('thumbnail'), children_data.get('icon_img'), children_data.get('header_img'), children_data.get('banner_img'), children_data.get('url') ] if m_thumb: images.insert(-2, m_thumb) if s_thumb: images.insert(-2, s_thumb) if p_thumb: images.insert(-2, p_thumb) if u_thumb: images.insert(-2, u_thumb) for i in images: if i in ['default', 'spoiler', 'image', 'self'] or not i: continue elif '.jpg' in i or '.png' in i: image = i break if '?' in image: image = image.partition('?')[0] except (KeyError, IndexError, TypeError): pass if 'embed.ly' in image: image = dict(parse_qsl(urlparse(image).query))['url'] try: try: p_fanart = children_data.get('preview').get( 'images')[0].get('source').get('url') except AttributeError: p_fanart = None try: s_fanart = children_data.get('secure_media').get( 'oembed').get('thumbnail_url') except AttributeError: s_fanart = None fanarts = [children_data.get('banner_background_image')] if p_fanart: fanarts.insert(0, p_fanart) if s_fanart: fanarts.insert(-1, s_fanart) for f in fanarts: if not f: continue elif f: fanart = f break if '?' in fanart: fanart = fanart.partition('?')[0] except (KeyError, IndexError): pass return image, fanart # Comment def t1_kind(children_data, next_url): author = children_data['author'] body = legacy_replace(children_data['body']) short = legacy_replace(body[:50] + '...') image = control.addonInfo('icon') subreddit = children_data['subreddit'] subreddit_id = children_data['subreddit_id'] name = children_data['name'] if children_data['replies']: reply_json = children_data['replies'] replies_children = reply_json['data']['children'] replies = len(replies_children) try: comprehension = [ base_link() + client.quote_paths(r['data']['permalink']) for r in replies_children ] replies_urls = json.dumps(comprehension) except KeyError: replies_urls = None else: replies_urls = None replies = 0 replies_num = ' | ' + control.lang(30102) + str( replies) if replies > 0 else '' title = short.replace( '\n', '') + self.formatting + '[I]' + author + '[/I]' + replies_num url = permalink = base_link() + children_data['permalink'] link_id = children_data['link_id'] pairs = { 'title': title, 'url': url, 'permalink': permalink, 'image': image, 'subreddit': subreddit, 'kind': 't1', 'subreddit_url': base_link() + '/r/' + subreddit, 'next': next_url, 'subreddit_id': subreddit_id, 'name': name, 'body': body, 'plot': body, 'query': replies_urls, 'replies_urls': replies_urls, 'link_id': link_id } return pairs # Link/Thread def t3_kind(children_data, next_url): title = client.replaceHTMLCodes(children_data['title']) name = children_data['name'] author = children_data['author'] domain = children_data['domain'] num_comments = str(children_data['num_comments']) try: if domain.startswith('self.'): selftext = legacy_replace(children_data['selftext']) if selftext == '': selftext = title else: selftext = None except KeyError: selftext = None subreddit = children_data['subreddit'] subreddit_id = children_data['subreddit_id'] url = children_data['url'] permalink = base_link() + children_data['permalink'] image, fanart = image_generator(children_data) if access_boolean() and 'reddit' in url and not 'video' in url: url = url.replace('www.reddit', 'oauth.reddit') label = title + ' | ' + subreddit + ' | ' + '[B]' + author + '[/B]' + self.formatting + '[I]' + domain + '[/I]' + ' | ' + '[B]' + control.lang( 30103) + num_comments + '[/B]' pairs = { 'label': label, 'title': title, 'url': url, 'image': image, 'fanart': fanart, 'next': next_url, 'subreddit_id': subreddit_id, 'subreddit': subreddit, 'subreddit_url': base_link() + '/r/' + subreddit, 'kind': 't3', 'permalink': permalink, 'domain': domain, 'name': name, 'selftext': selftext, 'author': author, 'plot': selftext, 'query': client.quote_paths(permalink) } return pairs # Subreddit def t5_kind(children_data, next_url): display_name = client.replaceHTMLCodes( children_data['display_name']) title = client.replaceHTMLCodes(children_data['title']) public_description = legacy_replace( children_data['public_description']) description = legacy_replace(children_data['description']) plot = json.dumps({ 'title': title, 'public_description': public_description, 'description': description }) subscribers = str(children_data['subscribers']) url = base_link() + children_data['url'] name = children_data['name'] image, fanart = image_generator(children_data) pairs = { 'title': title + ' | ' + subscribers + self.formatting + '[I]' + display_name + '[/I]', 'url': url, 'image': image, 'next': next_url, 'fanart': fanart, 'display_name': display_name, 'name': name, 'kind': 't5', 'plot': plot } return pairs # Multi def lm_kind(children_data): display_name = children_data['display_name'] name = children_data['name'] # description = html_processor(children_data['description_html']) try: image = children_data['icon_url'] if not image: raise KeyError except KeyError: image = control.addonInfo('icon') path = base_link() + children_data['path'] subreddits = json.dumps(children_data['subreddits']) pairs = { 'title': display_name, 'url': path, 'image': image, 'subreddits': subreddits, 'kind': 'LabeledMulti', 'name': name } return pairs def more_kind(children_data): # title = '' if children_data['depth'] == 0 else '>' * children_data['depth'] + ' ' + control.lang(30117) title = control.lang(30144) name, id = (children_data['name'], children_data['id']) if len(name) < 10: name = children_data['parent_id'] if len(id) < 7: id = children_data['parent_id'][3:] parsed = urlparse(link) permalink = urlunparse(parsed._replace(path=parsed.path + id)) if children_data['children']: replies_urls = json.dumps([ urlunparse(parsed._replace(path=parsed.path + u)) for u in children_data['children'] ]) else: replies_urls = None image = control.addonInfo('icon') pairs = { 'title': title, 'name': name, 'id': id, 'image': image, 'kind': 'more', 'permalink': permalink, 'replies_urls': replies_urls } return pairs def next_appender(json_data): try: next_id = json_data['after'] if not next_id: raise KeyError elif '&after=' in parsed.query: _next_url = urlunparse( parsed._replace( query=re.sub(r'&after=\w{8,9}', r'&after=' + next_id, parsed.query))) else: _next_url = urlunparse( parsed._replace(query=parsed.query + '&after=' + next_id)) except KeyError: _next_url = '' return _next_url def processor(_json): if isinstance(_json, list): for j in _json: data = j['data'] kind = j['kind'] if kind == 'LabeledMulti': pairs = lm_kind(data) self.data.append(pairs) else: children = data['children'] nu = next_appender(data) for c in children: kind = c['kind'] data = c['data'] if kind == 't3': pairs = t3_kind(data, nu) elif kind == 't1': pairs = t1_kind(data, nu) elif kind == 'more': pairs = more_kind(data) else: pairs = None self.data.append(pairs) return self.data else: data = _json['data'] children = data['children'] nu = next_appender(data) for d in children: item_data = d['data'] kind = d['kind'] # Link: if kind == 't3': pairs = t3_kind(item_data, nu) # Subreddit: elif kind == 't5': pairs = t5_kind(item_data, nu) # Comment: elif kind == 't1': pairs = t1_kind(item_data, nu) elif kind == 'more': pairs = more_kind(data) else: pairs = {'title': 'Null', 'action': None} self.data.append(pairs) return self.data #### End of nested helper functions #### parsed = urlparse(link) query = dict(parse_qsl(parsed.query)) path = parsed.path if 'limit' not in query: query.update({'limit': control.setting('items.limit')}) query = urlencode(query) if not access_boolean() and not path.endswith('.json'): path += dotjson link = urlunparse(parsed._replace(path=path, query=query)) json_object = client.request(link, headers=request_headers()) loaded = json.loads(json_object) self.list = processor(loaded) return self.list
def my_account(self): choices = [ control.lang(30091), control.lang(30092), control.lang(30093) ] username = control.setting('username.string') icon_img = control.setting('avatar.url') if not username or not icon_img: ai = account_info() username, icon_img = (ai['name'], ai['icon_img']) parsed = urlparse(icon_img) query = 'fit=crop&crop=faces%2Centropy&arh=1.0&w=256&h=256&s=' icon_img = urlunparse( parsed._replace(query=query + icon_img.rpartition('s=')[2])) control.setSetting('username.string', username) control.setSetting('avatar.url', icon_img) if control.setting('window.action') == '0': choice = control.selectDialog(choices) if choice == 0: self.listing(reddit_url('/user/{0}/saved/'.format(username))) elif choice == 1: self.bookmarks(table='reddit') elif choice == 2: self.listing(reddit_url('/api/multi/mine/')) else: control.execute('Dialog.Close(all)') return else: self.list = [{ 'title': choices[0], 'action': 'listing', 'url': reddit_url('/user/{0}/saved/'.format(username)), 'image': icon_img }, { 'title': choices[1], 'action': 'subscribed_subreddits', 'image': icon_img }, { 'title': choices[2], 'action': 'listing', 'url': reddit_url('/api/multi/mine/'), 'image': icon_img }] if control.setting('user.icon') == 'false': for i in self.list: del i['image'] directory.add(self.list)