Github - How to search a gist via API? - github

I understand that it is possible to search a repository via various criteria with the Github API but how to search for a single gist or find its id?
Ideally I'd like to use user and filename but it could be a hash inside the content body:
https://gist.github.com/search?q=sadjghasghdhjasgdfhs

I just found out that it is possible to search for a specific user and/or filename. I've looked at the advanced search of github, which uses user:some-user-name and path:some-file-name. These criteria also work on the gist search terms.
example: https://gist.github.com/search?q=user%3Amanuelvanrijn+path%3Aguid.js
Hope this helps until there's a search api available for gists

The GitHub community post given in as comment as the top answer no longer works. I pulled the Ruby example from archive.org, here it is:
module GistSearch
class << self
def search_public_gists(query:, page_limit: 10)
docs = []
base_url = "https://gist.github.com"
next_path = "/search?q=#{query}"
page_limit.times do
url = "#{base_url}#{next_path}"
html = fetch_cached(url)
doc = Nokogiri(html)
docs << doc
if !doc.search("a[rel=next]").empty?
next_path = doc.search("a[rel=next]").attr("href").value
else
break
end
end
docs
end
def build_items(queries:)
queries.flat_map do |query|
search_public_gists(query: query).flat_map do |doc|
doc.
search(".gist-snippet").
map do |gist_snippet|
Item.new(
creator: gist_snippet.search(".creator a:first").text,
title: gist_snippet.search(".creator a").map(&:text).join(" / ") + " (Matched query: #{query.inspect})",
link: gist_snippet.search(".link-overlay").attr("href").value,
guid: gist_snippet.search(".link-overlay").attr("href").value,
pub_date: Time.iso8601(gist_snippet.search("time-ago").attr("datetime").value),
content: gist_snippet.search(".js-file-line").map(&:text).join("\n"),
)
end
end
end
end

Related

Email Pdf attachments to Google sheet

I am looking for something that allows me from a mail PDF attachment to get a data in a google sheet.
We all often get PDF attachments in our email and it will be great if we get the entire data in a google sheet.
DO let me know if there is anything like this
Explanation:
Your question is very broad and it is impossible to give a specific answer because that answer would depend on the pdf but also on the data you want to fetch from that, besides all the other details you skipped to mention.
Here I will provide a general code snippet which you can use to get the pdf from the gmail attachments and then convert it to text (string). For this text you can use some regular expressions (which have to be very specific on your use case) to get the desired information and then put it in your sheet.
Code snippet:
The main code will this one. You should only modify this code:
function myFunction() {
const queryString = "label:unread from example#gmail.com" // use your email query
const threads = GmailApp.search(queryString);
const threadsMessages = GmailApp.getMessagesForThreads(threads);
const ss = SpreadsheetApp.getActive();
for (thr = 0, thrs = threads.length; thr < thrs; ++thr) {
let messages = threads[thr].getMessages();
for (msg = 0, msgs = messages.length; msg < msgs; ++msg) {
let attachments = messages[msg].getAttachments();
for (att = 0, atts = attachments.length; att < atts; ++att) {
let attachment_Name = attachments[att].getName();
let filetext = pdfToText( attachments[att], {keepTextfile: false} );
Logger.log(filetext)
// do something with filetext
// build some regular expression that fetches the desired data from filetext
// put this data to the sheet
}}}
}
and pdfToText is a function implemented by Mogsdad which you can find here. Just copy paste the code snippet provided in that link together with myFunction I posted in this answer. Also you have some options which you can use that are very well explained in the link I provided. Important thing to note, to use this library you need to enable the Drive API from the resources.
This will get you started and if you face any issues down the road which you can't find the solution for, you should create a new post here with the specific details of the problem.

How to get images in the product list view with an API call in Shopware?

