1 /* gmpc-guitartabs (GMPC plugin)
2 * Copyright (C) 2009 Syed Mushtaq Ahmed <syed1_ahmed@yahoo.co.in>
3 * Project homepage: http://gmpc.wikia.com/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <libmpd/debug_printf.h>
30 #include <gmpc/plugin.h>
31 #include <gmpc/gmpc_easy_download.h>
32 #include <gmpc/metadata.h>
34 #define BASE_URL "http://www.ultimate-guitar.com"
37 typedef struct Query
{
39 void (*callback
)(GList
*, gpointer
);
44 static int guitartabs_get_enabled()
46 return cfg_get_single_value_as_int_with_default(config
, "guitartabs", "enable", TRUE
);
48 static void guitartabs_set_enabled(int enabled
)
50 cfg_set_single_value_as_int(config
, "guitartabs", "enable", enabled
);
53 static int guitartabs_fetch_priority(void){
54 return cfg_get_single_value_as_int_with_default(config
, "guitartabs", "priority", 80);
56 static void guitartabs_fetch_priority_set(int priority
){
57 cfg_set_single_value_as_int(config
, "guitartabs", "priority", priority
);
60 static void tab_download_callback(const GEADAsyncHandler
*handle
, GEADStatus status
, gpointer data
)
62 Query
*q
= (Query
*)data
;
63 if(status
== GEAD_PROGRESS
) return;
64 if(status
== GEAD_DONE
)
68 const char *data
= gmpc_easy_handler_get_data(handle
, &size
);
71 GRegex
*re
= g_regex_new("<pre>(.*)</pre>",G_REGEX_CASELESS
|G_REGEX_DOTALL
, 0, &err
);
74 g_regex_match_full(re
,data
,strlen(data
),0,0,&info
,&err
);
75 if(g_match_info_matches(info
))
78 gchar
*tab
= g_match_info_fetch(info
,1);
79 gchar
*tab_utf8
= g_convert(tab
,strlen(tab
),"utf8","ISO-8859-1",NULL
,NULL
,&err
);
82 MetaData
*mtd
= meta_data_new();
83 mtd
->type
= META_SONG_GUITAR_TAB
;
84 mtd
->plugin_name
= plugin
.name
;
85 mtd
->content_type
= META_DATA_CONTENT_TEXT
;
86 mtd
->content
= tab_utf8
;
87 list
= g_list_append(list
, mtd
);
88 printf("%s\n", mtd
->content
);
90 q
->callback(list
,q
->user_data
);
94 printf("could not convert \n");
102 printf("did not match\n");
104 q
->callback(NULL
,q
->user_data
);
109 printf("error message: %s\n", err
->message
);
115 if(status
== GEAD_FAILED
|| status
== GEAD_CANCELLED
)
116 q
->callback(NULL
,q
->user_data
);
120 static void query_callback(const GEADAsyncHandler
*handle
, GEADStatus status
, gpointer data
)
122 Query
*q
= (Query
*)data
;
123 if(status
== GEAD_PROGRESS
) return;
124 if(status
== GEAD_DONE
)
127 const char *data
= gmpc_easy_handler_get_data(handle
, &size
);
130 GRegex
*re
= g_regex_new("<a[\\s+]href=\"(/tabs/[\\w-]+/[\\w/.]+)\" class=\"song\">(.+)</a>",G_REGEX_CASELESS
, 0, &err
);
133 g_regex_match_full(re
,data
,strlen(data
),0,0,&info
,NULL
);
134 if (g_match_info_matches(info
))
136 gchar
*link
= g_strdup(g_match_info_fetch(info
,1));
137 gchar
*tab_url
= g_strdup_printf("%s%s",BASE_URL
,link
);
138 gmpc_easy_async_downloader(tab_url
,tab_download_callback
,q
);
144 printf("did not match\n");
146 q
->callback(NULL
,q
->user_data
);
151 printf("error in regular expression\n");
157 if(status
== GEAD_FAILED
|| status
== GEAD_CANCELLED
)
158 q
->callback(NULL
,q
->user_data
);
166 static void guitartabs_fetch(mpd_Song
*song
, MetaDataType type
, void (*callback
)(GList
*list
, gpointer data
), gpointer user_data
)
169 if(song
->artist
!= NULL
&&song
->title
!= NULL
&& type
== META_SONG_GUITAR_TAB
&&
170 cfg_get_single_value_as_int_with_default(config
, "guitartabs", "enable" , TRUE
))
172 Query
*q
= g_malloc0(sizeof(*q
));
174 GRegex
*re
= g_regex_new( "^The +" , G_REGEX_CASELESS
, 0, &err
);
178 gchar
*artist
= gmpc_easy_download_uri_escape(song
->artist
);
179 gchar
*title
= gmpc_easy_download_uri_escape(song
->title
);
180 g_regex_replace(re
,artist
,strlen(artist
),0,"",0,&err
);
182 q
->callback
= callback
;
183 q
->user_data
= user_data
;
185 gchar
*query_url
= g_strdup_printf("%s/search.php?bn=%s&sn=%s&sort_fld=rate&updown=down&f=tab",BASE_URL
,artist
,title
);
189 gmpc_easy_async_downloader(query_url
,query_callback
,q
);
194 printf("error in regular expression");
200 callback(NULL
, user_data
);
207 static void guitartabs_init()
212 gmpcMetaDataPlugin gt_meta
= {
213 .get_priority
= guitartabs_fetch_priority
,
214 .set_priority
= guitartabs_fetch_priority_set
,
215 .get_metadata
= guitartabs_fetch
218 int plugin_api_version
= PLUGIN_API_VERSION
;
220 gmpcPlugin plugin
= {
221 .name
= "Guitar Tabs fetcher",
222 .version
= {PLUGIN_MAJOR_VERSION
,PLUGIN_MINOR_VERSION
,PLUGIN_MICRO_VERSION
},
223 .plugin_type
= GMPC_PLUGIN_META_DATA
,
224 .init
= guitartabs_init
,
225 .metadata
= >_meta
,
226 .get_enabled
= guitartabs_get_enabled
,
227 .set_enabled
= guitartabs_set_enabled