Skip to content

Commit

Permalink
add support for user_agent configuration
Browse files Browse the repository at this point in the history
fixes mlj#31
  • Loading branch information
jetmore committed Jan 22, 2023
1 parent da9727d commit de69096
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/castget.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ static int _process_channel(const gchar *channel_directory, GKeyFile *kf,

c = channel_new(channel_configuration->url, channel_file,
channel_configuration->spool_directory,
channel_configuration->filename_pattern, resume);
channel_configuration->filename_pattern, resume,
channel_configuration->user_agent);
g_free(channel_file);

if (!c) {
Expand Down
11 changes: 6 additions & 5 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void _enclosure_iterator(const void *user_data, int i,

channel *channel_new(const char *url, const char *channel_file,
const char *spool_directory, const char *filename_pattern,
int resume)
int resume, const char *user_agent)
{
channel *c;
xmlDocPtr doc;
Expand All @@ -70,6 +70,7 @@ channel *channel_new(const char *url, const char *channel_file,
c->channel_filename = g_strdup(channel_file);
c->spool_directory = g_strdup(spool_directory);
c->filename_pattern = g_strdup(filename_pattern);
c->user_agent = g_strdup(user_agent);
// c->resume = resume;
c->rss_last_fetched = NULL;
c->downloaded_enclosures =
Expand Down Expand Up @@ -126,7 +127,7 @@ static void _cast_channel_save_downloaded_enclosure(gpointer key,
g_free(escaped_key);
}

static int _cast_channel_save_channel(FILE *f, gpointer user_data, int debug)
static int _cast_channel_save_channel(FILE *f, gpointer user_data, int debug, channel *cc)
{
channel *c = (channel *)user_data;

Expand All @@ -149,7 +150,7 @@ static int _cast_channel_save_channel(FILE *f, gpointer user_data, int debug)
static void _cast_channel_save(channel *c, int debug)
{
write_by_temporary_file(c->channel_filename, _cast_channel_save_channel, c,
NULL, debug);
NULL, debug, c);
}

void channel_free(channel *c)
Expand Down Expand Up @@ -180,7 +181,7 @@ static rss_file *_get_rss(channel *c, void *user_data, channel_callback cb,

if (!strncmp("http://", c->url, strlen("http://")) ||
!strncmp("https://", c->url, strlen("https://")))
f = rss_open_url(c->url, debug);
f = rss_open_url(c->url, debug, c);
else
f = rss_open_file(c->url);

Expand Down Expand Up @@ -256,7 +257,7 @@ static int _do_download(channel *c, channel_info *channel_info, rss_item *item,
pb = NULL;

if (urlget_buffer(item->enclosure->url, enclosure_file, _enclosure_urlget_cb,
resume_from, debug, pb)) {
resume_from, debug, pb, c)) {
g_fprintf(stderr, "Error downloading enclosure from %s.\n",
item->enclosure->url);

Expand Down
3 changes: 2 additions & 1 deletion src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct _channel {
gchar *filename_pattern;
GHashTable *downloaded_enclosures;
gchar *rss_last_fetched;
gchar *user_agent;
} channel;

typedef struct _channel_info {
Expand All @@ -62,7 +63,7 @@ typedef void (*channel_callback)(void *user_data, channel_action action,

channel *channel_new(const char *url, const char *channel_file,
const char *spool_directory, const char *filename_pattern,
int resume);
int resume, const char *user_agent);
void channel_free(channel *c);
int channel_update(channel *c, void *user_data, channel_callback cb,
int no_download, int no_mark_read, int first_only,
Expand Down
7 changes: 6 additions & 1 deletion src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct channel_configuration *channel_configuration_new(
c->comment_tag =
_read_channel_configuration_key(kf, identifier, "comment_tag");
c->regex_filter = _read_channel_configuration_key(kf, identifier, "filter");
c->user_agent = _read_channel_configuration_key(kf, identifier, "user_agent");

/* Populate with defaults if necessary. */
if (defaults) {
Expand Down Expand Up @@ -143,6 +144,9 @@ struct channel_configuration *channel_configuration_new(

if (!c->regex_filter && defaults->regex_filter)
c->regex_filter = g_strdup(defaults->regex_filter);

if (!c->user_agent && defaults->user_agent)
c->user_agent = g_strdup(defaults->user_agent);
}

return c;
Expand Down Expand Up @@ -198,7 +202,8 @@ int channel_configuration_verify_keys(GKeyFile *kf, const char *identifier)
!strcmp(key_list[i], "genre_tag") ||
!strcmp(key_list[i], "year_tag") ||
!strcmp(key_list[i], "comment_tag") ||
!strcmp(key_list[i], "filter"))) {
!strcmp(key_list[i], "filter") ||
!strcmp(key_list[i], "user_agent"))) {
fprintf(stderr, "Invalid key %s in configuration of channel %s.\n",
key_list[i], identifier);
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct channel_configuration {
gchar *year_tag;
gchar *comment_tag;
gchar *regex_filter;
gchar *user_agent;
};

struct channel_configuration *channel_configuration_new(
Expand Down
9 changes: 5 additions & 4 deletions src/rss.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "channel.h"
#include "htmlent.h"
#include "libxmlutil.h"
#include "rss.h"
Expand Down Expand Up @@ -252,20 +253,20 @@ rss_file *rss_open_file(const char *filename)
return f;
}

static int _rss_open_url_cb(FILE *f, gpointer user_data, int debug)
static int _rss_open_url_cb(FILE *f, gpointer user_data, int debug, channel *c)
{
gchar *url = (gchar *)user_data;

return urlget_file(url, f, debug);
return urlget_file(url, f, debug, c);
}

rss_file *rss_open_url(const char *url, int debug)
rss_file *rss_open_url(const char *url, int debug, channel *c)
{
rss_file *f;
gchar *rss_filename;

if (write_by_temporary_file(NULL, _rss_open_url_cb, (gpointer)url,
&rss_filename, debug))
&rss_filename, debug, c))
return NULL;

f = rss_open_file(rss_filename);
Expand Down
2 changes: 1 addition & 1 deletion src/rss.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct _rss_file {
} rss_file;

rss_file *rss_open_file(const char *filename);
rss_file *rss_open_url(const char *url, int debug);
rss_file *rss_open_url(const char *url, int debug, channel *c);
void rss_close(rss_file *f);

#endif /* RSS_H */
14 changes: 9 additions & 5 deletions src/urlget.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
#include <stdlib.h>
#include <string.h>

int urlget_file(const char *url, FILE *f, int debug)
int urlget_file(const char *url, FILE *f, int debug, channel *c)
{
return urlget_buffer(url, (void *)f, NULL, 0, debug, NULL);
return urlget_buffer(url, (void *)f, NULL, 0, debug, NULL, c);
}

int urlget_buffer(const char *url, void *user_data,
size_t (*write_buffer)(void *buffer, size_t size,
size_t nmemb, void *user_data),
long resume_from, int debug, progress_bar *pb)
long resume_from, int debug, progress_bar *pb,
channel *c)
{
CURL *easyhandle;
CURLcode success;
Expand All @@ -46,8 +47,11 @@ int urlget_buffer(const char *url, void *user_data,
gchar *user_agent;

/* Construct user agent string. */
user_agent = g_strdup_printf("%s (%s rss enclosure downloader)",
PACKAGE_STRING, PACKAGE);
if (c->user_agent)
user_agent = g_strdup(c->user_agent);
else
user_agent = g_strdup_printf("%s (%s rss enclosure downloader)",
PACKAGE_STRING, PACKAGE);

/* Initialise curl. */
easyhandle = curl_easy_init();
Expand Down
6 changes: 4 additions & 2 deletions src/urlget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#define URLGET_H

#include "progress.h"
#include "channel.h"

int urlget_file(const char *url, FILE *f, int debug);
int urlget_file(const char *url, FILE *f, int debug, channel *c);
int urlget_buffer(const char *url, void *user_data,
size_t (*write_buffer)(void *buffer, size_t size,
size_t nmemb, void *user_data),
long resume_from, int debug, progress_bar *pb);
long resume_from, int debug, progress_bar *pb,
channel *c);

#endif /* URLGET_H */
6 changes: 3 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

int write_by_temporary_file(const gchar *filename,
int (*writer)(FILE *f, gpointer user_data,
int debug),
int debug, channel *c),
gpointer user_data, gchar **used_filename,
int debug)
int debug, channel *c)
{
int retval;
FILE *f;
Expand Down Expand Up @@ -72,7 +72,7 @@ int write_by_temporary_file(const gchar *filename,
return -1;
}

retval = writer(f, user_data, debug);
retval = writer(f, user_data, debug, c);

fclose(f);

Expand Down
6 changes: 4 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
#ifndef UTILS_H
#define UTILS_H

#include "channel.h"

#include <glib.h>
#include <stdio.h>

int write_by_temporary_file(const gchar *filename,
int (*writer)(FILE *f, gpointer user_data,
int debug),
int debug, channel *c),
gpointer user_data, gchar **used_filename,
int debug);
int debug, channel *c);
gchar *get_rfc822_time(void);

#endif /* UTILS_H */

0 comments on commit de69096

Please sign in to comment.