I’m working on a single-page-application with fully client-side rendered frontend (React) and Shopware acting as a headless CMS in the background. So all product-data will be pulled from the API and checkout will also use the API to send the data.
My problem is the following:
When trying to render the product list page, I call the articles endpoint which returns the basic info of all my products. The problem is that I would also need to render the main image associated with each product and this list does not expose any data from the media attached to the products.
I can get the media item(s) for a product using the Media endpoint, problem is that this one expects a Media Id which I cannot get from the listed representations of the articles.
So right now the only way I see to get the images is first making a call that gets all the products, then loop through them and get each product’s details, calling the article endpoint again with each product ID, then when I have the media IDs I loop through those and get the images for each product using the media endpoint, because that’s the only one that exposes the actual image path in the response. This seems way too complicated and slow.
Is there a smarter way to do this? Can I get Shopware to output the 1st image’s path in the Article list response, that’s associated with the current product?
Also I saw that the paths to the images look like this:
/media/image/f5/fb/95/03_200x200.jpg
The first part up to /media/image/ is fixed, that’s straight forward, and I get the image’s filename and extension in the article detail’s response in the media object and even the file extension, which looks like this in the API's response.
The problem is that I don’t know what’s the f5/fb/95/ stand for. If I could get this info from the details I could assemble the URL for the image programmatically and then I wouldn’t need the call to the media endpoint.
You can see how the path is generated in the class \Shopware\Bundle\MediaBundle\Strategy\Md5Strategy of Shopware's source code. The two relevant methods are:
<?php
public function normalize($path)
{
// remove filesystem directories
$path = str_replace('//', '/', $path);
// remove everything before /media/...
preg_match("/.*((media\/(?:archive|image|music|pdf|temp|unknown|video|vector)(?:\/thumbnail)?).*\/((.+)\.(.+)))/", $path, $matches);
if (!empty($matches)) {
return $matches[2] . '/' . $matches[3];
}
return $path;
}
public function encode($path)
{
if (!$path || $this->isEncoded($path)) {
return $this->substringPath($path);
}
$path = $this->normalize($path);
$path = ltrim($path, '/');
$pathElements = explode('/', $path);
$pathInfo = pathinfo($path);
$md5hash = md5($path);
if (empty($pathInfo['extension'])) {
return '';
}
$realPath = array_slice(str_split($md5hash, 2), 0, 3);
$realPath = $pathElements[0] . '/' . $pathElements[1] . '/' . join('/', $realPath) . '/' . $pathInfo['basename'];
if (!$this->hasBlacklistParts($realPath)) {
return $realPath;
}
foreach ($this->blacklist as $key => $value) {
$realPath = str_replace($key, $value, $realPath);
}
return $realPath;
}
Step for step, this happens generally to an image path, for example https://example.com/media/image/my_image.jpg, upon passing it to the encode-method:
The normalize method strips everything except for media/image/my_image.jpg
An md5-hash is generated out of the resulting string: 5dc18cdfa0...
The resulting string from step 1 is split at every /: ['media', 'image', 'my_image.jpg']
The first three character pairs from the md5-hash are being stored into an array: ['5d', 'c1', '8c']
The final path is being assembled out of the arrays from steps 3 and 4: media/image/5d/c1/8c/my_image.jpg
Every occurence of /ad/ is being replaced by /g0/. This is done, because there are ad-blockers which block requests to URLs containing /ad/
Shopware computes the path for you.
There is acutally no need to know about the hashed path...
You will be redirected to the correct path.
Try the following:
/media/image/ + image['path'] + image['extension']
An for the thumbnails:
/media/image/thumbnail/ + image['path'] + '_200x200' + image['extension']
or
/media/image/thumbnail/ + image['path'] + '_600x600' + image['extension']

How to overlay a cq widget so it is also available in SiteAdmin

The CQ.tagging.TagInputField provided two configuration parameter which won't work in combination:
tagsBasePath
namespaces
Using the OOTB facebook tags as example, I want to restric the dialog to only display the Favorite Teams. The Structure is this:
So I set tagBasePath to /etc/tags/facebook and namespaces to [favorite_teams]. This does what it is supposed to do and only shows the two teams in the dialog. But when you click on it, a JavaScript exceptions is thrown. The problem lies in the following method defined in /libs/cq/tagging/widgets/source/CQ.tagging.js
CQ.tagging.parseTag = function(tag, isPath) {
var tagInfo = {
namespace: null,
local: tag,
getTagID: function() {
return this.namespace + ":" + this.local;
}
};
// parse tag pattern: namespace:local
var colonPos = tag.indexOf(isPath ? '/' : ':');
if (colonPos > 0) {
// the first colon ":" delimits a namespace
// don't forget to trim the strings (in case of title paths)
tagInfo.namespace = tag.substring(0, colonPos).trim();
tagInfo.local = tag.substring(colonPos + 1).trim();
}
return tagInfo;
};
It does not respect the configurations set on the widget and returns a tagInfo where the namespace is null. I then overlayed the method in my authoring JavaScripts, but this is of course not working in the SiteAdmin as my custom JS are not included.
So, do I really have to overwrite the CQ.tagging.js below libs or can I somehow inject my overlay into the SiteAdmin so the PageProperties Dialog opened from there works as well?
UPDATE: I had a chat with Adobe support regarding this and it was pointed out that if you use tagsBasePath you need to place it somewhere else than below /etc/tags. But this won't work as well as the TagListServlet will return no tags as /etc/tags is also fixed in the TagManager as the tagsBasePath. I now overwrite the above mentioned js at its location, being well aware that I need to check it if we install a hotfix or an update. Is someone has a more elegant solution I'd be still thankful.

Failure creating document in Matter Centric HP/Autonomy WorkSite (iManage) DMS

