Permalink
Browse files

Merge pull request #21 from skn/master

pinboard bookmark script, guess title from url for bookmarking purposes
  • Loading branch information...
2 parents b25426b + ee604ab commit d8b4233cfa7cc9b96d2ab1c740997fe340c10117 @akrennmair committed Mar 26, 2012
Showing with 62 additions and 1 deletion.
  1. +36 −0 contrib/pinboard.pl
  2. +1 −0 include/formaction.h
  3. +25 −1 src/formaction.cpp
View
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use strict;
+use Net::Delicious;
+use LWP::UserAgent;
+
+my $url='';
+my $tag='newsbeuter';
+
+
+#Get redirected URL's permalink
+my $ua = LWP::UserAgent->new(
+ requests_redirectable => [],
+);
+my $res = $ua->get($ARGV[0]);
+if ($res->status_line == 301)
+ {$url = $res->header( 'location');}
+else
+ {$url = $ARGV[0];}
+
+my $pin = Net::Delicious->new({
+ user => "change",
+ pswd => "changeme",
+ endpoint => "https://api.pinboard.in/v1/",
+});
+
+my $result = $pin->add_post({
+ url => $url,
+ description => $ARGV[1],
+ tags => $tag,
+});
+
+if (!$result) {
+ # Must be an error. But who cares!
+}
+
View
@@ -79,6 +79,7 @@ class formaction {
private:
std::string prepare_keymap_hint(keymap_hint_entry * hints);
void start_next_question();
+ std::string make_title(const std::string& url);
std::vector<qna_pair> qna_prompts;
operation finish_operation;
View
@@ -5,6 +5,7 @@
#include <logger.h>
#include <cassert>
#include <exceptions.h>
+#include <algorithm>
namespace newsbeuter {
@@ -300,13 +301,36 @@ void formaction::start_bookmark_qna(const std::string& default_title, const std:
LOG(LOG_DEBUG, "formaction::start_bookmark_qna: starting bookmark Q&A... default_title = %s default_url = %s default_desc = %s", default_title.c_str(), default_url.c_str(), default_desc.c_str());
std::vector<qna_pair> prompts;
+ std::string new_title = "";
prompts.push_back(qna_pair(_("URL: "), default_url));
- prompts.push_back(qna_pair(_("Title: "), default_title));
+ if(default_title.empty()) { // call the function to figure out title from url only if the default_title is no good
+ new_title = make_title(default_url);
+ prompts.push_back(qna_pair(_("Title: "), new_title));
+ }
+ else {
+ prompts.push_back(qna_pair(_("Title: "), default_title));
+ }
prompts.push_back(qna_pair(_("Description: "), default_desc));
start_qna(prompts, OP_INT_BM_END);
}
+std::string formaction::make_title(const std::string& const_url) {
+ /* Sometimes it is possible to construct the title from the URL
+ * This attempts to do just that. eg: http://domain.com/story/yy/mm/dd/title-with-dashes?a=b
+ */
+ std::string url = (std::string&) const_url;
+ std::string::size_type pos_of_slash = url.find_last_of('/', (int)url.length()-2); // get to the final part of the URI's path, catering for situation where last char is '/'
+ if (url[url.length()-1] == '/')
+ url.erase((int)url.length()-1);
+ std::string path=url.substr(pos_of_slash+1); // extract just the juicy part 'title-with-dashes?a=b'
+ std::string::size_type pos_of_qmrk = path.find_first_of('?'); // find where query part of URI starts
+ std::string title = path.substr(0,pos_of_qmrk); //throw away the query part 'title-with-dashes'
+ std::replace(title.begin(), title.end(), '-', ' '); // 'title with dashes'
+ if (title.at(0)>= 'a' && title.at(0)<= 'z') title[0] -= 'a' - 'A'; //'Title with dashes'
+ return title;
+}
+
void formaction::start_next_question() {
/*
* If there is one more prompt to be presented to the user, set it up.

0 comments on commit d8b4233

Please sign in to comment.