本文整理汇总了Python中variety.Util.Util类的典型用法代码示例。如果您正苦于以下问题:Python Util类的具体用法?Python Util怎么用?Python Util使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Util类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _set_rating
def _set_rating(widget, rating=rating):
try:
Util.set_rating(file, rating)
main_window.on_rating_changed(file)
except Exception:
logger.exception(lambda: "Could not set EXIF rating")
main_window.show_notification(_("Could not set EXIF rating"))
开发者ID:GLolol,项目名称:variety-deb,代码行数:7,代码来源:ThumbsManager.py
示例2: fill_queue
def fill_queue(self):
page = random.randint(1, 250)
url = 'https://api.unsplash.com/photos/?page=%d&per_page=30&client_id=%s' % (page, UnsplashDownloader.CLIENT_ID)
logger.info(lambda: "Filling Unsplash queue from " + url)
r = Util.request(url)
if int(r.headers.get('X-Ratelimit-Remaining', 1000000)) < 100:
UnsplashDownloader.rate_limiting_started_time = time.time()
for item in r.json():
try:
width = item['width']
height = item['height']
if self.parent and not self.parent.size_ok(width, height):
continue
image_url = item['links']['download']
origin_url = item['links']['html']
filename = os.path.join(self.target_folder, Util.sanitize_filename(image_url.split('/')[-2] + '.jpg'))
extra_metadata = {
'sourceType': 'unsplash',
'sfwRating': 100,
'author': item['user']['name'],
'authorURL': item['user']['links']['html'],
'keywords': [cat['title'].lower().strip() for cat in item['categories']]
}
self.queue.append((origin_url, image_url, extra_metadata, filename))
except:
logger.exception(lambda: "Could not process an item from Unsplash")
raise
random.shuffle(self.queue)
logger.info(lambda: "Unsplash populated with %d URLs" % len(self.queue))
开发者ID:GLolol,项目名称:variety-deb,代码行数:35,代码来源:UnsplashDownloader.py
示例3: test_guess_image_url
def test_guess_image_url(self):
self.assertEquals('https://farm5.staticflickr.com/4032/4558166441_4e34855b39_o.jpg',
Util.guess_image_url({'sourceURL': 'https://www.flickr.com/photos/[email protected]/4558166441'}))
self.assertEquals('https://farm5.staticflickr.com/4077/4768189432_24275ea76b_b.jpg',
Util.guess_image_url({'sourceURL': 'http://www.flickr.com/photos/[email protected]/4768189432'}))
self.assertEquals('http://fc04.deviantart.net/fs71/i/2011/319/4/f/scarlet_leaf_wallpaper_by_venomxbaby-d4gc238.jpg',
Util.guess_image_url({'sourceURL': 'http://fc04.deviantart.net/fs71/i/2011/319/4/f/scarlet_leaf_wallpaper_by_venomxbaby-d4gc238.jpg'}))
开发者ID:GLolol,项目名称:variety-deb,代码行数:9,代码来源:TestUtil.py
示例4: save_locally
def save_locally(self, origin_url, image_url,
source_type=None, source_location=None, source_name=None,
force_download=False, extra_metadata={}, local_filename=None):
if not source_type:
source_type = self.source_type
if not source_name:
source_name = self.name
if not source_location:
source_location = self.location
if not force_download and self.parent and origin_url in self.parent.banned:
logger.info(lambda: "URL " + origin_url + " is banned, skip downloading")
return None
try:
os.makedirs(self.target_folder)
except Exception:
pass
if origin_url.startswith('//'):
origin_url = 'https:' + origin_url
if image_url.startswith('//'):
image_url = origin_url.split('//')[0] + image_url
if not local_filename:
local_filename = self.get_local_filename(image_url)
logger.info(lambda: "Origin URL: " + origin_url)
logger.info(lambda: "Image URL: " + image_url)
logger.info(lambda: "Local name: " + local_filename)
if not force_download and os.path.exists(local_filename):
logger.info(lambda: "File already exists, skip downloading")
return None
if self.parent and self.parent.options.safe_mode:
sfw_rating = Smart.get_sfw_rating(origin_url)
if sfw_rating is not None and sfw_rating < 100:
logger.info(lambda: "Skipping non-safe download %s. Is the source %s:%s "
"suitable for Safe mode?" % (origin_url, source_type, self.location))
return None
if self.parent and self.parent.options.safe_mode and 'keywords' in extra_metadata:
blacklisted = set(k.lower() for k in extra_metadata['keywords']) & Smart.get_safe_mode_keyword_blacklist()
if len(blacklisted) > 0:
logger.info(lambda: "Skipping non-safe download %s due to blacklisted keywords (%s). "
"Is the source %s:%s suitable for Safe mode?" %
(origin_url, str(blacklisted), source_type, self.location))
return None
try:
r = Util.request(image_url, stream=True)
with open(local_filename, 'wb') as f:
Util.request_write_to(r, f)
except Exception, e:
logger.info(lambda: "Download failed from image URL: %s (source location: %s) " % (image_url, self.location))
raise e
开发者ID:GLolol,项目名称:variety-deb,代码行数:57,代码来源:Downloader.py
示例5: test_is_dead_or_not_image
def test_is_dead_or_not_image(self):
self.assertTrue(Util.is_dead_or_not_image(None))
self.assertTrue(Util.is_dead_or_not_image('not a URL'))
self.assertTrue(Util.is_dead_or_not_image('http://www.google.com/'))
self.assertTrue(Util.is_dead_or_not_image('http://vrty.org/'))
self.assertTrue(Util.is_dead_or_not_image('http://www.google.com/dejkjdrelkjflkrejfjre'))
self.assertFalse(Util.is_dead_or_not_image('http://upload.wikimedia.org/wikipedia/commons/5/53/Wikipedia-logo-en-big.png'))
self.assertFalse(Util.is_dead_or_not_image('https://farm8.staticflickr.com/7133/7527967878_85fea93129_o.jpg'))
self.assertFalse(Util.is_dead_or_not_image('http://interfacelift.com/wallpaper/D98ef829/00899_rustedbolt_2560x1600.jpg'))
self.assertTrue(Util.is_dead_or_not_image('http://wallpapers.wallbase.cc/rozne/wallpaper-1227671.jpg'))
self.assertTrue(Util.is_dead_or_not_image('http://ns223506.ovh.net/rozne/a1b2/wallpaper-1996019.png'))
开发者ID:GLolol,项目名称:variety-deb,代码行数:11,代码来源:TestUtil.py
示例6: get_for_keyword
def get_for_keyword(self, keyword):
logger.info(lambda: "Fetching quotes from Goodreads for keyword=%s" % keyword)
url = iri2uri(u"https://www.goodreads.com/quotes/tag?utf8=\u2713&id=%s" % keyword)
soup = Util.html_soup(url)
page_links = list(Util.safe_map(int,
[pagelink.contents[0] for pagelink in
soup.find_all(href=re.compile('quotes/tag.*page='))]))
if page_links:
page = random.randint(1, max(page_links))
url = iri2uri(u"https://www.goodreads.com/quotes/tag?utf8=\u2713&id=%s&page=%d" % (keyword, page))
soup = Util.html_soup(url)
return self.get_from_soup(url, soup)
开发者ID:GLolol,项目名称:variety-deb,代码行数:14,代码来源:GoodreadsSource.py
示例7: check_quit
def check_quit():
global terminate
if not terminate:
GObject.timeout_add(1000, check_quit)
return
logging.getLogger("variety").info("Terminating signal received, quitting...")
safe_print(_("Terminating signal received, quitting..."),
"Terminating signal received, quitting...")
global VARIETY_WINDOW
if VARIETY_WINDOW:
GObject.idle_add(VARIETY_WINDOW.on_quit)
Util.start_force_exit_thread(10)
开发者ID:GLolol,项目名称:variety-deb,代码行数:14,代码来源:__init__.py
示例8: get_for_author
def get_for_author(self, author):
logger.info(lambda: "Fetching quotes from Goodreads for author=%s" % author)
url = iri2uri(u"https://www.goodreads.com/quotes/search?utf8=\u2713&q=%s" % author)
soup = Util.html_soup(url)
page_links = list(Util.safe_map(int,
[pagelink.contents[0] for pagelink in
soup.find_all(href=re.compile('quotes/search.*page='))]))
if page_links:
page = random.randint(1, max(page_links))
url = iri2uri(u"https://www.goodreads.com/quotes/search?utf8=\u2713&q=%s&page=%d" % (author, page))
soup = Util.html_soup(url)
return self.get_from_soup(url, soup)
开发者ID:GLolol,项目名称:variety-deb,代码行数:14,代码来源:GoodreadsSource.py
示例9: fill_queue
def fill_queue(self):
logger.info(lambda: "Filling Bing queue from " + self.location)
s = Util.fetch_json(BingDownloader.BING_JSON_URL)
for item in s['images']:
try:
image_url = 'https://www.bing.com' + item['url']
filename = item['url'].split('/')[-1]
name = filename[0:filename.find('_EN')]
src_url = 'https://www.bing.com/gallery/#images/%s' % name
try:
date = datetime.strptime(item['startdate'], '%Y%m%d').strftime('%Y-%m-%d')
except:
date = item['startdate']
extra_metadata = {
'sourceType': 'bing',
'sfwRating': 100,
'headline': 'Bing Photo of the Day, %s' % date,
'description': item['copyright'],
}
self.queue.append((src_url, image_url, extra_metadata))
except:
logger.exception(lambda: "Could not process an item in the Bing json result")
random.shuffle(self.queue)
logger.info(lambda: "Bing queue populated with %d URLs" % len(self.queue))
开发者ID:GLolol,项目名称:variety-deb,代码行数:26,代码来源:BingDownloader.py
示例10: ajax
def ajax(self, url, data, error_msg_handler):
try:
return Util.fetch_json(url, data)
except requests.exceptions.HTTPError, e:
logger.exception(lambda: 'HTTPError for ' + url)
error_msg_handler(_('Oops, server returned error (%s)') % e.response.status_code)
raise
开发者ID:GLolol,项目名称:variety-deb,代码行数:7,代码来源:LoginOrRegisterDialog.py
示例11: fill_queue
def fill_queue(self):
logger.info(lambda: "Reddit URL: " + self.location)
json_url = RedditDownloader.build_json_url(self.location)
s = Util.fetch_json(json_url)
for item in s['data']['children']:
try:
data = item['data']
image_url = data['url']
if re.match(r'^http(s)?://imgur\.com/\w+$', image_url):
image_url = image_url.replace('://', '://i.') + '.jpg'
if image_url.lower().endswith(('.jpg', '.jpeg', '.png')):
src_url = 'https://www.reddit.com' + data['permalink']
extra_metadata = {'sourceType': 'reddit'}
if data['over_18']:
extra_metadata['sfwRating'] = 0
if self.parent and self.parent.options.safe_mode:
continue
self.queue.append((src_url, image_url, extra_metadata))
except Exception:
logger.exception(lambda: "Could not process an item in the Reddit json result")
random.shuffle(self.queue)
logger.info(lambda: "Reddit queue populated with %d URLs" % len(self.queue))
开发者ID:GLolol,项目名称:variety-deb,代码行数:25,代码来源:RedditDownloader.py
示例12: get_image_url
def get_image_url(origin_url):
photo_id = FlickrDownloader.get_photo_id(origin_url)
call = 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=%s&photo_id=%s&format=json&nojsoncallback=1' % \
(API_KEY, photo_id)
resp = Util.fetch_json(call)
s = max(resp['sizes']['size'], key=lambda size: int(size['width']))
return s['source']
开发者ID:GLolol,项目名称:variety-deb,代码行数:7,代码来源:FlickrDownloader.py
示例13: set_icon
def set_icon(self, icon):
def set_from_theme_icon(name):
if self.indicator:
logger.info(lambda: "Showing indicator icon %s from GTK theme" % name)
self.indicator.set_icon(name)
if self.status_icon:
logger.info(lambda: "Showing status icon %s from GTK theme" % name)
self.status_icon.set_from_icon_name(name)
if icon == "Light":
if Gtk.IconTheme.get_default().has_icon(THEME_ICON_NAME):
set_from_theme_icon(THEME_ICON_NAME)
return
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator.png")
elif icon == "Dark":
if Gtk.IconTheme.get_default().has_icon(THEME_ICON_NAME_DARK):
set_from_theme_icon(THEME_ICON_NAME_DARK)
return
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator-dark.png")
elif icon and os.access(icon, os.R_OK) and Util.is_image(icon):
icon_path = icon
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator.png")
if self.indicator:
logger.info(lambda: "Showing indicator icon image: " + icon_path)
self.indicator.set_icon(icon_path)
if self.status_icon:
logger.info(lambda: "Showing status icon image: " + icon_path)
self.status_icon.set_from_file(icon_path)
开发者ID:GLolol,项目名称:variety-deb,代码行数:32,代码来源:indicator.py
示例14: populate_sfw_menu
def populate_sfw_menu(self):
try:
self.rating_items = []
sfw_ratings = Smart.get_all_sfw_ratings()
def _gui_update(sfw_ratings):
try:
def _add_menuitem(rating):
menuitem = Gtk.ImageMenuItem(_(rating['label_long']))
menuitem.set_visible(True)
def _rate(*args, **kwargs):
self.parent.report_sfw_rating(file=None, rating=rating['rating'])
menuitem.connect("activate", _rate)
try:
menuitem.set_always_show_image(True)
image = Gtk.Image()
image.set_from_file(varietyconfig.get_data_file("media", "sfw-%s.svg" % rating['rating']))
menuitem.set_image(image)
except:
logger.exception(lambda: "Could not set image to NSFW menuitem:")
self.sfw_menu.append(menuitem)
self.rating_items.append(menuitem)
map(_add_menuitem, reversed(sfw_ratings))
separator = Gtk.SeparatorMenuItem.new()
separator.set_visible(True)
self.sfw_menu.append(separator)
self.safe_mode = Gtk.CheckMenuItem(_("_Safe mode"))
self.safe_mode.set_visible(True)
self.safe_mode.set_active(self.parent.options.safe_mode)
self.safe_mode.set_use_underline(True)
self.safe_mode_handler_id = self.safe_mode.connect("toggled", self.parent.on_safe_mode_toggled)
self.sfw_menu.append(self.safe_mode)
self.sfw_menu_item.set_sensitive(True)
self.parent.update_indicator()
except Exception:
logger.exception(lambda: 'Oops, could not populate NSFW menu:')
Util.add_mainloop_task(_gui_update, sfw_ratings)
except Exception:
logger.exception(lambda: 'Oops, could not populate NSFW menu:')
开发者ID:GLolol,项目名称:variety-deb,代码行数:47,代码来源:indicator.py
示例15: main
def main():
if datetime.datetime.now() - datetime.datetime.strptime(REL_DATE, "%Y-%m-%d") > datetime.timedelta(
days=180):
print VarietyWindow.OUTDATED_MSG
return
# Ctrl-C
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigint_handler)
signal.signal(signal.SIGQUIT, sigint_handler)
Util.makedirs(os.path.expanduser(u"~/.config/variety/"))
arguments = map(_u, sys.argv[1:])
# validate arguments and set up logging
options, args = VarietyWindow.VarietyWindow.parse_options(arguments)
set_up_logging(options.verbose)
monkeypatch_ssl()
if options.verbose > 2:
Util.log_all(VarietyWindow.VarietyWindow)
if options.verbose > 3:
Util.log_all(ThumbsManager.ThumbsManager)
Util.log_all(ThumbsWindow.ThumbsWindow)
bus = dbus.SessionBus()
# ensure singleton
if bus.request_name(DBUS_KEY) != dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
if not arguments:
arguments = ["--preferences"]
safe_print(_("Variety is already running. Sending the command to the running instance."),
"Variety is already running. Sending the command to the running instance.")
method = bus.get_object(DBUS_KEY, DBUS_PATH).get_dbus_method("process_command")
result = method(arguments)
if result:
safe_print(result)
return
# Run the application.
window = VarietyWindow.VarietyWindow()
global VARIETY_WINDOW
VARIETY_WINDOW = window
service = VarietyService(window)
bus.call_on_disconnection(window.on_quit)
window.start(arguments)
GObject.timeout_add(2000, check_quit)
GObject.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
开发者ID:GLolol,项目名称:variety-deb,代码行数:56,代码来源:__init__.py
示例16: get_for_search_url
def get_for_search_url(self, url):
logger.info(lambda: "Fetching quotes from Goodreads for search url=%s" % url)
html = Util.fetch(url)
try:
page = random.randint(1, int(re.findall('Page 1 of (\d+)', html)[0]))
url += "&page=%d" % page
html = Util.fetch(url)
except Exception:
pass # probably just one page
logger.info(lambda: "Used QuotationsPage url %s" % url)
r = r'.*<dl>(.*)</dl>.*'
if re.match(r, html, flags=re.M | re.S):
html = re.sub(r, '<html><body>\\1</body></html>', html, flags=re.M | re.S)
# without this BeautifulSoup gets confused by some scripts
return self.get_from_html(url, html)
开发者ID:GLolol,项目名称:variety-deb,代码行数:18,代码来源:QuotationsPageSource.py
示例17: get_sanitized_config
def get_sanitized_config(parent):
options = parent.options
config = dict(options.__dict__)
del config['configfile']
# remove all references to local folders and files:
config['favorites_folder'] = \
'Default' if options.favorites_folder == os.path.expanduser(u"~/.config/variety/Favorites") else 'Changed'
config['download_folder'] = \
'Default' if options.download_folder == os.path.expanduser(u"~/.config/variety/Downloaded") else 'Changed'
config['fetched_folder'] = \
'Default' if options.fetched_folder == os.path.expanduser(u"~/.config/variety/Fetched") else 'Changed'
config['copyto_folder'] = \
'Default' if options.copyto_folder == 'Default' else 'Changed'
config['quotes_favorites_file'] = \
'Default' if options.quotes_favorites_file == os.path.expanduser(u"~/.config/variety/favorite_quotes.txt") else 'Changed'
config['slideshow_custom_folder'] = \
'Default' if options.slideshow_custom_folder == Util.get_xdg_pictures_folder() else 'Changed'
config['sources'] = [s for s in options.sources if s[1] not in
(Options.SourceType.FOLDER, Options.SourceType.IMAGE)]
config['favorites_operations'] = [
("custom_folder" if place not in ["/", "Downloaded", "Fetched", "Others"] else place, op)
for (place, op) in options.favorites_operations]
# add some general OS and desktop environment information
config["platform"] = str(platform.platform())
distro = platform.linux_distribution()
config["linux_distribution"] = distro
config["linux_distribution_distname"] = str(distro[0])
config["linux_distribution_version"] = str(distro[1])
config["linux_distribution_id"] = str(distro[2])
config["desktop_session"] = str(os.getenv('DESKTOP_SESSION'))
# add screen info - resolution, monitor count, etc.
config["total_screen_width"] = Gdk.Screen.get_default().get_width()
config["total_screen_height"] = Gdk.Screen.get_default().get_height()
config["monitor_count"] = Gdk.Screen.get_default().get_n_monitors()
try:
rect = Gdk.Screen.get_default().get_monitor_geometry(Gdk.Screen.get_default().get_primary_monitor())
config['primary_monitor_width'] = rect.width
config['primary_monitor_height'] = rect.height
except:
pass
# add some other Variety-specifics things:
config["variety_version"] = varietyconfig.get_version()
config["image_count"] = parent.image_count
with open(os.path.join(parent.config_folder, ".firstrun"), 'r') as f:
config["first_run_timestamp"] = f.read()
# add a timestamp
config["report_timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return config
开发者ID:GLolol,项目名称:variety-deb,代码行数:57,代码来源:Stats.py
示例18: test_compare_versions
def test_compare_versions(self):
self.assertEquals(-1, Util.compare_versions("0.4.10", "0.4.11"))
self.assertEquals(-1, Util.compare_versions("0.4.10", "0.5"))
self.assertEquals(-1, Util.compare_versions("0.4.10", "1"))
self.assertEquals(0, Util.compare_versions("0.4.10", "0.4.10"))
self.assertEquals(1, Util.compare_versions("0.4.10", "0.4.8"))
self.assertEquals(1, Util.compare_versions("0.4.10", "0.4"))
self.assertEquals(1, Util.compare_versions("0.4.10", "0"))
开发者ID:GLolol,项目名称:variety-deb,代码行数:8,代码来源:TestUtil.py
示例19: download_one
def download_one(self):
min_download_interval, min_fill_queue_interval = self.parse_server_options("wallhaven", 0, 0)
if time.time() - WallhavenDownloader.last_download_time < min_download_interval:
logger.info(
lambda: "Minimal interval between Wallhaven downloads is %d, skip this attempt" % min_download_interval
)
return None
logger.info(lambda: "Downloading an image from Wallhaven.cc, " + self.location)
logger.info(lambda: "Queue size: %d" % len(self.queue))
if not self.queue:
if time.time() - self.last_fill_time < min_fill_queue_interval:
logger.info(
lambda: "Wallhaven queue empty, but minimal interval between fill attempts is %d, "
"will try again later" % min_fill_queue_interval
)
return None
self.fill_queue()
if not self.queue:
logger.info(lambda: "Wallhaven queue still empty after fill request")
return None
WallhavenDownloader.last_download_time = time.time()
wallpaper_url = self.queue.pop()
logger.info(lambda: "Wallpaper URL: " + wallpaper_url)
s = Util.html_soup(wallpaper_url)
src_url = s.find("img", id="wallpaper")["src"]
logger.info(lambda: "Image src URL: " + src_url)
extra_metadata = {}
try:
extra_metadata["keywords"] = [el.text.strip() for el in s.find_all("a", {"class": "tagname"})]
except:
pass
try:
purity = s.find("div", "sidebar-content").find("label", "purity").text.lower()
sfw_rating = {"sfw": 100, "sketchy": 50, "nsfw": 0}[purity]
extra_metadata["sfwRating"] = sfw_rating
if self.parent and self.parent.options.safe_mode and sfw_rating < 100:
logger.info(
lambda: "Skipping non-safe download from Wallhaven. "
"Is the source %s suitable for Safe mode?" % self.location
)
return None
except:
pass
return self.save_locally(wallpaper_url, src_url, extra_metadata=extra_metadata)
开发者ID:GLolol,项目名称:variety-deb,代码行数:56,代码来源:WallhavenDownloader.py
示例20: is_valid_content
def is_valid_content(x):
return (
x is not None
and "url" in x.attrib
and (
Util.is_image(x.attrib["url"])
or ("medium" in x.attrib and x.attrib["medium"].lower() == "image")
or ("type" in x.attrib and x.attrib["type"].lower().startswith("image/"))
)
)
开发者ID:GLolol,项目名称:variety-deb,代码行数:10,代码来源:MediaRssDownloader.py
注:本文中的variety.Util.Util类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论