I'm trying to create a document in the DMS, set some attributes, then check it in. This worked great until we disallowed flat-space filing. Now I have to specify a workspace folder before I can save, but I can't seem to figure out how to do so.
lcDoc = mcDatabase.CreateDocument()
lcDoc.Security.DefaultVisibility = imSecurityType.imView
lcDoc.Security.GroupACLs.Add("INFORMATION_TECHNOLOGY", imAccessRight.imRightAll)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileDescription, FileName)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileAuthor, msUserID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileOperator, msUserID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileType, "ANSI")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileClass, "ADMIN")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom1, ClientID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom2, MatterID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom10, "060")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom14, DMSServerName)
' Fails here with error: [Folder ][AddDocument ]Operation requested on a record that does not exist.
DirectCast(loFolder, NRTFolder).AddDocument(DirectCast(lcDoc, NRTDocument))
' If I comment out the line above, this line returns a failure:
' This operation is currently not available due to flatspace filing restrictions.
Dim lcResults As IManCheckinResult = lcDoc.CheckInWithResults(lsFilePath, imCheckinDisposition.imCheckinNewDocument, imCheckinOptions.imDontKeepCheckedOut)
I'm struggling to find the right API document that gives this answer.
OK, so the solution was on an interface I didn't know about, but found the COM developer's guide on page 81.
lcDoc = mcDatabase.CreateDocument()
'lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileLocation, loFolder.ObjectID)
lcDoc.Security.DefaultVisibility = imSecurityType.imView
lcDoc.Security.GroupACLs.Add("APPLICATIONSDEVELOPMENTALL", imAccessRight.imRightAll)
lcDoc.Security.GroupACLs.Add("INFORMATION_TECHNOLOGY", imAccessRight.imRightAll)
' [04/21/2014 KB] Changed this to use the specified name instead of a hard coded one.
' lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileDescription, String.Format("iManageAPI:{0}", "Test Create Document"))
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileDescription, FileName)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileAuthor, msUserID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileOperator, msUserID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileType, "ANSI")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileClass, "ADMIN")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom1, ClientID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom2, MatterID)
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom10, "060")
lcDoc.Profile.SetAttributeByID(imProfileAttributeID.imProfileCustom14, DMSServerName)
''loFolder.AddDocument(lcDoc) 'Add document to folder
'DirectCast(loFolder, NRTFolder).AddDocument(DirectCast(lcDoc, NRTDocument))
'Dim lcResults As IManCheckinResult = lcDoc.CheckInWithResults(lsFilePath, imCheckinDisposition.imCheckinNewDocument, imCheckinOptions.imDontKeepCheckedOut)
lcResults = DirectCast(lcDoc, IManDocument3).
CheckInExToFolderAsNewDocumentWithResults(lsFilePath, imCheckinOptions.imDontKeepCheckedOut, loFolder,
imHistEvent.imHistoryNew,
System.Reflection.Assembly.GetExecutingAssembly().FullName,
"", "")

Facing Issue in zend_search_lucene

I am using Zend Lucene Search:
......
$results = $test->fetchAll();
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
foreach ($results as $result) {
$doc = new Zend_Search_Lucene_Document();
// add Fields
$doc->addField(
Zend_Search_Lucene_Field::Text('testid', $result->id));
$doc->addField(
Zend_Search_Lucene_Field::Keyword('testemail', strtolower(($result->email))));
$doc->addField(
Zend_Search_Lucene_Field::Text('testconfirmdate', $result->confirmdate));
$doc->addField(
Zend_Search_Lucene_Field::Text('testcreateddate', $result->createddate));
// Add document to the index
$index->addDocument($doc);
}
// Optimize index.
$index->optimize();
// Search by query
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
if(strlen($Data['name']) > 2){
//$query = Zend_Search_Lucene_Search_QueryParser::parse($Data['name'].'*');
$pattern = new Zend_Search_Lucene_Index_Term($Data['name'].'*');
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
$this->view->hits = $index->find(strtolower($query));
}
else{
$query = $Data['name'];
$this->view->hits = $index->find($query);
}
............
Works fine here:
It works when I give complete word, first 3 character, case insensitive words
My issues are:
When I search for email, i got error like "Wildcard search is supported only for non-multiple word terms "
When I search for number/date like "1234" or 09/06/2011, I got error like "At least 3 non-wildcard characters are required at the beginning of pattern"
I want to search date, email, number here.
In file zend/search/Lucene/search/search/query/wildcard a parameter is set,
private static $_minPrefixLength = 3;
chnage it and it may work..!
Based on NaanuManu's suggestion, I did a little more digging to figure this out - I posted my answer on a related question here, but repeating for convenience:
Taken directly from the Zend Reference documentation, you can use:
Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() to
query the minimum required prefix length and
use Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength() to
set it.
So my suggestion would be either of two things:
Set the prefixMinLength to 0 using Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0)
Validate all search queries using javascript or otherwise to ensure there is a minimum of Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() before any wildcards used (I recommend querying that instead of assuming the default of "3" so the validation is flexible)