Created
September 11, 2018 09:41
-
-
Save takapiko/154d82f6f938a2ba09120c29a7118dac to your computer and use it in GitHub Desktop.
特定タグの付いたはてなブックマークを削除
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#-* coding:utf-8 -*- | |
import feedparser,sys,urllib | |
import json | |
import requests | |
from requests_oauthlib import OAuth1 | |
def get_username(auth): | |
response = requests.get("http://api.b.hatena.ne.jp/1/my", auth=auth) | |
return json.loads(response.text)["name"] | |
def delete_bookmark(auth,url): | |
response = requests.delete('http://api.b.hatena.ne.jp/1/my/bookmark',params={"url": url}, auth=auth) | |
if response.status_code == 204: | |
print("delete:"+url) | |
else: | |
print("failed:"+url) | |
if __name__ == "__main__": | |
consumer_key = "" | |
consumer_secret = "" | |
access_token = "" | |
access_secret = "" | |
auth = OAuth1( consumer_key, consumer_secret, access_token, access_secret ) | |
#消したいタグ(空白だと全部消えるので注意) | |
tag = "hogehoge" | |
username = get_username(auth) | |
feed_url = "http://b.hatena.ne.jp/"+username+"/atomfeed?tag="+urllib.parse.quote(tag) | |
while True: | |
feed = feedparser.parse(feed_url,request_headers={'Cache-control': 'max-age=0'}) | |
if(len(feed['entries'])==0): | |
break | |
for entry in feed['entries']: | |
url = entry.links[0].href | |
delete_bookmark(auth,url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment