def add_pic(self, opened_file): """ Add picture to the Concept. Args: opened_file (file): opened file object """ # init meta if not self._meta: self._init_metadata() # get link to pic form data = download( url_context(self._meta["Přidej obrázek"]), session=self._session ) dom = dhtmlparser.parseString(data) # get information from pic form form = first(dom.find("form", {"enctype": "multipart/form-data"})) add_pic_url = form.params["action"] # send pic data = self._session.post( url_context(add_pic_url), data={ "action": "addScreenshot2", "finish": "Nahrát" }, files={"screenshot": opened_file} ) data = data.text.encode("utf-8") check_error_div(data, '<div class="error" id="screenshotError">')
def add_pic(self, opened_file): """ Add picture to the Concept. Args: opened_file (file): opened file object """ # init meta if not self._meta: self._init_metadata() # get link to pic form data = download(url_context(self._meta["Přidej obrázek"]), session=self._session) dom = dhtmlparser.parseString(data) # get information from pic form form = first(dom.find("form", {"enctype": "multipart/form-data"})) add_pic_url = form.params["action"] # send pic data = self._session.post(url_context(add_pic_url), data={ "action": "addScreenshot2", "finish": "Nahrát" }, files={"screenshot": opened_file}) data = data.text.encode("utf-8") check_error_div(data, '<div class="error" id="screenshotError">')
def edit(self, text, title=None, date_of_pub=None): """ Edit concept. Args: text (str): New text of the context. title (str, default None): New title of the concept. If not set, old title is used. date_of_pub (str/int, default None): Date string in abclinuxu format or timestamp determining when the concept should be automatically published. Note: `date_of_pub` can be string in format ``"%Y-%m-%d %H:%M"``. """ if not self._meta: self._init_metadata() data = download( url_context(self._meta["Uprav zápis"]), session=self._session ) dom = dhtmlparser.parseString(data) form = dom.find("form", {"name": "form"}) assert form, "Can't find edit form!" form = first(form) form_action = form.params["action"] if title is None: title = first(form.find("input", {"name": "title"})) title = title.params["value"] date = "" if date_of_pub is None: date = first(form.find("input", {"name": "publish"})) date = date.params["value"] elif isinstance(date_of_pub, basestring): date = date_of_pub else: date = ts_to_concept_date(date_of_pub) data = download( url=url_context(form_action), method="POST", data={ "cid": 0, "publish": date, "content": text, "title": title, "delay": "Ulož", "action": "edit2" }, session=self._session ) check_error_div(data, '<div class="error" id="contentError">') check_error_page(data)
def add_concept(self, text, title, ts_of_pub=None): """ Adds new concept into your concepts. Args: text (str): Text of your concept. title (str): Title of your contept. Do not use HTML in title! ts_of_pub (int/float, default None): Timestamp of the publication. Raises: UserWarning: if the site is broken or user was logged out. """ if not self.has_blog: raise ValueError("User doesn't have blog!") self.login() dom = dhtmlparser.parseString(self._get(self.blog_url)) # get section with links to new blog s_sekce = filter( lambda x: "Vlož nový zápis" in x.getContent(), dom.find("div", {"class": "s_sekce"}) ) if not s_sekce: raise UserWarning("Can't resolve right div tag!") # get link to "add blog" page add_blog_link = filter( lambda x: "href" in x.params and x.params["href"].endswith("action=add"), s_sekce[0].find("a") ) if not add_blog_link: raise UserWarning("Can't resolve user number!") add_blog_link = add_blog_link[0].params["href"] # get "add blog" page data = self._get(ABCLINUXU_URL + add_blog_link) dom = dhtmlparser.parseString(data) form_action = dom.find("form", {"name": "form"})[0].params["action"] data = self.session.post( ABCLINUXU_URL + form_action, data={ "cid": 0, "publish": shared.ts_to_concept_date(ts_of_pub), "content": text, "title": dhtmlparser.removeTags(title), "delay": "Do konceptů", "action": "add2" }, verify=False, ) data = data.text.encode("utf-8") check_error_div(data, '<div class="error" id="contentError">') check_error_div(data, '<div class="error" id="titleError">')
def edit(self, text, title=None, date_of_pub=None): """ Edit concept. Args: text (str): New text of the context. title (str, default None): New title of the concept. If not set, old title is used. date_of_pub (str/int, default None): Date string in abclinuxu format or timestamp determining when the concept should be automatically published. Note: `date_of_pub` can be string in format ``"%Y-%m-%d %H:%M"``. """ if not self._meta: self._init_metadata() data = download(url_context(self._meta["Uprav zápis"]), session=self._session) dom = dhtmlparser.parseString(data) form = dom.find("form", {"name": "form"}) assert form, "Can't find edit form!" form = first(form) form_action = form.params["action"] if title is None: title = first(form.find("input", {"name": "title"})) title = title.params["value"] date = "" if date_of_pub is None: date = first(form.find("input", {"name": "publish"})) date = date.params["value"] elif isinstance(date_of_pub, basestring): date = date_of_pub else: date = ts_to_concept_date(date_of_pub) data = download(url=url_context(form_action), method="POST", data={ "cid": 0, "publish": date, "content": text, "title": title, "delay": "Ulož", "action": "edit2" }, session=self._session) check_error_div(data, '<div class="error" id="contentError">') check_error_page(data)
def add_concept(self, text, title, ts_of_pub=None): """ Adds new concept into your concepts. Args: text (str): Text of your concept. title (str): Title of your contept. Do not use HTML in title! ts_of_pub (int/float, default None): Timestamp of the publication. Raises: UserWarning: if the site is broken or user was logged out. """ if not self.has_blog: raise ValueError("User doesn't have blog!") self.login() dom = dhtmlparser.parseString(self._get(self.blog_url)) # get section with links to new blog s_sekce = filter(lambda x: "Vlož nový zápis" in x.getContent(), dom.find("div", {"class": "s_sekce"})) if not s_sekce: raise UserWarning("Can't resolve right div tag!") # get link to "add blog" page add_blog_link = filter( lambda x: "href" in x.params and x.params["href"].endswith( "action=add"), s_sekce[0].find("a")) if not add_blog_link: raise UserWarning("Can't resolve user number!") add_blog_link = add_blog_link[0].params["href"] # get "add blog" page data = self._get(ABCLINUXU_URL + add_blog_link) dom = dhtmlparser.parseString(data) form_action = dom.find("form", {"name": "form"})[0].params["action"] data = self.session.post( ABCLINUXU_URL + form_action, data={ "cid": 0, "publish": shared.ts_to_concept_date(ts_of_pub), "content": text, "title": dhtmlparser.removeTags(title), "delay": "Do konceptů", "action": "add2" }, verify=False, ) data = data.text.encode("utf-8") check_error_div(data, '<div class="error" id="contentError">')